<commit_before><?php

namespace Symfony\Bundle\TwigBundle\Extension;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\FieldGroupInterface;
use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\CollectionField;
use Symfony\Bundle\TwigBundle\TokenParser\FormThemeTokenParser;
use Symfony\Bundle\FrameworkBundle\Templating\HtmlGeneratorInterface;

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

/**
 *
 * @author <NAME> <<EMAIL>>
 */
class FormExtension extends \Twig_Extension
{
    static protected $cache = array();

    protected $resources;
    protected $templates;
    protected $environment;
    protected $themes;
    protected $generator;

    public function __construct(HtmlGeneratorInterface $generator, array $resources = array())
    {
        $this->generator = $generator;
        $this->themes = new \SplObjectStorage();
        $this->resources = array_merge(array(
            'TwigBundle::form.twig',
            'TwigBundle::widgets.twig',
        ), $resources);
    }

    /**
     * {@inheritdoc}
     */
    public function initRuntime(\Twig_Environment $environment)
    {
        $this->environment = $environment;
    }

    public function setTheme(FieldGroupInterface $group, array $resources)
    {
        $this->themes->attach($group, $resources);
    }

    /**
     * Returns the token parser instance to add to the existing list.
     *
     * @return array An array of Twig_TokenParser instances
     */
    public function getTokenParsers()
    {
        return array(
            // {% form_theme form "SomeBungle::widgets.twig" %}
            new FormThemeTokenParser(),
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getFilters()
    {
        return array(
            'render_enctype' => new \Twig_Filter_Method($this, 'renderEnctype', array('is_escaper' => true)),
            'render'         => new \Twig_Filter_Method($this, 'render', array('is_escaper' => true)),
            'render_hidden'  => new \Twig_Filter_Method($this, 'renderHidden', array('is_escaper' => true)),
            'render_errors'  => new \Twig_Filter_Method($this, 'renderErrors', array('is_escaper' => true)),
            'render_widget'  => new \Twig_Filter_Method($this, 'renderWidget', array('is_escaper' => true)),
            'render_label'   => new \Twig_Filter_Method($this, 'renderLabel', array('is_escaper' => true)),
            'render_data'    => new \Twig_Filter_Method($this, 'renderData', array('is_escaper' => true)),
            'render_choices' => new \Twig_Filter_Method($this, 'renderChoices', array('is_escaper' => true)),
        );
    }

    public function renderEnctype(Form $form)
    {
        return $form->isMultipart() ? 'enctype="multipart/form-data"' : '';
    }

    public function render(FieldInterface $field, array $attributes = array())
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        if ($field instanceof Form || get_class($field) === 'Symfony\Component\Form\FieldGroup') {
            return $this->templates['group']->getBlock('group', array(
                'group'      => $field,
                'attributes' => $attributes,
            ));
        }

        if ($field instanceof CollectionField) {
            return $this->templates['group']->getBlock('collection', array(
                'collection' => $field,
                'attributes' => $attributes,
            ));
        }

        return $this->templates['field']->getBlock('field', array(
            'field'      => $field,
            'attributes' => $attributes,
        ));
    }

    public function renderHidden(FieldGroupInterface $form)
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        return $this->templates['hidden']->getBlock('hidden', array(
            'fields' => $form->getHiddenFields()
        ));
    }

    public function renderErrors($formOrField)
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        return $this->templates['errors']->getBlock('errors', array(
            'errors' => $formOrField->getErrors()
        ));
    }

    public function renderLabel(FieldInterface $field, $label = null, array $attributes = array())
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        return $this->templates['label']->getBlock('label', array(
            'id'         => $field->getId(),
            'key'        => $field->getKey(),
            'label'      => null !== $label ? $label : ucfirst(strtolower(str_replace('_', ' ', $field->getKey()))),
            'attributes' => $attributes,
        ));
    }

    public function renderWidget(FieldInterface $field, array $attributes = array(), $resources = null)
    {
        if (null === $resources) {
            $parent = $field;
            $resources = array();
            while ($parent = $parent->getParent()) {
                if (isset($this->themes[$parent])) {
                    $resources = $this->themes[$parent];
                }
            }
        } else {
            $resources = array($resources);
        }

        list($widget, $template) = $this->getWidget($field, $resources);

        return $template->getBlock($widget, array(
            'field'      => $field,
            'attributes' => array_merge($field->getAttributes(), $attributes),
        ));
    }

    public function renderData(FieldInterface $field)
    {
        return $field->getData();
    }

    public function renderChoices(FieldInterface $field)<commit_msg>added initiating template in renderWidget<commit_after><?php

namespace Symfony\Bundle\TwigBundle\Extension;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\FieldGroupInterface;
use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\CollectionField;
use Symfony\Bundle\TwigBundle\TokenParser\FormThemeTokenParser;
use Symfony\Bundle\FrameworkBundle\Templating\HtmlGeneratorInterface;

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

/**
 *
 * @author <NAME> <<EMAIL>>
 */
class FormExtension extends \Twig_Extension
{
    static protected $cache = array();

    protected $resources;
    protected $templates;
    protected $environment;
    protected $themes;
    protected $generator;

    public function __construct(HtmlGeneratorInterface $generator, array $resources = array())
    {
        $this->generator = $generator;
        $this->themes = new \SplObjectStorage();
        $this->resources = array_merge(array(
            'TwigBundle::form.twig',
            'TwigBundle::widgets.twig',
        ), $resources);
    }

    /**
     * {@inheritdoc}
     */
    public function initRuntime(\Twig_Environment $environment)
    {
        $this->environment = $environment;
    }

    public function setTheme(FieldGroupInterface $group, array $resources)
    {
        $this->themes->attach($group, $resources);
    }

    /**
     * Returns the token parser instance to add to the existing list.
     *
     * @return array An array of Twig_TokenParser instances
     */
    public function getTokenParsers()
    {
        return array(
            // {% form_theme form "SomeBungle::widgets.twig" %}
            new FormThemeTokenParser(),
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getFilters()
    {
        return array(
            'render_enctype' => new \Twig_Filter_Method($this, 'renderEnctype', array('is_escaper' => true)),
            'render'         => new \Twig_Filter_Method($this, 'render', array('is_escaper' => true)),
            'render_hidden'  => new \Twig_Filter_Method($this, 'renderHidden', array('is_escaper' => true)),
            'render_errors'  => new \Twig_Filter_Method($this, 'renderErrors', array('is_escaper' => true)),
            'render_widget'  => new \Twig_Filter_Method($this, 'renderWidget', array('is_escaper' => true)),
            'render_label'   => new \Twig_Filter_Method($this, 'renderLabel', array('is_escaper' => true)),
            'render_data'    => new \Twig_Filter_Method($this, 'renderData', array('is_escaper' => true)),
            'render_choices' => new \Twig_Filter_Method($this, 'renderChoices', array('is_escaper' => true)),
        );
    }

    public function renderEnctype(Form $form)
    {
        return $form->isMultipart() ? 'enctype="multipart/form-data"' : '';
    }

    public function render(FieldInterface $field, array $attributes = array())
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        if ($field instanceof Form || get_class($field) === 'Symfony\Component\Form\FieldGroup') {
            return $this->templates['group']->getBlock('group', array(
                'group'      => $field,
                'attributes' => $attributes,
            ));
        }

        if ($field instanceof CollectionField) {
            return $this->templates['group']->getBlock('collection', array(
                'collection' => $field,
                'attributes' => $attributes,
            ));
        }

        return $this->templates['field']->getBlock('field', array(
            'field'      => $field,
            'attributes' => $attributes,
        ));
    }

    public function renderHidden(FieldGroupInterface $form)
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        return $this->templates['hidden']->getBlock('hidden', array(
            'fields' => $form->getHiddenFields()
        ));
    }

    public function renderErrors($formOrField)
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        return $this->templates['errors']->getBlock('errors', array(
            'errors' => $formOrField->getErrors()
        ));
    }

    public function renderLabel(FieldInterface $field, $label = null, array $attributes = array())
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        return $this->templates['label']->getBlock('label', array(
            'id'         => $field->getId(),
            'key'        => $field->getKey(),
            'label'      => null !== $label ? $label : ucfirst(strtolower(str_replace('_', ' ', $field->getKey()))),
            'attributes' => $attributes,
        ));
    }

    public function renderWidget(FieldInterface $field, array $attributes = array(), $resources = null)
    {
        if (null === $this->templates) {
            $this->templates = $this->resolveResources($this->resources);
        }

        if (null === $resources) {
            $parent = $field;
            $resources = array();
            while ($parent = $parent->getParent()) {
                if (isset($this->themes[$parent])) {
                    $resources = $this->themes[$parent];
                }
            }
        } else {
            $resources = array($resources);
        }

        list($widget, $template) = $this->getWidget($field, $resources);

        return $template->getBlock($widget, array(
            'field'      => $field,
            'attributes' => array_merge($field->getAttributes(), $attributes),
        ));
    }

    public function renderData(FieldInterface $field)
    {
        return $field->getData();
    }

    public function renderChoices(FieldInterface $field)
{'fraction_non_alphanumeric': 0.148421926910299, 'fraction_numerical': 0.0, 'mean_word_length': 2.423656525447825, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 173, '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': '2649754', 'n_tokens_mistral': 3332, 'n_tokens_neox': 3278, 'n_words': 676}