<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of eelly package.
 *
 * (c) eelly.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Eelly\Application;

use Eelly\Di\Injectable;
use Eelly\Di\WebDi;
use Eelly\Loader\Loader;
use Eelly\Mvc\Application;
use Eelly\SDK\EellyClient;
use Phalcon\Config;

/**
 * Class WebApplication.
 *
 * @property \Eelly\Mvc\Application $application
 */
class WebApplication extends Injectable
{
    public function __construct(array $config)
    {
        $di = new WebDi();
        $di->setShared('config', new Config($config));
        $this->setDI($di);
    }

    public function initialize()
    {
        $di = $this->getDI();
        $di->setShared('application', new Application($di));
        $config = $this->config;
        ApplicationConst::$env = $config->env;
        ApplicationConst::$appName = $config->appName;
        date_default_timezone_set($config->defaultTimezone);
        // TODO WebHandler
        //$errorHandler = $di->getShared(ErrorHandler::class);
        //$errorHandler->register();
        $this->initAutoload()
            ->initEventsManager()
            ->registerServices();

        return $this;
    }

    /**
     * @param string $uri
     *
     * @return \Phalcon\Http\ResponseInterface
     */
    public function handle($uri = null)
    {
        $this->application->useImplicitView(true);
        $response = $this->application->handle($uri);

        return $response;
    }

    public function run(): void
    {
        $this->initialize()->handle()->send();
    }

    /**
     * @return self
     */
    private function initEventsManager()
    {
        $eventsManager = $this->eventsManager;
        $eventsManager->attach('di:afterServiceResolve', function (\Phalcon\Events\Event $event, \Phalcon\Di $di, array $service): void {
            if ($service['instance'] instanceof \Phalcon\Events\EventsAwareInterface) {
                $service['instance']->setEventsManager($di->getEventsManager());
            }
            if (method_exists($service['instance'], 'afterServiceResolve')) {
                $service['instance']->afterServiceResolve();
            }
        });
        $this->application->setEventsManager($eventsManager);
        $this->di->setInternalEventsManager($eventsManager);

        return $this;
    }

    /**
     * @return self
     */
    private function initAutoload()
    {
        $this->loader = new Loader();
        $this->loader->registerNamespaces([
            ApplicationConst::$appName => 'src',
        ])->register();

        return $this;
    }

    /**
     * @return self
     */
    private function registerServices()
    {
        $this->getDI()->setShared('router', $this->config->routes);
        // eelly client service
        $this->getDI()->setShared('eellyClient', function () {
            $options = $this->getConfig()->oauth2Client->eelly->toArray();
            if (ApplicationConst::ENV_PRODUCTION === ApplicationConst::$env) {
                $eellyClient = EellyClient::init($options['options']);
            } else {
                $collaborators = [
                    'httpClient' => new \GuzzleHttp\Client(['verify' => false]),
                ];
                $eellyClient = EellyClient::init($options['options'], $collaborators, $options['providerUri']);
            }
            if ($this->has('cache')) {
                $eellyClient->getProvider()->setAccessTokenCache($this->getCache());
            }

            return $eellyClient;
        });

        return $this;
    }
}<commit_msg>修复webapp的一些问题<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of eelly package.
 *
 * (c) eelly.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Eelly\Application;

use Composer\Autoload\ClassLoader;
use Eelly\Di\Injectable;
use Eelly\Di\WebDi;
use Eelly\Loader\Loader;
use Eelly\Mvc\Application;
use Eelly\SDK\EellyClient;
use Phalcon\Config;

/**
 * Class WebApplication.
 *
 * @property \Eelly\Mvc\Application $application
 */
class WebApplication
{
    /**
     * @var Application
     */
    private $application;

    private $di;

    /**
     * WebApplication constructor.
     * @param ClassLoader $classLoade
     */
    public function __construct(ClassLoader $classLoader)
    {
        $this->di = new WebDi();
        $this->di->setShared('loader', $classLoader);
        if (!file_exists('.env')) {
            file_put_contents('.env', preg_replace(
                    '/^APPLICATION_KEY=/m',
                    'APPLICATION_KEY='.base64_encode(random_bytes(32)),
                    file_get_contents('.env.example'))
            );
        }
        $dotenv = new \Dotenv\Dotenv(getcwd(), '.env');
        $dotenv->load();
        $appEnv = getenv('APPLICATION_ENV');
        $appKey = getenv('APPLICATION_KEY');
        $arrayConfig = require 'var/config/config.'.$appEnv.'.php';
        if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
            $arrayConfig['requestTime'] = $_SERVER['REQUEST_TIME_FLOAT'];
        } elseif (isset($_SERVER['REQUEST_TIME'])) {
            $arrayConfig['requestTime'] = $_SERVER['REQUEST_TIME'];
        } else {
            $arrayConfig['requestTime'] = microtime(true);
        }
        define('APP', [
            'env'      => $appEnv,
            'key'      => $appKey,
            'timezone' => $arrayConfig['timezone'],
            'appname'  => $arrayConfig['appName'],
        ]);
        $this->di->setShared('config', new Config($arrayConfig));
        date_default_timezone_set(APP['timezone']);
        $this->application = $this->di->getShared(Application::class);
        $this->di->setShared('application', $this->application);
    }

    /**
     * @param string $uri
     *
     * @return \Phalcon\Http\ResponseInterface
     */
    public function handle($uri = null)
    {
        $this->initAutoload()
            ->initEventsManager()
            ->registerServices();
        $this->application->useImplicitView(true);
        $response = $this->application->handle($uri);

        return $response;
    }

    public function run(): void
    {
        $this->handle()->send();
    }

    /**
     * @return self
     */
    private function initEventsManager()
    {
        $eventsManager = $this->di->getShared('eventsManager');
        $eventsManager->attach('di:afterServiceResolve', function (\Phalcon\Events\Event $event, \Phalcon\Di $di, array $service): void {
            if ($service['instance'] instanceof \Phalcon\Events\EventsAwareInterface) {
                $service['instance']->setEventsManager($di->getEventsManager());
            }
            if (method_exists($service['instance'], 'afterServiceResolve')) {
                $service['instance']->afterServiceResolve();
            }
        });
        $this->application->setEventsManager($eventsManager);
        $this->di->setInternalEventsManager($eventsManager);

        return $this;
    }

    /**
     * @return self
     */
    private function initAutoload()
    {
        /* @var ClassLoader $loader */
        $loader = $this->di->getShared('loader');
        $loader->addPsr4(APP['appname'].'\\', 'src');
        $loader->register();

        return $this;
    }

    /**
     * @return self
     */
    private function registerServices()
    {
        $this->di->setShared('router', $this->di->getShared('config')->routes);
        // eelly client service
        $this->di->setShared('eellyClient', function () {
            $options = $this->getConfig()->oauth2Client->eelly->toArray();
            if (ApplicationConst::ENV_PRODUCTION === ApplicationConst::$env) {
                $eellyClient = EellyClient::init($options['options']);
            } else {
                $collaborators = [
                    'httpClient' => new \GuzzleHttp\Client(['verify' => false]),
                ];
                $eellyClient = EellyClient::init($options['options'], $collaborators, $options['providerUri']);
            }
            if ($this->has('cache')) {
                $eellyClient->getProvider()->setAccessTokenCache($this->getCache());
            }

            return $eellyClient;
        });

        return $this;
    }
}
{'fraction_non_alphanumeric': 0.15494466262049267, 'fraction_numerical': 0.0010710460549803642, 'mean_word_length': 2.295686274509804, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, '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': '650862', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2336, 'n_words': 409}