<commit_before><?php
namespace Metagist;

/**
 * WebController for Metagist.
 * 
 * @author <NAME> <<EMAIL>>
 */
class WebController
{
    /**
     * the application instance
     * @var Application 
     */
    protected $application;

    /**
     * Constructor.
     * 
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->application = $app;
        $this->initRoutes();
    }
    
    /**
     * Routing setup.
     * 
     * 
     */
    protected function initRoutes()
    {
        $routes = array(
            'homepage' => array('match' => '/', 'method' => 'index'),
            'errors' => array('match' => '/errors', 'method' => 'errors'),
            'login' => array('match' => '/auth/login', 'method' => 'login'),
            'logout' => array('match' => '/auth/logout', 'method' => 'logout'),
            'vote' => array('match' => '/vote/{author}/{name}', 'method' => 'vote'),
            'package' => array('match' => '/package/{author}/{name}', 'method' => 'package'),
        );
        
        foreach ($routes as $name => $data) {
            $this->application
                ->match($data['match'], array($this, $data['method']))
                ->bind($name);
        }
        
        $this->registerErrorFunction();
    }

    /**
     * Default.
     * 
     * @return string
     */
    public function index()
    {
        return $this->application->render('index.html.twig');
    }

    /**
     * Github oAuth, redirect to use the github strategy.
     */
    public function login()
    {
        return $this->application->redirect('/auth/login/github');
    }
    
    /**
     * Logout clears the session.
     * 
     * @return RedirectResponse
     */
    public function logout()
    {
        $this->session()->clear();
        return $this->application->redirect(
            $this->application['url_generator']->generate('homepage')
        );
    }
    
    /**
     * Shows package info.
     * 
     * @param string $author
     * @param string $name
     * @return string
     */
    public function package($author, $name)
    {
        $repository = $this->application->packages();
        $package = $repository->byAuthorAndName($author, $name);
        return $this->application->render(
            'package.html.twig',
            array(
                'package' => $package,
            )
        );
    }
    
    /**
     * Vote a package.
     * 
     * @param string $author
     * @param string $name
     * @return string
     */
    public function vote($author, $name)
    {
        $package = $this->application->packages()->byAuthorAndName($author, $name);
        return $this->application->render(
            'vote.html.twig',
            array(
                'package' => $package,
            )
        );
    }
    
    /**
     * Renders any errors.
     * 
     * @return string
     */
    public function errors()
    {
        $flashBag = $this->application->session()->getFlashBag();
        $flashBag->add('warning', 'Warning flash message');
        $flashBag->add('info', 'Info flash message');
        $flashBag->add('success', 'Success flash message');
        $flashBag->add('error', 'Error flash message');

        return $this->render('errors.html.twig');
    }
    
    /**
     * 
     * @return void
     */
    protected function registerErrorFunction()
    {
        $app = $this->application;
        $this->application->error(function (\Exception $exception, $code) use ($app) {
            if ($app['debug']) {
                return;
            }

            switch ($code) {
                case 404:
                    $message = 'The requested page could not be found.';
                    break;
                default:
                    $message = 'We are sorry, but something went terribly wrong.';
            }

            return new Response($message, $code);
        });
    }
}
<commit_msg>showing packages with recent updates on index page<commit_after><?php
namespace Metagist;

/**
 * WebController for Metagist.
 * 
 * @author <NAME> <<EMAIL>>
 */
class WebController
{
    /**
     * the application instance
     * @var Application 
     */
    protected $application;

    /**
     * Constructor.
     * 
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->application = $app;
        $this->initRoutes();
    }
    
    /**
     * Routing setup.
     * 
     * 
     */
    protected function initRoutes()
    {
        $routes = array(
            'homepage' => array('match' => '/', 'method' => 'index'),
            'errors' => array('match' => '/errors', 'method' => 'errors'),
            'login' => array('match' => '/auth/login', 'method' => 'login'),
            'logout' => array('match' => '/auth/logout', 'method' => 'logout'),
            'vote' => array('match' => '/vote/{author}/{name}', 'method' => 'vote'),
            'package' => array('match' => '/package/{author}/{name}', 'method' => 'package'),
        );
        
        foreach ($routes as $name => $data) {
            $this->application
                ->match($data['match'], array($this, $data['method']))
                ->bind($name);
        }
        
        $this->registerErrorFunction();
    }

    /**
     * Default.
     * 
     * @return string
     */
    public function index()
    {
        return $this->application->render(
            'index.html.twig',
            array(
                'latest' => $this->application->packages()->latest(),
            )
        );
    }

    /**
     * Github oAuth, redirect to use the github strategy.
     */
    public function login()
    {
        return $this->application->redirect('/auth/login/github');
    }
    
    /**
     * Logout clears the session.
     * 
     * @return RedirectResponse
     */
    public function logout()
    {
        $this->session()->clear();
        return $this->application->redirect(
            $this->application['url_generator']->generate('homepage')
        );
    }
    
    /**
     * Shows package info.
     * 
     * @param string $author
     * @param string $name
     * @return string
     */
    public function package($author, $name)
    {
        $package = $this->getPackage($author, $name);
        return $this->application->render(
            'package.html.twig',
            array(
                'package' => $package,
            )
        );
    }
    
    /**
     * Vote a package.
     * 
     * @param string $author
     * @param string $name
     * @return string
     */
    public function vote($author, $name)
    {
        $package = $this->application->packages()->byAuthorAndName($author, $name);
        return $this->application->render(
            'vote.html.twig',
            array(
                'package' => $package,
            )
        );
    }
    
    /**
     * Renders any errors.
     * 
     * @return string
     */
    public function errors()
    {
        $flashBag = $this->application->session()->getFlashBag();
        $flashBag->add('warning', 'Warning flash message');
        $flashBag->add('info', 'Info flash message');
        $flashBag->add('success', 'Success flash message');
        $flashBag->add('error', 'Error flash message');

        return $this->render('errors.html.twig');
    }
    
    /**
     * 
     * @return void
     */
    protected function registerErrorFunction()
    {
        $app = $this->application;
        $this->application->error(function (\Exception $exception, $code) use ($app) {
            if ($app['debug']) {
                return;
            }

            switch ($code) {
                case 404:
                    $message = 'The requested page could not be found.';
                    break;
                default:
                    $message = 'We are sorry, but something went terribly wrong.';
            }

            return new Response($message, $code);
        });
    }
    
    /**
     * Retrieves a package either from the db or packagist.
     * 
     * @param string $author
     * @param string $name
     * @return Package
     */
    protected function getPackage($author, $name)
    {
        $packageRepo = $this->application->packages();
        $package = $packageRepo->byAuthorAndName($author, $name);
        if ($package == null) {
            $package = $this->application[RepoProvider::PACKAGE_FACTORY]->byAuthorAndName($author, $name);
            if ($packageRepo->save($package)) {
                /* @var $metaInfoRepo MetaInfoRepository */
                $metaInfoRepo = $this->application[RepoProvider::METAINFO_REPO];
                $metaInfoRepo->savePackage($package);
            }
        }
        
        return $package;
    }
}

{'fraction_non_alphanumeric': 0.15803196916449383, 'fraction_numerical': 0.000680194989230246, 'mean_word_length': 1.6676746295736318, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861756', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2420, 'n_words': 513}