<commit_before><?php

namespace Concrete\Core\Http;

use Concrete\Controller\Frontend\PageForbidden;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Controller\Controller;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Page\Collection\Collection;
use Concrete\Core\Page\Collection\Version\Version;
use Concrete\Core\Page\Controller\PageController;
use Concrete\Core\Page\Event;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Relation\Menu\Item\RelationListItem;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Permission\Key\Key;
use Concrete\Core\Routing\RedirectResponse;
use Concrete\Core\Routing\RouterInterface;
use Concrete\Core\User\User;
use Concrete\Core\View\View;
use Detection\MobileDetect;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Session\Session;

class ResponseFactory implements ResponseFactoryInterface, ApplicationAwareInterface
{

    use ApplicationAwareTrait;

    /**
     * @var \Symfony\Component\HttpFoundation\Session\Session
     */
    protected $session;

    /**
     * @var \Concrete\Core\Http\Request
     */
    protected $request;
    /**
     * @var \Concrete\Core\Http\Localization
     */
    private $localization;
    /**
     * @var \Concrete\Core\Config\Repository\Repository
     */
    private $config;

    public function __construct(Session $session, Request $request, Localization $localization, Repository $config)
    {
        $this->session = $session;
        $this->request = $request;
        $this->localization = $localization;
        $this->config = $config;
    }

    /**
     * @inheritdoc
     */
    public function create($content, $code = Response::HTTP_OK, array $headers = array())
    {
        return \Concrete\Core\Http\Response::create($content, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function json($data, $code = Response::HTTP_OK, array $headers = array())
    {
        return JsonResponse::create($data, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function notFound($content, $code = Response::HTTP_NOT_FOUND, $headers = array())
    {
        if (strcasecmp($this->request->server->get('HTTP_X_REQUESTED_WITH', ''), 'xmlhttprequest') === 0) {
            $loc = $this->localization;
            $changeContext = $this->shouldChangeContext();
            if ($changeContext) {
                $loc->pushActiveContext('site');
            }
            $responseData = [
                'error' => t('Page not found'),
                'errors' => [t('Page not found')],
            ];
            if ($changeContext) {
                $loc->popActiveContext();
            }

            return $this->json($responseData, $code, $headers);
        }

        $item = '/page_not_found';
        $c = Page::getByPath($item);

        if (is_object($c) && !$c->isError()) {
            return $this->collection($c, $code, $headers);
        }

        $cnt = $this->app->make(PageForbidden::class);
        return $this->controller($cnt, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function error($content, $code = Response::HTTP_INTERNAL_SERVER_ERROR, $headers = array())
    {
        return $this->create($content, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function forbidden($requestUrl, $code = Response::HTTP_FORBIDDEN, $headers = array())
    {
        // set page for redirection after successful login
        $this->session->set('rUri', $requestUrl);

        // load page forbidden
        $item = '/page_forbidden';
        $c = Page::getByPath($item);
        if (is_object($c) && !$c->isError()) {
            return $this->collection($c, $code, $headers);
        }

        $cnt = $this->app->make(PageForbidden::class);
        $this->controller($cnt, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function redirect($to, $code = Response::HTTP_MOVED_PERMANENTLY, $headers = array())
    {
        return RedirectResponse::create($to, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function view(View $view, $code = Response::HTTP_OK, $headers = array())
    {
        $changeContext = $this->shouldChangeContext();
        if ($changeContext) {
            $this->localization->pushActiveContext('site');
        }

        $contents = $view->render();
        if ($changeContext) {
            $this->localization->popActiveContext();
        }

        return $this->create($contents, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function controller(Controller $controller, $code = Response::HTTP_OK, $headers = array())
    {
        $request = $this->request;

        $controller->on_start();

        if ($controller instanceof PageController) {
            $controller->setupRequestActionAndParameters($request);

            $response = $controller->validateRequest();
            // If validaterequest returned a response
            if ($response instanceof \Symfony\Component\HttpFoundation\Response) {
                return $response;
            } else {
                // If validateRequest did not return true
                if ($response == false) {
                    return $this->notFound('', Response::HTTP_NOT_FOUND, $headers);
                }
            }

            $requestTask = $controller->getRequestAction();
            $requestParameters = $controller->getRequestActionParameters();
            $response = $controller->runAction($requestTask, $requestParameters);
            if ($response instanceof \Symfony\Component\HttpFoundation\Response) {
                return $response;
            }

        } else {
            $controller->runAction('view');
        }<commit_msg>Make it so that you can return responses from on_start<commit_after><?php

namespace Concrete\Core\Http;

use Concrete\Controller\Frontend\PageForbidden;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Controller\Controller;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Page\Collection\Collection;
use Concrete\Core\Page\Collection\Version\Version;
use Concrete\Core\Page\Controller\PageController;
use Concrete\Core\Page\Event;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Relation\Menu\Item\RelationListItem;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Permission\Key\Key;
use Concrete\Core\Routing\RedirectResponse;
use Concrete\Core\Routing\RouterInterface;
use Concrete\Core\User\User;
use Concrete\Core\View\View;
use Detection\MobileDetect;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Session\Session;

class ResponseFactory implements ResponseFactoryInterface, ApplicationAwareInterface
{

    use ApplicationAwareTrait;

    /**
     * @var \Symfony\Component\HttpFoundation\Session\Session
     */
    protected $session;

    /**
     * @var \Concrete\Core\Http\Request
     */
    protected $request;
    /**
     * @var \Concrete\Core\Http\Localization
     */
    private $localization;
    /**
     * @var \Concrete\Core\Config\Repository\Repository
     */
    private $config;

    public function __construct(Session $session, Request $request, Localization $localization, Repository $config)
    {
        $this->session = $session;
        $this->request = $request;
        $this->localization = $localization;
        $this->config = $config;
    }

    /**
     * @inheritdoc
     */
    public function create($content, $code = Response::HTTP_OK, array $headers = array())
    {
        return \Concrete\Core\Http\Response::create($content, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function json($data, $code = Response::HTTP_OK, array $headers = array())
    {
        return JsonResponse::create($data, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function notFound($content, $code = Response::HTTP_NOT_FOUND, $headers = array())
    {
        if (strcasecmp($this->request->server->get('HTTP_X_REQUESTED_WITH', ''), 'xmlhttprequest') === 0) {
            $loc = $this->localization;
            $changeContext = $this->shouldChangeContext();
            if ($changeContext) {
                $loc->pushActiveContext('site');
            }
            $responseData = [
                'error' => t('Page not found'),
                'errors' => [t('Page not found')],
            ];
            if ($changeContext) {
                $loc->popActiveContext();
            }

            return $this->json($responseData, $code, $headers);
        }

        $item = '/page_not_found';
        $c = Page::getByPath($item);

        if (is_object($c) && !$c->isError()) {
            return $this->collection($c, $code, $headers);
        }

        $cnt = $this->app->make(PageForbidden::class);
        return $this->controller($cnt, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function error($content, $code = Response::HTTP_INTERNAL_SERVER_ERROR, $headers = array())
    {
        return $this->create($content, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function forbidden($requestUrl, $code = Response::HTTP_FORBIDDEN, $headers = array())
    {
        // set page for redirection after successful login
        $this->session->set('rUri', $requestUrl);

        // load page forbidden
        $item = '/page_forbidden';
        $c = Page::getByPath($item);
        if (is_object($c) && !$c->isError()) {
            return $this->collection($c, $code, $headers);
        }

        $cnt = $this->app->make(PageForbidden::class);
        $this->controller($cnt, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function redirect($to, $code = Response::HTTP_MOVED_PERMANENTLY, $headers = array())
    {
        return RedirectResponse::create($to, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function view(View $view, $code = Response::HTTP_OK, $headers = array())
    {
        $changeContext = $this->shouldChangeContext();
        if ($changeContext) {
            $this->localization->pushActiveContext('site');
        }

        $contents = $view->render();
        if ($changeContext) {
            $this->localization->popActiveContext();
        }

        return $this->create($contents, $code, $headers);
    }

    /**
     * @inheritdoc
     */
    public function controller(Controller $controller, $code = Response::HTTP_OK, $headers = array())
    {
        $request = $this->request;

        if ($response = $controller->on_start()) {
            return $response;
        }

        if ($controller instanceof PageController) {
            $controller->setupRequestActionAndParameters($request);

            $response = $controller->validateRequest();
            // If validaterequest returned a response
            if ($response instanceof \Symfony\Component\HttpFoundation\Response) {
                return $response;
            } else {
                // If validateRequest did not return true
                if ($response == false) {
                    return $this->notFound('', Response::HTTP_NOT_FOUND, $headers);
                }
            }

            $requestTask = $controller->getRequestAction();
            $requestParameters = $controller->getRequestActionParameters();
            $response = $controller->runAction($requestTask, $requestParameters);
            if ($response instanceof \Symfony\Component\HttpFoundation\Response) {
                return $response;
            }

        } else {
            $controller->runAction('view');
        }
{'fraction_non_alphanumeric': 0.13942348096478696, 'fraction_numerical': 0.00016808135137406504, 'mean_word_length': 2.478515054077755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '350530', 'n_tokens_mistral': 3334, 'n_tokens_neox': 3260, 'n_words': 625}