<commit_before><?php

namespace Core;

use Prob\Handler\ParameterMap;
use Prob\Router\Dispatcher;
use Prob\Rewrite\Request;
use Prob\Router\Map;
use Prob\Router\Matcher;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

class Application
{
    /**
     * @var Map
     */
    private $routerMap;

    private $routeConfig = [];
    private $siteConfig = [];
    private $viewEngineConfig = [];
    private $dbConfig = [];

    /**
     * Singleton: private constructor
     */
    private function __construct()
    {
    }

    /**
     * @return Application
     */
    public static function getInstance()
    {
        static $instance = null;

        if ($instance === null) {
            $instance = new self();
        }

        return $instance;
    }

    public function boot()
    {
        $this->setDisplayError();

        $this->setSiteConfig(require '../config/site.php');
        $this->setDbConfig(require '../config/db.php');
        $this->setViewEngineConfig(require '../config/viewEngine.php');
        $this->setRouterConfig(require '../config/router.php');

        $this->dispatcher(new Request());
    }

    public function setDisplayError($isDisplay = true)
    {
        error_reporting(E_ALL);
        ini_set('display_errors', $isDisplay);
    }

    public function setRouterConfig(array $routeConfig)
    {
        $this->routeConfig = $routeConfig;
        $this->buildRouterMap();
    }

    public function setSiteConfig(array $siteConfig)
    {
        $this->siteConfig = $siteConfig;
    }

    public function setDbConfig(array $dbConfig)
    {
        $this->dbConfig = $dbConfig;
    }

    public function setViewEngineConfig(array $viewEngineConfig)
    {
        $this->viewEngineConfig = $viewEngineConfig;
    }

    private function buildRouterMap()
    {
        $routerMap = new Map();

        foreach ($this->routeConfig as $k => $v) {
            if ($k === 'namespace') {
                $routerMap->setNamespace($v);
            } else {
                $this->addRouterMap($routerMap, $k, $v);
            }
        }

        $this->routerMap = $routerMap;
    }

    private function addRouterMap(Map $routerMap, $path, $handler)
    {
        // string | closure
        if (gettype($handler) === 'string' || is_callable($handler)) {
            $routerMap->get($path, $handler);
            return;
        }

        // array
        // [
        //     'GET' => '...',
        //     'POST' => '...'
        // ]
        if (isset($handler['GET'])) {
            $routerMap->get($path, $handler['GET']);
        }
        if (isset($handler['POST'])) {
            $routerMap->post($path, $handler['POST']);
        }
    }


    public function url($url = '')
    {<commit_msg>Application::addRouterMap() 주석을 이해하기 쉽게 다듬음<commit_after><?php

namespace Core;

use Prob\Handler\ParameterMap;
use Prob\Router\Dispatcher;
use Prob\Rewrite\Request;
use Prob\Router\Map;
use Prob\Router\Matcher;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

class Application
{
    /**
     * @var Map
     */
    private $routerMap;

    private $routeConfig = [];
    private $siteConfig = [];
    private $viewEngineConfig = [];
    private $dbConfig = [];

    /**
     * Singleton: private constructor
     */
    private function __construct()
    {
    }

    /**
     * @return Application
     */
    public static function getInstance()
    {
        static $instance = null;

        if ($instance === null) {
            $instance = new self();
        }

        return $instance;
    }

    public function boot()
    {
        $this->setDisplayError();

        $this->setSiteConfig(require '../config/site.php');
        $this->setDbConfig(require '../config/db.php');
        $this->setViewEngineConfig(require '../config/viewEngine.php');
        $this->setRouterConfig(require '../config/router.php');

        $this->dispatcher(new Request());
    }

    public function setDisplayError($isDisplay = true)
    {
        error_reporting(E_ALL);
        ini_set('display_errors', $isDisplay);
    }

    public function setRouterConfig(array $routeConfig)
    {
        $this->routeConfig = $routeConfig;
        $this->buildRouterMap();
    }

    public function setSiteConfig(array $siteConfig)
    {
        $this->siteConfig = $siteConfig;
    }

    public function setDbConfig(array $dbConfig)
    {
        $this->dbConfig = $dbConfig;
    }

    public function setViewEngineConfig(array $viewEngineConfig)
    {
        $this->viewEngineConfig = $viewEngineConfig;
    }

    private function buildRouterMap()
    {
        $routerMap = new Map();

        foreach ($this->routeConfig as $k => $v) {
            if ($k === 'namespace') {
                $routerMap->setNamespace($v);
            } else {
                $this->addRouterMap($routerMap, $k, $v);
            }
        }

        $this->routerMap = $routerMap;
    }

    /**
     * @param Map    $routerMap
     * @param string $path  url path
     * @param string|array|closure $handler
     */
    private function addRouterMap(Map $routerMap, $path, $handler)
    {
        // string | closure
        if (gettype($handler) === 'string' || is_callable($handler)) {
            $routerMap->get($path, $handler);
            return;
        }

        /**
         * $handler array schema
         * (optional) $handler['GET' | 'POST']
         *                => method_name(ex. 'classname.methodname')
         *                    or function_name
         *                    or closure
         */
        if (isset($handler['GET'])) {
            $routerMap->get($path, $handler['GET']);
        }
        if (isset($handler['POST'])) {
            $routerMap->post($path, $handler['POST']);
        }
    }


    public function url($url = '')
    {
{'fraction_non_alphanumeric': 0.1455793991416309, 'fraction_numerical': 0.0, 'mean_word_length': 1.9800511508951406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4698071', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1657, 'n_words': 324}