<commit_before><?php
/**
 * @author ueaner <<EMAIL>>
 */
namespace Soli;

use Soli\Http\Response;

/**
 * 应用
 *
 * @property \Soli\RouterInterface $router
 * @property \Soli\Dispatcher $dispatcher
 * @property \Soli\Http\Request $request
 * @property \Soli\Http\Response $response
 * @property \Soli\Session $session
 * @property \Soli\Session\Flash $flash
 * @property \Soli\ViewInterface $view
 */
class Application extends BaseApplication
{
    /**
     * 默认注册服务
     */
    protected $defaultServices = [
        'router'     => \Soli\Router\Simple::class,
        'dispatcher' => \Soli\Dispatcher::class,
        'request'    => \Soli\Http\Request::class,
        'response'   => \Soli\Http\Response::class,
        'session'    => \Soli\Session::class,
        'flash'      => \Soli\Session\Flash::class,
        'filter'     => \Soli\Filter::class,
    ];

    /**
     * 应用程序启动方法
     *
     * @param string|null $uri
     * @return \Soli\Http\Response
     */
    public function handle($uri = null)
    {
        try {
            return $this->handleInternal($uri);
        } catch (\Exception $e) {
            return $this->handleException($e);
        } catch (\Throwable $e) {
            return $this->handleException($e);
        }
    }

    protected function handleInternal($uri)
    {
        $this->trigger('application.boot');

        $router = $this->router;
        $dispatcher = $this->dispatcher;

        $router->handle($uri);

        // 调度器预处理：设置控制器、方法及参数
        if ($router->getNamespaceName()) {
            $dispatcher->setNamespaceName($router->getNamespaceName());
        }
        if ($router->getControllerName()) {
            $dispatcher->setHandlerName($router->getControllerName());
        }
        if ($router->getActionName()) {
            $dispatcher->setActionName($router->getActionName());
        }
        if ($router->getParams()) {
            $dispatcher->setParams($router->getParams());
        }

        // 不自动渲染视图的四种方式:
        // 1. 返回 Response 实例
        // 2. 返回 string 类型作为响应内容
        // 3. 返回 false
        // 4. 禁用视图

        // 执行调度
        $returnedResponse = $dispatcher->dispatch();

        if ($returnedResponse instanceof Response) {
            $response = $returnedResponse;
        } else {
            $response = $this->response;
            if (is_string($returnedResponse)) {
                // 作为响应内容
                $response->setContent($returnedResponse);
            } elseif ($returnedResponse !== false) {
                // 渲染视图
                $response->setContent($this->viewRender());
            }
        }

        $this->trigger('application.finish', $response);

        $response->sendHeaders();
        $response->sendCookies();

        return $response;
    }

    /**
     * 获取视图自动渲染内容
     *
     * @return string
     */
    protected function viewRender()
    {
        if (!$this->container->has('view')) {
            return null;
        }

        // 视图实例
        $view = $this->view;

        // 视图被禁用
        if ($view->isDisabled()) {<commit_msg>统一使用 Response::send() 响应内容、cookie和头信息<commit_after><?php
/**
 * @author ueaner <<EMAIL>>
 */
namespace Soli;

use Soli\Http\Response;

/**
 * 应用
 *
 * @property \Soli\RouterInterface $router
 * @property \Soli\Dispatcher $dispatcher
 * @property \Soli\Http\Request $request
 * @property \Soli\Http\Response $response
 * @property \Soli\Session $session
 * @property \Soli\Session\Flash $flash
 * @property \Soli\ViewInterface $view
 */
class Application extends BaseApplication
{
    /**
     * 默认注册服务
     */
    protected $defaultServices = [
        'router'     => \Soli\Router\Simple::class,
        'dispatcher' => \Soli\Dispatcher::class,
        'request'    => \Soli\Http\Request::class,
        'response'   => \Soli\Http\Response::class,
        'session'    => \Soli\Session::class,
        'flash'      => \Soli\Session\Flash::class,
        'filter'     => \Soli\Filter::class,
    ];

    /**
     * 应用程序启动方法
     *
     * @param string|null $uri
     * @return \Soli\Http\Response
     */
    public function handle($uri = null)
    {
        try {
            return $this->handleInternal($uri);
        } catch (\Exception $e) {
            return $this->handleException($e);
        } catch (\Throwable $e) {
            return $this->handleException($e);
        }
    }

    protected function handleInternal($uri)
    {
        $this->trigger('application.boot');

        $router = $this->router;
        $dispatcher = $this->dispatcher;

        $router->handle($uri);

        // 调度器预处理：设置控制器、方法及参数
        if ($router->getNamespaceName()) {
            $dispatcher->setNamespaceName($router->getNamespaceName());
        }
        if ($router->getControllerName()) {
            $dispatcher->setHandlerName($router->getControllerName());
        }
        if ($router->getActionName()) {
            $dispatcher->setActionName($router->getActionName());
        }
        if ($router->getParams()) {
            $dispatcher->setParams($router->getParams());
        }

        // 不自动渲染视图的四种方式:
        // 1. 返回 Response 实例
        // 2. 返回 string 类型作为响应内容
        // 3. 返回 false
        // 4. 禁用视图

        // 执行调度
        $returnedResponse = $dispatcher->dispatch();

        if ($returnedResponse instanceof Response) {
            $response = $returnedResponse;
        } else {
            $response = $this->response;
            if (is_string($returnedResponse)) {
                // 作为响应内容
                $response->setContent($returnedResponse);
            } elseif ($returnedResponse !== false) {
                // 渲染视图
                $response->setContent($this->viewRender());
            }
        }

        $this->trigger('application.finish', $response);

        return $response;
    }

    /**
     * 获取视图自动渲染内容
     *
     * @return string
     */
    protected function viewRender()
    {
        if (!$this->container->has('view')) {
            return null;
        }

        // 视图实例
        $view = $this->view;

        // 视图被禁用
        if ($view->isDisabled()) {
{'fraction_non_alphanumeric': 0.15244203256043415, 'fraction_numerical': 0.001315573096530176, 'mean_word_length': 1.8420560747663552, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 81, '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': '6926274', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1929, 'n_words': 306}