<commit_before><?php

namespace Oro\Bundle\LayoutBundle\Layout\DataProvider;

use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

abstract class AbstractFormProvider
{
    const USED_FOR_CACHE_ONLY_OPTION = 'usedForCacheOnlyOption';

    /** @var array */
    protected $forms = [];

    /** @var FormFactoryInterface */
    protected $formFactory;

    /** @var UrlGeneratorInterface */
    protected $router;

    /**
     * @param FormFactoryInterface  $formFactory
     * @param UrlGeneratorInterface $router
     */
    public function __construct(FormFactoryInterface $formFactory, UrlGeneratorInterface $router)
    {
        $this->formFactory = $formFactory;
        $this->router = $router;
    }

    /**
     * Build new form
     *
     * @param string $formName
     * @param mixed $data
     * @param array $options
     *
     * @return FormInterface
     */
    protected function getForm($formName, $data = null, array $options = [])
    {
        $cacheKey = $this->getCacheKey($formName, $options);
        unset($options[self::USED_FOR_CACHE_ONLY_OPTION]);

        if (!array_key_exists($cacheKey, $this->forms)) {
            $this->forms[$cacheKey] = $this->formFactory->create($formName, $data, $options);
        }

        return $this->forms[$cacheKey];
    }

    /**
     * Generate Url
     *
     * @param string $name
     * @param array  $arguments
     * @return mixed
     */
    protected function generateUrl($name, $arguments = array())
    {
        return $this->router->generate($name, $arguments);
    }

    /**
     * Get form cache key
     *
     * @param string $formName
     * @param array $options
     *
     * @return string
     */
    protected function getCacheKey($formName, array $options = [])
    {
        return sprintf('%s:%s', $formName, md5(serialize($options)));
    }
}<commit_msg>BB-5243: Refactor form data providers to return FormInterface instead of FormAccessor - refactor all layout form data providers to return form view<commit_after><?php

namespace Oro\Bundle\LayoutBundle\Layout\DataProvider;

use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

abstract class AbstractFormProvider
{
    /** @var array */
    protected $forms = [];

    /** @var array */
    protected $formViews = [];

    /** @var FormFactoryInterface */
    protected $formFactory;

    /** @var UrlGeneratorInterface */
    protected $router;

    /**
     * @param FormFactoryInterface  $formFactory
     * @param UrlGeneratorInterface $router
     */
    public function __construct(FormFactoryInterface $formFactory, UrlGeneratorInterface $router)
    {
        $this->formFactory = $formFactory;
        $this->router = $router;
    }

    /**
     * Build new form
     *
     * @param string $formName
     * @param mixed  $data
     * @param array  $options
     * @param array  $cacheKeyOptions
     *
     * @return FormInterface
     */
    protected function getForm($formName, $data = null, array $options = [], array $cacheKeyOptions = [])
    {
        $cacheKey = $this->getCacheKey($formName, $options, $cacheKeyOptions);

        if (!array_key_exists($cacheKey, $this->forms)) {
            $this->forms[$cacheKey] = $this->createForm($formName, $data, $options);
        }

        return $this->forms[$cacheKey];
    }

    /**
     * Retrieve form view
     *
     * @param string $formName
     * @param mixed  $data
     * @param array  $options
     * @param array  $cacheKeyOptions
     *
     * @return mixed
     */
    protected function getFormView($formName, $data = null, array $options = [], array $cacheKeyOptions = [])
    {
        $cacheKey = $this->getCacheKey($formName, $options, $cacheKeyOptions);
        if (!array_key_exists($cacheKey, $this->formViews)) {
            $form = $this->getForm($formName, $data, $options, $cacheKeyOptions);

            $this->formViews[$cacheKey] = $form->createView();
        }

        return $this->formViews[$cacheKey];
    }

    /**
     * @param string $formName
     * @param null   $data
     * @param array  $options
     *
     * @return FormInterface
     */
    protected function createForm($formName, $data = null, array $options = [])
    {
        return $this->formFactory->create($formName, $data, $options);
    }

    /**
     * Generate Url
     *
     * @param string $name
     * @param array  $arguments
     * @return mixed
     */
    protected function generateUrl($name, array $arguments = [])
    {
        return $this->router->generate($name, $arguments);
    }

    /**
     * Get form cache key
     *
     * @param string $formName
     * @param array  $options
     * @param array  $cacheKeyOptions
     *
     * @return string
     */
    protected function getCacheKey($formName, array $options = [], array $cacheKeyOptions = [])
    {
        return sprintf('%s:%s', $formName, md5(serialize(array_merge($options, $cacheKeyOptions))));
    }
}
{'fraction_non_alphanumeric': 0.13157380254154447, 'fraction_numerical': 0.0011730205278592375, 'mean_word_length': 2.67264895908112, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '6204405', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1468, 'n_words': 328}