<commit_before><?php

namespace SideDevOrg\MiniPhpFw;

/**
 * Framework bootstrap class.
 */
class Bootstrap
{
    /**
     * Default options.
     *
     * @var array
     */
    private $config = [
        'lang' => 'en',
        'paths' => [
            'database' => 'app/config/database.php',
            'middlewares' => 'app/config/middlewares.php',
            'routes' => 'app/config/routes.php',
            'env' => 'app',
            'view' => 'app/views',
            'i18n' => 'app/langs',
            'routesCache' => 'app/storage/route.cache',
            'assets_manifest' => 'static/build/mix-manifest.json',
        ],
        'config' => [
            'not_found_controller' => '\SideDevOrg\MiniPhpFw\Controller::not_found',
        ]
    ];

    /**
     * Response
     *
     * @var \Psr\Http\Message\ResponseInterface
     */
    private $response;

    /**
     * Load framework.
     *
     * @param array $config
     *
     * @return \SideDevOrg\MiniPhpFw\Bootstrap
     */
    public function load(array $config = [])
    {
        $this->config = array_replace_recursive($this->config, $config);

        $this->enviroment();
        $this->errors();
        $this->templates();
        $this->orm();
        $this->router();

        return $this;
    }

    /**
     * Load errors strategy.
     */
    private function errors()
    {
        if (env('DEV_MODE')) {
            $whoops = new \Whoops\Run();
            $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
            $whoops->register();
        }
    }

    /**
     * Load enviroment strategy.
     */
    private function enviroment()
    {
        $dotenv = new \Dotenv\Dotenv($this->config['paths']['env']);
        $dotenv->load();
    }

    /**
     * Load templates strategy.
     */
    private function templates()
    {
        \Mustache_Autoloader::register();
    }

    /**
     * Load orm strategy.
     */
    private function orm()
    {
        if (env('LOAD_ORM')) {
            $capsule = new \Illuminate\Database\Capsule\Manager();
            $connections = require_once $this->config['paths']['database'];
            foreach ($connections as $connection) {
                $name = $connection['name'];
                unset($connection['name']);
                $capsule->addConnection($connection, $name);
            }
            $capsule->setEventDispatcher(
                new \Illuminate\Events\Dispatcher(
                    new \Illuminate\Container\Container()
                )
            );
            $capsule->setAsGlobal();
            $capsule->bootEloquent();
        }
    }

    /**
     * Load router strategy.
     */
    private function router()
    {
        $routes = require_once $this->config['paths']['routes'];
        $request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();

        $dispatcher = \FastRoute\cachedDispatcher(
            function (\FastRoute\RouteCollector $r) use ($routes) {
                foreach ($routes as $route) {
                    $r->addRoute($route['methods'], $route['endpoint'], $route['call']);
                }
            },
            [
                'cacheFile' => $this->config['paths']['routesCache'],
            ]
        );

        $request = $request->withHeader('config', json_encode($this->config));

        $routeInfo = $dispatcher->dispatch(
            $request->getMethod(),
            $request->getUri()->getPath()
        );

        switch ($routeInfo[0]) {

            case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
                $allowedMethods = $routeInfo[1];
                echo '405 METHOD_NOT_ALLOWED';
                break;

            case \FastRoute\Dispatcher::NOT_FOUND:
            case \FastRoute\Dispatcher::FOUND:

                $handler = isset($routeInfo[1]) ? $routeInfo[1] : $this->config['config']['not_found_controller'];
                $vars = isset($routeInfo[2]) ? $routeInfo[2] : [];
                $handler = explode('::', $handler);
                $class = $handler[0];
                $method = $handler[1];

                $response = (new \SideDevOrg\MiniPhpFw\Resolver())->dispatch(
                    $class,
                    $method,
                    $vars,
                    $request,
                    $response = new \Zend\Diactoros\Response(),
                    $middlewares = require_once $this->config['paths']['middlewares']
                );

                $this->response = $response;
                break;
        }
    }

    /**
     * Get response.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getResponse() : \Psr\Http\Message\ResponseInterface
    {
        return $this->response;
    }
}
<commit_msg>update and fix bootstrap class<commit_after><?php

namespace SideDevOrg\MiniPhpFw;

/**
 * Framework bootstrap class.
 */
class Bootstrap
{
    /**
     * Default options.
     *
     * @var array
     */
    private $config = [
        'lang' => 'en',
        'paths' => [
            'database' => 'app/config/database.php',
            'middlewares' => 'app/config/middlewares.php',
            'routes' => 'app/config/routes.php',
            'env' => 'app',
            'view' => 'app/views',
            'i18n' => 'app/langs',
            'routesCache' => 'app/storage/route.cache',
            'assets_manifest' => 'static/build/mix-manifest.json',
        ],
        'config' => [
            'not_found_controller' => '\SideDevOrg\MiniPhpFw\Controller::not_found',
        ]
    ];

    /**
     * Response
     *
     * @var \Psr\Http\Message\ResponseInterface
     */
    private $response;

    /**
     * Load framework.
     *
     * @param array $config
     *
     * @return \SideDevOrg\MiniPhpFw\Bootstrap
     */
    public function load(array $config = [])
    {
        $this->config = array_replace_recursive($this->config, $config);

        $this->enviroment();
        $this->errors();
        $this->templates();
        $this->orm();
        $this->router();

        return $this;
    }

    /**
     * Load errors strategy.
     */
    private function errors()
    {
        if (env('DEV_MODE')) {
            $whoops = new \Whoops\Run();
            $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
            $whoops->register();
        }
    }

    /**
     * Load enviroment strategy.
     */
    private function enviroment()
    {
        $dotenv = new \Dotenv\Dotenv($this->config['paths']['env']);
        $dotenv->load();
    }

    /**
     * Load templates strategy.
     */
    private function templates()
    {
        \Mustache_Autoloader::register();
    }

    /**
     * Load orm strategy.
     */
    private function orm()
    {
        if (env('LOAD_ORM')) {
            $capsule = new \Illuminate\Database\Capsule\Manager();
            $connections = require $this->config['paths']['database'];
            foreach ($connections as $connection) {
                $name = $connection['name'];
                unset($connection['name']);
                $capsule->addConnection($connection, $name);
            }
            $capsule->setEventDispatcher(
                new \Illuminate\Events\Dispatcher(
                    new \Illuminate\Container\Container()
                )
            );
            $capsule->setAsGlobal();
            $capsule->bootEloquent();
        }
    }

    /**
     * Load router strategy.
     */
    private function router()
    {
        $routes = require $this->config['paths']['routes'];
        $request = \Zend\Diactoros\ServerRequestFactory::fromGlobals();

        $dispatcher = \FastRoute\cachedDispatcher(
            function (\FastRoute\RouteCollector $r) use ($routes) {
                foreach ($routes as $route) {
                    $r->addRoute($route['methods'], $route['endpoint'], $route['call']);
                }
            },
            [
                'cacheFile' => $this->config['paths']['routesCache'],
            ]
        );

        $request = $request->withHeader('config', json_encode($this->config));

        $routeInfo = $dispatcher->dispatch(
            $request->getMethod(),
            $request->getUri()->getPath()
        );

        $response = new \Zend\Diactoros\Response();

        switch ($routeInfo[0]) {

            case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
                $allowedMethods = $routeInfo[1];
                $response->getBody()->write('METHOD NOT ALLOWED');
                $response = $response->withStatus(405);
                $this->response = $response;
                break;

            case \FastRoute\Dispatcher::NOT_FOUND:
            case \FastRoute\Dispatcher::FOUND:

                $handler = isset($routeInfo[1]) ? $routeInfo[1] : $this->config['config']['not_found_controller'];
                $vars = isset($routeInfo[2]) ? $routeInfo[2] : [];
                $handler = explode('::', $handler);
                $class = $handler[0];
                $method = $handler[1];

                $response = (new \SideDevOrg\MiniPhpFw\Resolver())->dispatch(
                    $class,
                    $method,
                    $vars,
                    $request,
                    $response,
                    $middlewares = require $this->config['paths']['middlewares']
                );

                if (!isset($routeInfo[1])) {
                    $response = $response->withStatus(404);
                }
                $this->response = $response;
                break;
        }
    }

    /**
     * Get response.
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getResponse() : \Psr\Http\Message\ResponseInterface
    {
        return $this->response;
    }
}

{'fraction_non_alphanumeric': 0.1530674846625767, 'fraction_numerical': 0.003067484662576687, 'mean_word_length': 1.5992559128355035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 94, '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': '5855237', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2721, 'n_words': 435}