<commit_before><?php

namespace Penny;

use Exception;
use Penny\Config\Loader;
use Penny\Event\HttpFlowEvent;
use ReflectionClass;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Interop\Container\ContainerInterface;

class App
{
    /**
     * Dependency Injection container.
     *
     * @var ContainerInterface
     */
    private $container;

    /**
     * Application initialization.
     *
     * @param ContainerInterface $container Dependency Injection container.
     *
     * @throws Exception If no router is defined.
     */
    public function __construct(ContainerInterface $container = null)
    {
        if ($container === null) {
            $container = Container\PHPDiFactory::buildContainer(Loader::load());
        }

        if ($container->has('router') === false) {
            throw new Exception('Define router config');
        }

        $this->container = $container;
    }

    /**
     * Container getter.
     *
     * @return ContainerInterface
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Penny dispatcher getter.
     *
     * @return Dispatcher
     */
    private function getDispatcher()
    {
        $dispatcher = $this->container->get('dispatcher');
        if (!is_callable($dispatcher)) {
            throw new \RuntimeException('Dispatcher must be a callable');
        }

        return $dispatcher;
    }

    /**
     * Penny HTTP flow event getter.
     *
     * @return HttpFlowEvent
     */
    private function getEventManager()
    {
        return $this->container->get('event_manager');
    }

    /**
     * Application execution.
     *
     * @param mixed|null $request  Representation of an outgoing, client-side request.
     * @param mixed|null $response Representation of an incoming, server-side response.
     *
     * @return mixed
     */
    public function run($request = null, $response = null)
    {
        $request = $request ?: ServerRequestFactory::fromGlobals();
        $response = $response ?: new Response();

        $event = new HttpFlowEvent('bootstrap', $request, $response);

        $dispatcher = $this->getDispatcher();
        $httpFlow = $this->getEventManager();

        try {
            $routerInfo = call_user_func($dispatcher, $request);
        } catch (Exception $exception) {
            $event->setName('ERROR_DISPATCH');
            $event->setException($exception);
            $httpFlow->trigger($event);

            return $event->getResponse();
        }

        $controller = $this->container->get($routerInfo[1][0]);
        $method = $routerInfo[1][1];
        $function = (new ReflectionClass($controller))->getShortName();

        $eventName = sprintf('%s.%s', strtolower($function), $method);
        $event->setName($eventName);
        $event->setRouteInfo($routerInfo);

        $httpFlow->attach($eventName, function ($event) use ($controller, $method) {
            $event->setResponse(call_user_func_array(
                [$controller, $method],
                [$event->getRequest(), $event->getResponse()] + $event->getRouteInfo()[2]
            ));
        }, 0);
<commit_msg>Use request and response by getter<commit_after><?php

namespace Penny;

use Exception;
use Penny\Config\Loader;
use Penny\Event\HttpFlowEvent;
use ReflectionClass;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Interop\Container\ContainerInterface;

class App
{
    /**
     * Dependency Injection container.
     *
     * @var ContainerInterface
     */
    private $container;

    /**
     * Application initialization.
     *
     * @param ContainerInterface $container Dependency Injection container.
     *
     * @throws Exception If no router is defined.
     */
    public function __construct(ContainerInterface $container = null)
    {
        if ($container === null) {
            $container = Container\PHPDiFactory::buildContainer(Loader::load());
        }

        if ($container->has('router') === false) {
            throw new Exception('Define router config');
        }

        $this->container = $container;
    }

    /**
     * Container getter.
     *
     * @return ContainerInterface
     */
    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Penny dispatcher getter.
     *
     * @return Dispatcher
     */
    private function getDispatcher()
    {
        $dispatcher = $this->container->get('dispatcher');
        if (!is_callable($dispatcher)) {
            throw new \RuntimeException('Dispatcher must be a callable');
        }

        return $dispatcher;
    }

    /**
     * Penny HTTP flow event getter.
     *
     * @return HttpFlowEvent
     */
    private function getEventManager()
    {
        return $this->container->get('event_manager');
    }

    /**
     * Application execution.
     *
     * @param mixed|null $request  Representation of an outgoing, client-side request.
     * @param mixed|null $response Representation of an incoming, server-side response.
     *
     * @return mixed
     */
    public function run($request = null, $response = null)
    {
        $request = $request ?: ServerRequestFactory::fromGlobals();
        $response = $response ?: new Response();

        $event = new HttpFlowEvent('bootstrap', $request, $response);

        $dispatcher = $this->getDispatcher();
        $httpFlow = $this->getEventManager();

        try {
            $routerInfo = call_user_func($dispatcher, $event->getRequest());
        } catch (Exception $exception) {
            $event->setName('ERROR_DISPATCH');
            $event->setException($exception);
            $httpFlow->trigger($event);

            return $event->getResponse();
        }

        $controller = $this->container->get($routerInfo[1][0]);
        $method = $routerInfo[1][1];
        $function = (new ReflectionClass($controller))->getShortName();

        $eventName = sprintf('%s.%s', strtolower($function), $method);
        $event->setName($eventName);
        $event->setRouteInfo($routerInfo);

        $httpFlow->attach($eventName, function ($event) use ($controller, $method) {
            $event->setResponse(call_user_func_array(
                [$controller, $method],
                [$event->getRequest(), $event->getResponse()] + $event->getRouteInfo()[2]
            ));
        }, 0);

{'fraction_non_alphanumeric': 0.13399968764641573, 'fraction_numerical': 0.0018741215055442762, 'mean_word_length': 2.393746687864335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, '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': '3247431', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1709, 'n_words': 375}