<commit_before><?php
namespace Hostnet\HnDependencyInjectionPlugin;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;

/**
 * Does a fallback to Symfony 1 if there was no route found in Symfony 2
 *
 * @author <NAME> <<EMAIL>>
 */
class Symfony1Fallback
{
    /**
     * @var Symfony1Kernel
     */
    private $kernel;

    /**
     * @var bool
     */
    private $first_controller_is_sf1 = null;

    /**
     * @param Symfony1Kernel $kernel
     */
    public function __construct(Symfony1Kernel $kernel)
    {
        $this->kernel = $kernel;
    }

    /**
     * Keeps track of whether symfony1 has been initialized already
     *
     * This can be used to track back on whether to handle the 404 here or in sf1
     *
     * @param FilterControllerEvent $event
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        $controller = $event->getController();

        // we just want to know the first call
        if (null !== $this->first_controller_is_sf1) {
            return;
        }

        if (is_array($controller) && $event->getController()[0] === $this) {
            $this->first_controller_is_sf1 = true;
        }

        $this->first_controller_is_sf1 = false;
    }

    /**
     * To be able to create routes to Symfony 1 from Symfony 2,
     * you can create a Symfony 2 routing rule like this:
     * sf1:
     *     path: /{module}/{action}/{params}
     *     defaults: { action: index, params: '', _controller: "kernel.listener.symfony1_fallback:fallbackToSymfony1" }
     *     requirements:
     *         params: "[a-zA-Z0-9_\/]+"
     * @return Response
     */
    public function fallbackToSymfony1()
    {
        $configuration = $this->kernel->getConfiguration();
        $context       = \sfContext::createInstance($configuration);
        try {
            $context->dispatch();
        } catch(\sfError404Exception $e) {
            // the page was actually not found in sf1
            return;
        } catch(\sfStopException $e) {
        }

        $code = 0;
        $response = new Response();

        if ($context->getResponse() instanceof \sfWebResponse) {
            $web_response = $context->getResponse();
            /* @var $web_response \sfWebResponse */
            $code = $web_response->getStatusCode();

            // If we're trying to redirect to another location, set the statuscode and header in the symfony2 response
            // properly, because this response is overwriting the sf1 response if the content of the body is less than
            // 4kb large due to output buffering.
            if (($code === 302 || $code === 304)) {
                $response->setStatusCode($code, Response::$statusTexts[$code]);
                $response->headers->set('Location', $web_response->getHttpHeader('Location'));
            }
        }

        // Symfony1 will usually send headers for us
        //Check if found response code is a known SF2 response code
        if (!isset(Response::$statusTexts[$code])) {
            // But for some reason it appears not to be, lets keep Symfony2
            // busy with an empty response :p
            // For some ajax requests it doesn't, dunno why, but thats why the
            // 200 status code.
            $code = 200;
        }

        $response->headers->set('X-Status-Code', $code);
        return $response;
    }

    /**
     * Fires on the kernel.exception event
     *
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if (!$event->getException() instanceof NotFoundHttpException) {
            return;
        }

        // Unique case here; If symfony2 has been initialized properly,
        // that means a 404 exception and no sf1 matching route via "sf1",
        // it should not try to go into sf1 because we explicitly threw
        // a 404 exception in our controller (or code).
        if (null !== $this->first_controller_is_sf1) {
            return;
        }

        // check if symfony1 doesn't want to handle the 404, need to continue here otherwise<commit_msg>Simplified the logic<commit_after><?php
namespace Hostnet\HnDependencyInjectionPlugin;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;

/**
 * Does a fallback to Symfony 1 if there was no route found in Symfony 2
 *
 * @author <NAME> <<EMAIL>>
 */
class Symfony1Fallback
{
    /**
     * @var Symfony1Kernel
     */
    private $kernel;

    /**
     * @var bool
     */
    private $fallback_on_404 = true;

    /**
     * @param Symfony1Kernel $kernel
     */
    public function __construct(Symfony1Kernel $kernel)
    {
        $this->kernel = $kernel;
    }

    /**
     * When a controller has been called, it shouldn't
     * fall back to Symfony1 on kernel exceptions
     *
     * @param FilterControllerEvent $event
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        $this->fallback_on_404 = false;
    }

    /**
     * To be able to create routes to Symfony 1 from Symfony 2,
     * you can create a Symfony 2 routing rule like this:
     * sf1:
     *     path: /{module}/{action}/{params}
     *     defaults: { action: index, params: '', _controller: "kernel.listener.symfony1_fallback:fallbackToSymfony1" }
     *     requirements:
     *         params: "[a-zA-Z0-9_\/]+"
     * @return Response
     */
    public function fallbackToSymfony1()
    {
        $configuration = $this->kernel->getConfiguration();
        $context       = \sfContext::createInstance($configuration);
        try {
            $context->dispatch();
        } catch(\sfError404Exception $e) {
            // the page was actually not found in sf1
            return;
        } catch(\sfStopException $e) {
        }

        $code = 0;
        $response = new Response();

        if ($context->getResponse() instanceof \sfWebResponse) {
            $web_response = $context->getResponse();
            /* @var $web_response \sfWebResponse */
            $code = $web_response->getStatusCode();

            // If we're trying to redirect to another location, set the statuscode and header in the symfony2 response
            // properly, because this response is overwriting the sf1 response if the content of the body is less than
            // 4kb large due to output buffering.
            if (($code === 302 || $code === 304)) {
                $response->setStatusCode($code, Response::$statusTexts[$code]);
                $response->headers->set('Location', $web_response->getHttpHeader('Location'));
            }
        }

        // Symfony1 will usually send headers for us
        //Check if found response code is a known SF2 response code
        if (!isset(Response::$statusTexts[$code])) {
            // But for some reason it appears not to be, lets keep Symfony2
            // busy with an empty response :p
            // For some ajax requests it doesn't, dunno why, but thats why the
            // 200 status code.
            $code = 200;
        }

        $response->headers->set('X-Status-Code', $code);
        return $response;
    }

    /**
     * Fires on the kernel.exception event
     *
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if (!$event->getException() instanceof NotFoundHttpException) {
            return;
        }

        // Unique case here; If symfony2 has been initialized properly,
        // it should not try to go into sf1 because we explicitly threw
        // a 404 exception in our controller (or code).
        // A proper initialization means that it didn't match a "sf1"
        // route and didn't 404
        if (false === $this->fallback_on_404) {
            return;
        }

        // check if symfony1 doesn't want to handle the 404, need to continue here otherwise
{'fraction_non_alphanumeric': 0.10419925400072194, 'fraction_numerical': 0.014919985561304295, 'mean_word_length': 2.201848998459168, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, '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': '4821953', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2209, 'n_words': 698}