<commit_before><?php
/*
 * This file is part of the Sonata project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\PageBundle\CmsManager;

use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Templating\StreamingEngineInterface;
use Symfony\Component\HttpFoundation\Request;
use Sonata\SeoBundle\Seo\SeoPageInterface;

/**
 * Render a PageInterface
 *
 * @author <NAME> <<EMAIL>>
 */
class PageRenderer implements PageRendererInterface
{
    protected $router;

    protected $cmsSelector;

    protected $templating;

    protected $templates = array();

    protected $defaultTemplateCode = 'default';

    protected $defaultTemplatePath = 'SonataPageBundle::layout.html.twig';

    protected $errorCodes;

    protected $seoPage;

    /**
     * @param RouterInterface             $router
     * @param CmsManagerSelectorInterface $cmsSelector
     * @param EngineInterface             $templating
     * @param array                       $templates
     * @param array                       $errorCodes
     * @param SeoPageInterface            $seoPage
     */
    public function __construct(RouterInterface $router, CmsManagerSelectorInterface $cmsSelector, EngineInterface $templating, array $templates, array $errorCodes, SeoPageInterface $seoPage = null)
    {
        $this->router      = $router;
        $this->cmsSelector = $cmsSelector;
        $this->templating  = $templating;
        $this->templates   = $templates;
        $this->errorCodes  = $errorCodes;
        $this->seoPage     = $seoPage;
    }

    /**
     * {@inheritdoc}
     */
    public function render(PageInterface $page, array $params = array(), Response $response = null)
    {
        $cms = $this->cmsSelector->retrieve();

        if (!$response) {

            if ($page->getTarget()) {
                $page->addHeader('Location', sprintf('%s%s', $this->router->getContext()->getBaseUrl(), $page->getTarget()->getUrl()));

                return new Response('', 302, $page->getHeaders());
            }

            if ($page->getHeaders()) {
                $response = new Response('', 200, $page->getHeaders());
            }
        }

        $template = false;
        if ($cms->getCurrentPage()) {
            $template = $this->getTemplate($cms->getCurrentPage()->getTemplateCode())->getPath();
        }

        if (!$template) {
            $template = $this->defaultTemplatePath;
        }

        $params['page']        = $page;
        $params['site']        = $page->getSite();
        $params['manager']     = $cms;
        $params['error_codes'] = $this->errorCodes;

        if ($this->templating instanceof StreamingEngineInterface) {
            $templating = $this->templating;

            return new StreamedResponse(
                function() use ($templating, $template, &$params) { $templating->stream($template, $params); },
                $response ? $response->getStatusCode() : 200,
                $response ? $response->headers->all() : array()
            );
        }

        $this->addSeoMeta($page);

        $response = $this->templating->renderResponse($template, $params, $response);

        if (!$this->cmsSelector->isEditor() && $page->isCms()) {
            $response->setTtl($page->getTtl());
        }

        return $response;
    }
<commit_msg>Fix SEO ordering<commit_after><?php
/*
 * This file is part of the Sonata project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\PageBundle\CmsManager;

use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Templating\StreamingEngineInterface;
use Symfony\Component\HttpFoundation\Request;
use Sonata\SeoBundle\Seo\SeoPageInterface;

/**
 * Render a PageInterface
 *
 * @author <NAME> <<EMAIL>>
 */
class PageRenderer implements PageRendererInterface
{
    protected $router;

    protected $cmsSelector;

    protected $templating;

    protected $templates = array();

    protected $defaultTemplateCode = 'default';

    protected $defaultTemplatePath = 'SonataPageBundle::layout.html.twig';

    protected $errorCodes;

    protected $seoPage;

    /**
     * @param RouterInterface             $router
     * @param CmsManagerSelectorInterface $cmsSelector
     * @param EngineInterface             $templating
     * @param array                       $templates
     * @param array                       $errorCodes
     * @param SeoPageInterface            $seoPage
     */
    public function __construct(RouterInterface $router, CmsManagerSelectorInterface $cmsSelector, EngineInterface $templating, array $templates, array $errorCodes, SeoPageInterface $seoPage = null)
    {
        $this->router      = $router;
        $this->cmsSelector = $cmsSelector;
        $this->templating  = $templating;
        $this->templates   = $templates;
        $this->errorCodes  = $errorCodes;
        $this->seoPage     = $seoPage;
    }

    /**
     * {@inheritdoc}
     */
    public function render(PageInterface $page, array $params = array(), Response $response = null)
    {
        $cms = $this->cmsSelector->retrieve();

        if (!$response) {

            if ($page->getTarget()) {
                $page->addHeader('Location', sprintf('%s%s', $this->router->getContext()->getBaseUrl(), $page->getTarget()->getUrl()));

                return new Response('', 302, $page->getHeaders());
            }

            if ($page->getHeaders()) {
                $response = new Response('', 200, $page->getHeaders());
            }
        }

        $template = false;
        if ($cms->getCurrentPage()) {
            $template = $this->getTemplate($cms->getCurrentPage()->getTemplateCode())->getPath();
        }

        if (!$template) {
            $template = $this->defaultTemplatePath;
        }

        $params['page']        = $page;
        $params['site']        = $page->getSite();
        $params['manager']     = $cms;
        $params['error_codes'] = $this->errorCodes;

        $this->addSeoMeta($page);

        if ($this->templating instanceof StreamingEngineInterface) {
            $templating = $this->templating;

            return new StreamedResponse(
                function() use ($templating, $template, &$params) { $templating->stream($template, $params); },
                $response ? $response->getStatusCode() : 200,
                $response ? $response->headers->all() : array()
            );
        }

        $response = $this->templating->renderResponse($template, $params, $response);

        if (!$this->cmsSelector->isEditor() && $page->isCms()) {
            $response->setTtl($page->getTtl());
        }

        return $response;
    }

{'fraction_non_alphanumeric': 0.13498585477569716, 'fraction_numerical': 0.002424895594773003, 'mean_word_length': 2.5743861338468945, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 95, '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': '5256722', 'n_tokens_mistral': 2095, 'n_tokens_neox': 2020, 'n_words': 420}