<commit_before><?php
/**
 * phlexible
 *
 * @copyright 2007-2013 brainbits GmbH (http://www.brainbits.net)
 * @license   proprietary
 */

namespace Phlexible\Bundle\IndexerElementBundle\Indexer\ContentRenderer;

use Phlexible\Bundle\ElementRendererBundle\Configurator\Configuration;
use Phlexible\Bundle\ElementRendererBundle\Configurator\ConfiguratorInterface;
use Phlexible\Bundle\IndexerElementBundle\Indexer\DocumentDescriptor;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Templating\EngineInterface;

/**
 * Content renderer
 *
 * @author <NAME> <<EMAIL>>
 */
class ContentRenderer implements ContentRendererInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * @var RequestContext
     */
    private $requestContext;

    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @var ConfiguratorInterface
     */
    private $configurator;

    /**
     * @var EngineInterface
     */
    private $templating;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param ContainerInterface    $container
     * @param RequestContext        $requestContext
     * @param RequestStack          $requestStack
     * @param ConfiguratorInterface $configurator
     * @param EngineInterface       $templating
     * @param LoggerInterface       $logger
     */
    public function __construct(
        ContainerInterface $container,
        RequestContext $requestContext,
        RequestStack $requestStack,
        ConfiguratorInterface $configurator,
        EngineInterface $templating,
        LoggerInterface $logger
    ) {
        $this->container = $container;
        $this->requestContext = $requestContext;
        $this->requestStack = $requestStack;
        $this->configurator = $configurator;
        $this->templating = $templating;
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function render(DocumentDescriptor $descriptor)
    {
        $node = $descriptor->getNode();
        $siteroot = $descriptor->getSiteroot();
        $language = $descriptor->getLanguage();
        $parameters = $this->createParameters($descriptor);

        try {
            ob_start();

            $request = new Request();
            $this->requestStack->push($request);

            $this->container->enterScope('request');
            $this->container->set('request', $request, 'request');

            $siterootUrl = $siteroot->getDefaultUrl();

            foreach ($parameters as $key => $value) {
                $this->requestContext->setParameter($key, $value);
                $request->attributes->set($key, $value);
            }
            $request->setLocale($language);
            $request->attributes->set('routeDocument', $node);
            $request->attributes->set('contentDocument', $node);
            $request->attributes->set('siterootUrl', $siterootUrl);

            try {
                /* @var $configuration Configuration */
                $configuration = $this->configurator->configure($request, null);
                $data = $configuration->getVariables();

                $content = $this->templating->render($data['template'], (array) $data);
            } catch (\Exception $e) {
                $this->logger->error("Error while rendering node {$node->getId()}: {$e->getMessage()}");<commit_msg>add server vars to request, to enable host name matching<commit_after><?php
/**
 * phlexible
 *
 * @copyright 2007-2013 brainbits GmbH (http://www.brainbits.net)
 * @license   proprietary
 */

namespace Phlexible\Bundle\IndexerElementBundle\Indexer\ContentRenderer;

use Phlexible\Bundle\ElementRendererBundle\Configurator\Configuration;
use Phlexible\Bundle\ElementRendererBundle\Configurator\ConfiguratorInterface;
use Phlexible\Bundle\IndexerElementBundle\Indexer\DocumentDescriptor;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Templating\EngineInterface;

/**
 * Content renderer
 *
 * @author <NAME> <<EMAIL>.net>
 */
class ContentRenderer implements ContentRendererInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    /**
     * @var RequestContext
     */
    private $requestContext;

    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @var ConfiguratorInterface
     */
    private $configurator;

    /**
     * @var EngineInterface
     */
    private $templating;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param ContainerInterface    $container
     * @param RequestContext        $requestContext
     * @param RequestStack          $requestStack
     * @param ConfiguratorInterface $configurator
     * @param EngineInterface       $templating
     * @param LoggerInterface       $logger
     */
    public function __construct(
        ContainerInterface $container,
        RequestContext $requestContext,
        RequestStack $requestStack,
        ConfiguratorInterface $configurator,
        EngineInterface $templating,
        LoggerInterface $logger
    ) {
        $this->container = $container;
        $this->requestContext = $requestContext;
        $this->requestStack = $requestStack;
        $this->configurator = $configurator;
        $this->templating = $templating;
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function render(DocumentDescriptor $descriptor)
    {
        $node = $descriptor->getNode();
        $siteroot = $descriptor->getSiteroot();
        $language = $descriptor->getLanguage();
        $parameters = $this->createParameters($descriptor);

        try {
            ob_start();

            $request = new Request(
                array(),
                array(),
                array(),
                array(),
                array(),
                array(
                    'SERVER_NAME' => $siteroot->getDefaultUrl()->getHostname(),
                )
            );
            $this->requestStack->push($request);

            $this->container->enterScope('request');
            $this->container->set('request', $request, 'request');

            $siterootUrl = $siteroot->getDefaultUrl();

            foreach ($parameters as $key => $value) {
                $this->requestContext->setParameter($key, $value);
                $request->attributes->set($key, $value);
            }
            $request->setLocale($language);
            $request->attributes->set('routeDocument', $node);
            $request->attributes->set('contentDocument', $node);
            $request->attributes->set('siterootUrl', $siterootUrl);

            try {
                /* @var $configuration Configuration */
                $configuration = $this->configurator->configure($request, null);
                $data = $configuration->getVariables();

                $content = $this->templating->render($data['template'], (array) $data);
            } catch (\Exception $e) {
                $this->logger->error("Error while rendering node {$node->getId()}: {$e->getMessage()}");
{'fraction_non_alphanumeric': 0.12703713598717606, 'fraction_numerical': 0.0021373230029388193, 'mean_word_length': 2.479089219330855, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7547172', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1923, 'n_words': 356}