<commit_before><?php

namespace Bone\Mvc\Router;

use Bone\Mvc\Router\Decorator\ExceptionDecorator;
use Bone\Mvc\Router\Decorator\NotAllowedDecorator;
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
use Bone\Mvc\View\PlatesEngine;
use Bone\Traits\HasLayoutTrait;
use Exception;
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
use League\Route\Route;
use League\Route\Strategy\ApplicationStrategy;
use League\Route\Strategy\StrategyInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\Stream;

class PlatesStrategy extends ApplicationStrategy implements StrategyInterface
{
    use HasLayoutTrait;

    /** @var PlatesEngine $viewEngine */
    private $viewEngine;

    /** @var NotFoundDecorator $notFoundDecorator */
    private $notFoundDecorator;

    /** @var NotAllowedDecorator $notAllowedDecorator */
    private $notAllowedDecorator;

    /** @var ExceptionDecorator $exceptionDecorator */
    private $exceptionDecorator;

    /** @var bool  */
    private $i18nEnabled = false;

    /** @var array  */
    private $supportedLocales = [];

    /**
     * PlatesStrategy constructor.
     * @param PlatesEngine $viewEngine
     * @param NotFoundDecorator $notFound
     * @param NotAllowedDecorator $notAllowed
     * @param string $layout
     */
    public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator)
    {
        $this->viewEngine = $viewEngine;
        $this->notFoundDecorator = $notFound;
        $this->notAllowedDecorator = $notAllowed;
        $this->exceptionDecorator = $exceptionDecorator;
        $this->setLayout($layout);
    }

    /**
     * @param bool $i18nEnabled
     */
    public function setI18nEnabled(bool $i18nEnabled): void
    {
        $this->i18nEnabled = $i18nEnabled;
    }

    /**
     * @param array $locales
     */
    public function setSupportedLocales(array $locales): void
    {
        $this->supportedLocales = $locales;
    }


    /**
     * Invoke the route callable based on the strategy.
     *
     * @param \League\Route\Route $route
     * @param \Psr\Http\Message\ServerRequestInterface $request
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface
    {
        try {

            $response = parent::invokeRouteCallable($route, $request);
            if ($response instanceof JsonResponse) {
                return $response;
            }

            $body = ['content' => $response->getBody()->getContents()];
            $body = $this->viewEngine->render($this->layout, $body);

            return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode());

        } catch (Exception $e) {
            $body = $this->viewEngine->render('error/error', [
                'message' => $e->getMessage(),
                'code' => $e->getCode(),
                'trace' => $e->getTrace(),
            ]);
            $body = $this->viewEngine->render($this->layout, [
                'content' => $body,
            ]);
            $status = ($e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500;

            return $this->getResponseWithBodyAndStatus(new HtmlResponse($body), $body, $status);
        }

    }

    /**
     * @param ResponseInterface $response
     * @param string $body
     * @param int $status
     * @return \Psr\Http\Message\MessageInterface|Response
     */
    private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200)
    {
        $stream = new Stream('php://memory', 'r+');
        $stream->write($body);
        $response = $response->withStatus($status)->withBody($stream);

        return $response;
    }

    /**
     * Get a middleware that will decorate a NotFoundException
     *
     * @param \League\Route\Http\Exception\NotFoundException $exception
     *
     * @return \Psr\Http\Server\MiddlewareInterface
     */
    public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface
    {
        return $this->notFoundDecorator;
    }

    /**
     * Get a middleware that will decorate a NotAllowedException
     *
     * @param \League\Route\Http\Exception\NotFoundException $e
     *
     * @return \Psr\Http\Server\MiddlewareInterface
     */
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface
    {
        return $this->notAllowedDecorator;
    }

    /**
     * @return MiddlewareInterface
     */
    public function getExceptionHandler(): MiddlewareInterface
    {
        return $this->exceptionDecorator;
    }
}<commit_msg>Check content type rather than response class<commit_after><?php

namespace Bone\Mvc\Router;

use Bone\Mvc\Router\Decorator\ExceptionDecorator;
use Bone\Mvc\Router\Decorator\NotAllowedDecorator;
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
use Bone\Mvc\View\PlatesEngine;
use Bone\Traits\HasLayoutTrait;
use Exception;
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
use League\Route\Route;
use League\Route\Strategy\ApplicationStrategy;
use League\Route\Strategy\StrategyInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\Stream;

class PlatesStrategy extends ApplicationStrategy implements StrategyInterface
{
    use HasLayoutTrait;

    /** @var PlatesEngine $viewEngine */
    private $viewEngine;

    /** @var NotFoundDecorator $notFoundDecorator */
    private $notFoundDecorator;

    /** @var NotAllowedDecorator $notAllowedDecorator */
    private $notAllowedDecorator;

    /** @var ExceptionDecorator $exceptionDecorator */
    private $exceptionDecorator;

    /** @var bool  */
    private $i18nEnabled = false;

    /** @var array  */
    private $supportedLocales = [];

    /**
     * PlatesStrategy constructor.
     * @param PlatesEngine $viewEngine
     * @param NotFoundDecorator $notFound
     * @param NotAllowedDecorator $notAllowed
     * @param string $layout
     */
    public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator)
    {
        $this->viewEngine = $viewEngine;
        $this->notFoundDecorator = $notFound;
        $this->notAllowedDecorator = $notAllowed;
        $this->exceptionDecorator = $exceptionDecorator;
        $this->setLayout($layout);
    }

    /**
     * @param bool $i18nEnabled
     */
    public function setI18nEnabled(bool $i18nEnabled): void
    {
        $this->i18nEnabled = $i18nEnabled;
    }

    /**
     * @param array $locales
     */
    public function setSupportedLocales(array $locales): void
    {
        $this->supportedLocales = $locales;
    }


    /**
     * Invoke the route callable based on the strategy.
     *
     * @param \League\Route\Route $route
     * @param \Psr\Http\Message\ServerRequestInterface $request
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface
    {
        try {

            $response = parent::invokeRouteCallable($route, $request);

            if (strstr($response->getHeader('Content-Type')[0], 'application/json')) {
                return $response;
            }

            $body = ['content' => $response->getBody()->getContents()];
            $body = $this->viewEngine->render($this->layout, $body);

            return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode());

        } catch (Exception $e) {
            $body = $this->viewEngine->render('error/error', [
                'message' => $e->getMessage(),
                'code' => $e->getCode(),
                'trace' => $e->getTrace(),
            ]);
            $body = $this->viewEngine->render($this->layout, [
                'content' => $body,
            ]);
            $status = ($e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500;

            return $this->getResponseWithBodyAndStatus(new HtmlResponse($body), $body, $status);
        }

    }

    /**
     * @param ResponseInterface $response
     * @param string $body
     * @param int $status
     * @return \Psr\Http\Message\MessageInterface|Response
     */
    private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200)
    {
        $stream = new Stream('php://memory', 'r+');
        $stream->write($body);
        $response = $response->withStatus($status)->withBody($stream);

        return $response;
    }

    /**
     * Get a middleware that will decorate a NotFoundException
     *
     * @param \League\Route\Http\Exception\NotFoundException $exception
     *
     * @return \Psr\Http\Server\MiddlewareInterface
     */
    public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface
    {
        return $this->notFoundDecorator;
    }

    /**
     * Get a middleware that will decorate a NotAllowedException
     *
     * @param \League\Route\Http\Exception\NotFoundException $e
     *
     * @return \Psr\Http\Server\MiddlewareInterface
     */
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface
    {
        return $this->notAllowedDecorator;
    }

    /**
     * @return MiddlewareInterface
     */
    public function getExceptionHandler(): MiddlewareInterface
    {
        return $this->exceptionDecorator;
    }
}
{'fraction_non_alphanumeric': 0.1217174238642037, 'fraction_numerical': 0.0048926610084872695, 'mean_word_length': 2.888198757763975, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 80, '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': '4102794', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2870, 'n_words': 559}