<commit_before><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/11/19
 * Time: 下午11:39
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 * WebSite: http://www.janhuang.me
 */

namespace FastD\Framework\Dispatcher\Handle;

use FastD\Framework\Bundle\Controllers\ControllerInterface;
use FastD\Framework\Dispatcher\Dispatch;
use FastD\Http\Response;
use FastD\Routing\Route;

/**
 * Framework kernel boot http handle.
 * Handle every time user request.
 *
 * Class RequestHandler
 *
 * @package FastD\Framework\Dispatcher\Handle
 */
class RequestHandler extends Dispatch
{
    /**
     * @return string
     */
    public function getName()
    {
        return 'handle.http';
    }

    /**
     * @param Route $route
     * @return mixed
     * @throws \Exception
     */
    public function handleRoute(Route $route)
    {
        $callback = $route->getCallback();

        list($controller, $action) = explode('@', $callback);
        $controller = str_replace(':', '\\', $controller);
        $bundle = trim(substr($controller, 0, strpos($controller, 'Controller')), '\\');
        $this->getContainer()->singleton('kernel')->setActiveBundle($bundle); unset($bundle);

        $service = $this->container->set('request_callback', $controller)->get('request_callback');
        if (($instance = $service->singleton()) instanceof ControllerInterface) {
            $instance->setContainer($this->container);
            $instance->currentAction = $action;
        }

        try {
            $response = $service->__initialize();
            if ($response instanceof Response) {
                return $response;
            }
        } catch (\Exception $e) {}

        $response = call_user_func_array([$service, $action], $route->getParameters());
        $instance->statusCode = $response->getStatusCode() === 200 ? 1 : 0;
        if (1 !== $instance->statusCode) {
            $instance->code = $response->getStatusCode();
            $instance->msg = $response->getContent();
        }
        unset($instance);
        return $response;
    }

    /**
     * @param array|null $parameters
     * @return mixed
     */
    public function dispatch(array $parameters = null)
    {
        $route = $this->getContainer()->singleton('kernel.routing')->match($parameters[0]->getMethod(), $parameters[0]->getPathInfo());

        return $this->handleRoute($route);
    }
}<commit_msg>update handle<commit_after><?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/11/19
 * Time: 下午11:39
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: <EMAIL>
 * WebSite: http://www.janhuang.me
 */

namespace FastD\Framework\Dispatcher\Handle;

use FastD\Framework\Bundle\Controllers\ControllerInterface;
use FastD\Framework\Dispatcher\Dispatch;
use FastD\Http\Response;
use FastD\Routing\Route;

/**
 * Framework kernel boot http handle.
 * Handle every time user request.
 *
 * Class RequestHandler
 *
 * @package FastD\Framework\Dispatcher\Handle
 */
class RequestHandler extends Dispatch
{
    /**
     * @return string
     */
    public function getName()
    {
        return 'handle.http';
    }

    /**
     * @param Route $route
     * @return mixed
     * @throws \Exception
     */
    public function handleRoute(Route $route)
    {
        $callback = $route->getCallback();

        list($controller, $action) = explode('@', $callback);
        $controller = str_replace(':', '\\', $controller);
        $bundle = trim(substr($controller, 0, strpos($controller, 'Controller')), '\\');
        $this->getContainer()->singleton('kernel')->setActiveBundle($bundle); unset($bundle);

        $service = $this->container->set('request_callback', $controller)->get('request_callback');
        if (($instance = $service->singleton()) instanceof ControllerInterface) {
            $instance->setContainer($this->container);
            $instance->currentAction = $action;
        }

        try {
            $response = $service->__initialize();
            if ($response instanceof Response) {
                return $response;
            }
            $response = call_user_func_array([$service, $action], $route->getParameters());
            $instance->statusCode = $response->getStatusCode() === 200 ? 1 : 0;
            if (1 !== $instance->statusCode) {
                $instance->code = $response->getStatusCode();
                $instance->msg = $response->getContent();
            }

            unset($instance);
            return $response;
        } catch (\Exception $e) {
            return new Response('server interval error.', 500);
        }
    }

    /**
     * @param array|null $parameters
     * @return mixed
     */
    public function dispatch(array $parameters = null)
    {
        $route = $this->getContainer()->singleton('kernel.routing')->match($parameters[0]->getMethod(), $parameters[0]->getPathInfo());

        return $this->handleRoute($route);
    }
}
{'fraction_non_alphanumeric': 0.15845947499520982, 'fraction_numerical': 0.007855911094079326, 'mean_word_length': 2.712660028449502, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, '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': '1210523', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1467, 'n_words': 289}