<commit_before><?php

    namespace Dez\Mvc;

    use Dez\DependencyInjection\Injectable;
    use Dez\Auth\Auth;
    use Dez\Config\Config;
    use Dez\Db\Connection;
    use Dez\DependencyInjection\Service;
    use Dez\EventDispatcher\Dispatcher;
    use Dez\Http\Cookies;
    use Dez\Http\Request;
    use Dez\Http\Response;
    use Dez\Loader\Loader;
    use Dez\Mvc\Controller\MvcException;
    use Dez\Router\Router;
    use Dez\Session\Adapter;
    use Dez\Url\Url;
    use Dez\View\View;
    use Dez\Flash\Adapter as Flash;

    use Dez\Mvc\Controller\Dispatcher as ControllerDispatcher;


    /**
     * Class Application
     * @package Dez\Mvc
     *
     * @property Loader loader
     * @property Config config
     * @property Dispatcher eventDispatcher
     * @property Dispatcher event
     * @property Request request
     * @property Cookies cookies
     * @property Response response
     * @property Adapter session
     * @property Router router
     * @property Url url
     * @property View view
     * @property Connection db
     * @property Auth auth
     * @property Flash flash
     */

    class Application extends Injectable {

        protected $page404Handler;

        protected $errorHandler;

        protected $controllerNamespace  = '\\App\\Controller\\';

        public function __construct()
        {
            $this->setDi( FactoryContainer::instance() );
            $this->initialize();
        }

        /**
         * @return $this
         */
        public function initialize()
        {
            $this->router->add( '/' );
            $this->router->add( '/:controller' );
            $this->router->add( '/:controller/:action' );
            $this->router->add( '/:controller/:action/:id' );

            /** @var Service $service */
            foreach( $this->dependencyInjector as $service ) {
                $this->view->set( $service->getName(), $this->{ $service->getName() } );
            }

            return $this;
        }

        /**
         * @return Response
         * @throws MvcException
         * @throws \Dez\EventDispatcher\Exception
         * @throws \Exception
         */
        public function run()
        {
            $this->event->dispatch( 'beforeApplicationRun', new MvcEvent( $this ) );

            $router     = $this->router;

            $router->handle();

            if( $router->isFounded() ) {

                $dispatcher = new ControllerDispatcher( $this->getDi() );

                $dispatcher->setNamespace( $this->getControllerNamespace() );
                $dispatcher->setController( $router->getController() );
                $dispatcher->setAction( $router->getAction() );
                $dispatcher->setParams( $router->getMatches() );

                try {
                    $dispatcher->dispatch();

                    $this->view->addLayout( "layouts/{$router->getController()}" );
                    $content    = $this->view->render( "{$router->getController()}/{$router->getAction()}" );

                    $this->response->setContent( $content );

                    $this->event->dispatch( 'afterApplicationRun', new MvcEvent( $this ) );
                } catch ( \Exception $exception ) {
                    $this->response->setStatusCode( 500 );

                    if( $this->getErrorHandler() instanceof \Closure ) {
                        call_user_func_array( $this->getErrorHandler(), [ $exception, $this ] );
                        $this->response->setContent( $this->view->render( 'internal_error.php' ) );
                    } else {
                        throw $exception;
                    }
                }

            } else {
                $this->event->dispatch( 'onPageNotFound', new MvcEvent( $this ) );
                $this->response->setStatusCode( 404 );

                if( $this->getPage404Handler() instanceof \Closure ) {
                    call_user_func_array( $this->getPage404Handler(), [ $this ] );
                    $this->response->setContent( $this->view->render( 'error_404.php' ) );
                } else {
                    throw new MvcException( "Page {$router->getTargetUri()} not found" );
                }
            }

            return $this->response;
        }

        /**
         * @return \Closure<commit_msg>headers<commit_after><?php

    namespace Dez\Mvc;

    use Dez\DependencyInjection\Injectable;
    use Dez\Auth\Auth;
    use Dez\Config\Config;
    use Dez\Db\Connection;
    use Dez\DependencyInjection\Service;
    use Dez\EventDispatcher\Dispatcher;
    use Dez\Http\Cookies;
    use Dez\Http\Request;
    use Dez\Http\Response;
    use Dez\Loader\Loader;
    use Dez\Mvc\Controller\MvcException;
    use Dez\Router\Router;
    use Dez\Session\Adapter;
    use Dez\Url\Url;
    use Dez\View\View;
    use Dez\Flash\Adapter as Flash;

    use Dez\Mvc\Controller\Dispatcher as ControllerDispatcher;


    /**
     * Class Application
     * @package Dez\Mvc
     *
     * @property Loader loader
     * @property Config config
     * @property Dispatcher eventDispatcher
     * @property Dispatcher event
     * @property Request request
     * @property Cookies cookies
     * @property Response response
     * @property Adapter session
     * @property Router router
     * @property Url url
     * @property View view
     * @property Connection db
     * @property Auth auth
     * @property Flash flash
     */

    class Application extends Injectable {

        protected $page404Handler;

        protected $errorHandler;

        protected $controllerNamespace  = '\\App\\Controller\\';

        public function __construct()
        {
            $this->setDi( FactoryContainer::instance() );
            $this->initialize();
        }

        /**
         * @return $this
         */
        public function initialize()
        {
            $this->router->add( '/' );
            $this->router->add( '/:controller' );
            $this->router->add( '/:controller/:action' );
            $this->router->add( '/:controller/:action/:id' );

            /** @var Service $service */
            foreach( $this->dependencyInjector as $service ) {
                $this->view->set( $service->getName(), $this->{ $service->getName() } );
            }

            return $this;
        }

        /**
         * @return Response
         * @throws MvcException
         * @throws \Dez\EventDispatcher\Exception
         * @throws \Exception
         */
        public function run()
        {
            $this->event->dispatch( 'beforeApplicationRun', new MvcEvent( $this ) );

            $router     = $this->router;

            $router->handle();

            if( $router->isFounded() ) {

                $dispatcher = new ControllerDispatcher( $this->getDi() );

                $dispatcher->setNamespace( $this->getControllerNamespace() );
                $dispatcher->setController( $router->getController() );
                $dispatcher->setAction( $router->getAction() );
                $dispatcher->setParams( $router->getMatches() );

                try {
                    $dispatcher->dispatch();

                    $this->view->addLayout( "layouts/{$router->getController()}" );
                    $content    = $this->render( "{$router->getController()}/{$router->getAction()}" );

                    $this->response->setContent( $content );

                    $this->event->dispatch( 'afterApplicationRun', new MvcEvent( $this ) );
                } catch ( \Exception $exception ) {
                    $this->response->setStatusCode( 500 );

                    if( $this->getErrorHandler() instanceof \Closure ) {
                        call_user_func_array( $this->getErrorHandler(), [ $exception, $this ] );
                        $this->response->setContent( $this->render( 'internal_error.php' ) );
                    } else {
                        throw $exception;
                    }
                }

            } else {
                $this->event->dispatch( 'onPageNotFound', new MvcEvent( $this ) );
                $this->response->setStatusCode( 404 );

                if( $this->getPage404Handler() instanceof \Closure ) {
                    call_user_func_array( $this->getPage404Handler(), [ $this ] );
                    $this->response->setContent( $this->render( 'error_404.php' ) );
                } else {
                    throw new MvcException( "Page {$router->getTargetUri()} not found" );
                }
            }

            return $this->response->sendContent();
        }

        protected function render( $path )
        {
            $this->response->sendCookies()->sendHeaders();
            return $this->view->render( $path );
        }

        /**
         * @return \Closure
{'fraction_non_alphanumeric': 0.13319695419934083, 'fraction_numerical': 0.004091374019774974, 'mean_word_length': 1.698558724317694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 130, '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': '2451673', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2271, 'n_words': 457}