<commit_before><?php

namespace Raindrop\PageBundle\Renderer;

use Raindrop\PageBundle\Renderer\RendererInterface;
use Raindrop\RoutingBundle\Entity\Route;
use Raindrop\PageBundle\Entity\Page;
use Symfony\Component\HttpFoundation\Response;
use Raindrop\PageBundle\Renderer\RenderableObjectInterface;

class PageRenderer implements RendererInterface
{
    protected $page, $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    /**
     * Renders a page using the same logic as the base symfony 2 controller.
     *
     * @param  \Raindrop\PageBundle\Renderer\RenderableObjectInterface $page
     * @return type
     */
    public function render(RenderableObjectInterface $object)
    {
        return $this->container
            ->get('templating')
            ->renderResponse(
                $this->getLayout($object),
                $this->getParameters($object),
                $this->getBaseResponse($object, $object->getExpiresAfter())
            )
            ;
    }

    protected function getLayout($object)
    {
        if ($object instanceof Page) {
            return $object->getLayout();
        }

        return null;
    }

    protected function getParameters($object)
    {
        return $object->getParameters();
    }

    public function setPage($page)
    {
        $this->page = $page;

        return $this;
    }

    public function getBaseResponse($object, $expiresAfter = 86400)
    {
        $response = new Response;

        if ($this->container->getParameter('kernel.environment') !== 'prod') {
            return $response;
        }

        $response->setPublic();
        $lastModified = $object->getLastModified();

        // if last modified is a timestamp, convert to \DateTime objects
        if (!$lastModified instanceof \DateTime) {
            $date = new \DateTime();
            $date->setTimestamp($lastModified);
            $lastModified = $date;
        }

        $response->setLastModified($lastModified);
        $response->headers->set('Expires', gmdate("D, d M Y H:i:s", time() + $expiresAfter) . " GMT");

        return $response;
    }

    protected function getAssetsUrlSuffix()
    {
        $release_version = '';

        if ($this->container->hasParameter('release_version')) {
            $release_version = '?' . $this->container->getParameter('release_version');
        }

        return $release_version;
    }

    public function renderJavascript()
    {
        $blocks = $this->getPageBlocks();

        if (!count($blocks)) {
            return;
        }

        $requirements = array();
        $inclusion = array();

        foreach ($blocks as $block) {
            if ($this->blockInUse($block)) {
                if ($block->hasJavascripts()) {
                    foreach ($block->getJavascripts() as $js) {
                        if (!isset($requirements[$js])) {
                            $requirements [$js]= true;
                            $inclusion []= $js;
                        }
                    }
                }
            }
        }

        if (empty($requirements)) {
            return '';
        }

        $path = $this->container->get('router')->generate('raindrop_combined_assets', array(
            'type' => 'js',
            'assets' => implode(",", $inclusion)
        ));

        return '<script src="'. $path . $this->getAssetsUrlSuffix() .'" type="text/javascript"></script>';
    }

    public function renderStylesheet()
    {
        $blocks = $this->getPageBlocks();

        if (empty($blocks)) {
            return '';
        }

        $requirements = array();
        $inclusion = array();

        foreach ($blocks as $block) {
            if ($this->blockInUse($block)) {
                if ($block->hasStylesheets()) {
                    foreach ($block->getStylesheets() as $css) {
                        if (!isset($requirements[$css])) {
                            $requirements [$css]= true;
                            $inclusion []= $css;
                        }
                    }
                }
            }
        }

        if (empty($requirements)) {
            return '';
        }

        $path = $this->container->get('router')->generate('raindrop_combined_assets', array(
            'type' => 'css',
            'assets' => implode(",", $inclusion)
        ));

        return '<link rel="stylesheet" type="text/css" href="'. $path . $this->getAssetsUrlSuffix() .'" />';
    }

    protected function blockInUse($block)
    {
        $useLiipTheme = $this->container->getParameter('use_liip_theme');
        if ($useLiipTheme) {
            $defaultTheme = 'desktop';
            $theme = $this->container->get('liip_theme.active_theme')->getName();

            /**
             * If theme is null and block has no reference to theme, it's ok.
             * If theme is not null and block refers to that theme, it's ok.
             */
            if (is_null($theme) or $theme == $defaultTheme) {<commit_msg>add children blocks configuration to combined css/js<commit_after><?php

namespace Raindrop\PageBundle\Renderer;

use Raindrop\PageBundle\Renderer\RendererInterface;
use Raindrop\RoutingBundle\Entity\Route;
use Raindrop\PageBundle\Entity\Page;
use Symfony\Component\HttpFoundation\Response;
use Raindrop\PageBundle\Renderer\RenderableObjectInterface;

class PageRenderer implements RendererInterface
{
    protected $page, $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    /**
     * Renders a page using the same logic as the base symfony 2 controller.
     *
     * @param  \Raindrop\PageBundle\Renderer\RenderableObjectInterface $page
     * @return type
     */
    public function render(RenderableObjectInterface $object)
    {
        return $this->container
            ->get('templating')
            ->renderResponse(
                $this->getLayout($object),
                $this->getParameters($object),
                $this->getBaseResponse($object, $object->getExpiresAfter())
            )
            ;
    }

    protected function getLayout($object)
    {
        if ($object instanceof Page) {
            return $object->getLayout();
        }

        return null;
    }

    protected function getParameters($object)
    {
        return $object->getParameters();
    }

    public function setPage($page)
    {
        $this->page = $page;

        return $this;
    }

    public function getBaseResponse($object, $expiresAfter = 86400)
    {
        $response = new Response;

        if ($this->container->getParameter('kernel.environment') !== 'prod') {
            return $response;
        }

        $response->setPublic();
        $lastModified = $object->getLastModified();

        // if last modified is a timestamp, convert to \DateTime objects
        if (!$lastModified instanceof \DateTime) {
            $date = new \DateTime();
            $date->setTimestamp($lastModified);
            $lastModified = $date;
        }

        $response->setLastModified($lastModified);
        $response->headers->set('Expires', gmdate("D, d M Y H:i:s", time() + $expiresAfter) . " GMT");

        return $response;
    }

    protected function getAssetsUrlSuffix()
    {
        $release_version = '';

        if ($this->container->hasParameter('release_version')) {
            $release_version = '?' . $this->container->getParameter('release_version');
        }

        return $release_version;
    }

    public function renderJavascript()
    {
        $blocks = $this->getPageBlocks();

        if (!count($blocks)) {
            return;
        }

        $requirements = array();
        $inclusion = array();

        foreach ($blocks as $block) {
            if ($this->blockInUse($block)) {
                if ($block->hasJavascripts()) {
                    foreach ($block->getJavascripts() as $js) {
                        if (!isset($requirements[$js])) {
                            $requirements [$js]= true;
                            $inclusion []= $js;
                        }
                    }
                }

                // add also first level of nested blocks
                if ($block->hasChildren()) {
                    foreach ($block->getChildren() as $children_block) {
                        if ($this->blockInUse($children_block)) {
                            if ($children_block->hasJavascripts()) {
                                foreach ($children_block->getJavascripts() as $js) {
                                    if (!isset($requirements[$js])) {
                                        $requirements [$js]= true;
                                        $inclusion []= $js;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (empty($requirements)) {
            return '';
        }

        $path = $this->container->get('router')->generate('raindrop_combined_assets', array(
            'type' => 'js',
            'assets' => implode(",", $inclusion)
        ));

        return '<script src="'. $path . $this->getAssetsUrlSuffix() .'" type="text/javascript"></script>';
    }

    public function renderStylesheet()
    {
        $blocks = $this->getPageBlocks();

        if (empty($blocks)) {
            return '';
        }

        $requirements = array();
        $inclusion = array();

        foreach ($blocks as $block) {
            if ($this->blockInUse($block)) {
                if ($block->hasStylesheets()) {
                    foreach ($block->getStylesheets() as $css) {
                        if (!isset($requirements[$css])) {
                            $requirements [$css]= true;
                            $inclusion []= $css;
                        }
                    }
                }


                // add also first level of nested blocks
                if ($block->hasChildren()) {
                    foreach ($block->getChildren() as $children_block) {
                        if ($this->blockInUse($children_block)) {
                            if ($children_block->hasStylesheets()) {
                                foreach ($children_block->getStylesheets() as $css) {
                                    if (!isset($requirements[$css])) {
                                        $requirements [$css]= true;
                                        $inclusion []= $css;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (empty($requirements)) {
            return '';
        }

        $path = $this->container->get('router')->generate('raindrop_combined_assets', array(
            'type' => 'css',
            'assets' => implode(",", $inclusion)
        ));

        return '<link rel="stylesheet" type="text/css" href="'. $path . $this->getAssetsUrlSuffix() .'" />';
    }

    protected function blockInUse($block)
    {
        $useLiipTheme = $this->container->getParameter('use_liip_theme');
        if ($useLiipTheme) {
            $defaultTheme = 'desktop';
            $theme = $this->container->get('liip_theme.active_theme')->getName();

            /**
             * If theme is null and block has no reference to theme, it's ok.
             * If theme is not null and block refers to that theme, it's ok.
             */
            if (is_null($theme) or $theme == $defaultTheme) {
{'fraction_non_alphanumeric': 0.13108710562414266, 'fraction_numerical': 0.00102880658436214, 'mean_word_length': 1.5688174410922704, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '780959', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2898, 'n_words': 612}