<commit_before><?php

namespace Opifer\ContentBundle\Twig;

use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;

use Opifer\ContentBundle\Model\ContentManager;
use Opifer\ContentBundle\Model\ContentInterface;

class ContentExtension extends \Twig_Extension
{
    /** @var \Twig_Environment */
    protected $twig;

    /** @var FragmentHandler */
    protected $fragmentHandler;

    /** @var ContentManager */
    protected $contentManager;

    /**
     * Constructor
     *
     * @param Twig_Environment $twig
     * @param FragmentHandler  $fragmentHandler
     * @param ContentManager   $contentManager
     */
    public function __construct(\Twig_Environment $twig, FragmentHandler $fragmentHandler, ContentManager $contentManager)
    {
        $this->twig = $twig;
        $this->fragmentHandler = $fragmentHandler;
        $this->contentManager = $contentManager;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('placeholder', [$this, 'getPlaceholder'], array(
                'is_safe' => array('html'),
                'needs_context' => true
            )),
            new \Twig_SimpleFunction('get_content', [$this, 'getContent'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('get_content_by_id', [$this, 'getContentById'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('render_content', [$this, 'renderContent'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('nested_content', [$this, 'renderNestedContent'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('breadcrumbs', [$this, 'getBreadcrumbs'], [
                'is_safe' => array('html')
            ]),
        ];
    }

    /**
     * Get a content item by its slug
     *
     * @return \Opifer\CmsBundle\Entity\Content
     */
    public function getContent($slug)
    {
        $content = $this->contentManager->getRepository()
            ->findOneBySlug($slug);

        return $content;
    }
    
    /**
     * Get a content item by its id
     *
     * @return \Opifer\CmsBundle\Entity\Content
     */
    public function getContentById($id)
    {
        $content = $this->contentManager->getRepository()
            ->findOneById($id);

        return $content;
    }

    /**
     * Render a content item by its slug
     *
     * @return string
     */
    public function renderContent($slug)
    {
        $content = $this->getContent($slug);

        $action = new ControllerReference('OpiferContentBundle:Frontend/Content:view', ['content' => $content]);
        $string = $this->fragmentHandler->render($action);

        return $string;
    }

    /**
     * Render nested content
     *
     * @param ArrayCollection $values
     *
     * @return string
     */
    public function renderNestedContent($values)
    {
        $content = '';
        foreach ($values as $value) {
            $action = new ControllerReference('OpiferContentBundle:Frontend/Content:nested', ['id' => $value]);
            $content .= $this->fragmentHandler->render($action);
        }

        return $content;
    }

    /**
     * Get the view for the placeholder
     *
     * @param array  $context Passed automatically, when needs_context is set to TRUE
     * @param string $key
     *
     * @return string
     */
    public function getPlaceholder($context, $key)
    {
        if (!array_key_exists('layout', $context)) {<commit_msg>Added content rendering twig functions<commit_after><?php

namespace Opifer\ContentBundle\Twig;

use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;

use Opifer\ContentBundle\Model\ContentManager;
use Opifer\ContentBundle\Model\ContentInterface;

class ContentExtension extends \Twig_Extension
{
    /** @var \Twig_Environment */
    protected $twig;

    /** @var FragmentHandler */
    protected $fragmentHandler;

    /** @var ContentManager */
    protected $contentManager;

    /**
     * Constructor
     *
     * @param Twig_Environment $twig
     * @param FragmentHandler  $fragmentHandler
     * @param ContentManager   $contentManager
     */
    public function __construct(\Twig_Environment $twig, FragmentHandler $fragmentHandler, ContentManager $contentManager)
    {
        $this->twig = $twig;
        $this->fragmentHandler = $fragmentHandler;
        $this->contentManager = $contentManager;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('placeholder', [$this, 'getPlaceholder'], array(
                'is_safe' => array('html'),
                'needs_context' => true
            )),
            new \Twig_SimpleFunction('get_content', [$this, 'getContent'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('get_content_by_id', [$this, 'getContentById'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('render_content', [$this, 'renderContent'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('render_content_by_id', [$this, 'renderContentById'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('content_picker', [$this, 'contentPicker'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('nested_content', [$this, 'renderNestedContent'], [
                'is_safe' => array('html')
            ]),
            new \Twig_SimpleFunction('breadcrumbs', [$this, 'getBreadcrumbs'], [
                'is_safe' => array('html')
            ]),
        ];
    }

    /**
     * Get a content item by its slug
     *
     * @return \Opifer\CmsBundle\Entity\Content
     */
    public function getContent($slug)
    {
        $content = $this->contentManager->getRepository()
            ->findOneBySlug($slug);

        return $content;
    }
    
    /**
     * Get a content item by its id
     *
     * @return \Opifer\CmsBundle\Entity\Content
     */
    public function getContentById($id)
    {
        $content = $this->contentManager->getRepository()
            ->findOneById($id);

        return $content;
    }

    /**
     * Render a content item by its slug or passed content object
     *
     * @return string
     */
    public function renderContent($contentItem)
    {
        $content = ($contentItem instanceof ContentInterface) ? $contentItem : $this->getContent($contentItem);

        $action = new ControllerReference('OpiferContentBundle:Frontend/Content:view', ['content' => $content]);
        $string = $this->fragmentHandler->render($action);

        return $string;
    }
    
    /**
     * Render a content item by its slug
     *
     * @return string
     */
    public function renderContentById($id)
    {
        $content = $this->getContentById($id);

        $action = new ControllerReference('OpiferContentBundle:Frontend/Content:view', ['content' => $content]);
        $string = $this->fragmentHandler->render($action);

        return $string;
    }
    
    /**
     * Render a content picker item or slug content
     *
     * @return string
     */
    public function contentPicker($contentValue, $default)
    {
        if($contentValue && $contentValue->getValue()) {
            $contentItem = $this->getContentById($contentValue->getValue());
        } else {
            $contentItem = $default;
        }
        
        return $this->renderContent($contentItem);
    }

    /**
     * Render nested content
     *
     * @param ArrayCollection $values
     *
     * @return string
     */
    public function renderNestedContent($values)
    {
        $content = '';
        foreach ($values as $value) {
            $action = new ControllerReference('OpiferContentBundle:Frontend/Content:nested', ['id' => $value]);
            $content .= $this->fragmentHandler->render($action);
        }

        return $content;
    }

    /**
     * Get the view for the placeholder
     *
     * @param array  $context Passed automatically, when needs_context is set to TRUE
     * @param string $key
     *
     * @return string
     */
    public function getPlaceholder($context, $key)
    {
        if (!array_key_exists('layout', $context)) {
{'fraction_non_alphanumeric': 0.13022284122562675, 'fraction_numerical': 0.0, 'mean_word_length': 2.2333958724202625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '4593013', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2341, 'n_words': 482}