<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\AdminBundle\Form\Type;

use Darvin\ContentBundle\Widget\WidgetInterface;
use Darvin\ContentBundle\Widget\WidgetPoolInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * CKEditor form type
 */
class CKEditorType extends AbstractType
{
    const CKEDITOR_TYPE_CLASS = __CLASS__;

    /**
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    private $requestStack;

    /**
     * @var \Darvin\ContentBundle\Widget\WidgetPoolInterface
     */
    private $widgetPool;

    /**
     * @var string
     */
    private $pluginFilename;

    /**
     * @var string
     */
    private $pluginsPath;

    /**
     * @var string
     */
    private $webDir;

    /**
     * @param \Symfony\Component\HttpFoundation\RequestStack   $requestStack   Request stack
     * @param \Darvin\ContentBundle\Widget\WidgetPoolInterface $widgetPool     Widget pool
     * @param string                                           $pluginFilename Plugin filename
     * @param string                                           $pluginsPath    Plugins path
     * @param string                                           $webDir         Web directory
     */
    public function __construct(
        RequestStack $requestStack,
        WidgetPoolInterface $widgetPool,
        $pluginFilename,
        $pluginsPath,
        $webDir
    ) {
        $this->requestStack = $requestStack;
        $this->widgetPool = $widgetPool;
        $this->pluginFilename = $pluginFilename;
        $this->pluginsPath = $pluginsPath;
        $this->webDir = $webDir;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $plugins = array(
            'lineutils' => array(
                'path'     => $this->pluginsPath.'/lineutils/',
                'filename' => 'plugin.js',
            ),
            'widget' => array(
                'path'     => $this->pluginsPath.'/widget/',
                'filename' => 'plugin.js',
            ),
        );

        $extraPlugins = array(
            'lineutils',
            'widget',
        );

        foreach ($this->widgetPool->getAllWidgets() as $widget) {
            $path = $this->getWidgetPluginPath($widget);

            if (empty($path)) {
                continue;
            }

            $widgetName = $widget->getName();

            $plugins[$widgetName] = array(
                'path'     => $path,
                'filename' => $this->getWidgetPluginFilename($widget),
            );
            $extraPlugins[] = $widgetName;
        }

        $config = array(
            'extraPlugins' => implode(',', $extraPlugins),
        );

        $request = $this->requestStack->getCurrentRequest();

        if (!empty($request)) {
            $config['language'] = $request->getLocale();
        }

        $resolver->setDefaults(array(
            'config'  => $config,
            'plugins' => $plugins,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'darvin_admin_ckeditor';
    }

    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return 'ckeditor';
    }

    /**
     * @param \Darvin\ContentBundle\Widget\WidgetInterface $widget Widget
     *
     * @return string
     */
    private function getWidgetPluginPath(WidgetInterface $widget)
    {
        $options = $widget->getOptions();

        if (!isset($options['ckeditor_plugin_path'])) {
            return null;
        }

        $path = $options['ckeditor_plugin_path'];

        if ('/' !== substr($path, -1)) {
            $path .= '/';
        }

        return $path;
    }

    /**
     * @param \Darvin\ContentBundle\Widget\WidgetInterface $widget Widget
     *
     * @return string
     */
    private function getWidgetPluginFilename(WidgetInterface $widget)<commit_msg>Replace form type name with FQCN in getParent() methods.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, <NAME>
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\AdminBundle\Form\Type;

use Darvin\ContentBundle\Widget\WidgetInterface;
use Darvin\ContentBundle\Widget\WidgetPoolInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * CKEditor form type
 */
class CKEditorType extends AbstractType
{
    const CKEDITOR_TYPE_CLASS = __CLASS__;

    /**
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    private $requestStack;

    /**
     * @var \Darvin\ContentBundle\Widget\WidgetPoolInterface
     */
    private $widgetPool;

    /**
     * @var string
     */
    private $pluginFilename;

    /**
     * @var string
     */
    private $pluginsPath;

    /**
     * @var string
     */
    private $webDir;

    /**
     * @param \Symfony\Component\HttpFoundation\RequestStack   $requestStack   Request stack
     * @param \Darvin\ContentBundle\Widget\WidgetPoolInterface $widgetPool     Widget pool
     * @param string                                           $pluginFilename Plugin filename
     * @param string                                           $pluginsPath    Plugins path
     * @param string                                           $webDir         Web directory
     */
    public function __construct(
        RequestStack $requestStack,
        WidgetPoolInterface $widgetPool,
        $pluginFilename,
        $pluginsPath,
        $webDir
    ) {
        $this->requestStack = $requestStack;
        $this->widgetPool = $widgetPool;
        $this->pluginFilename = $pluginFilename;
        $this->pluginsPath = $pluginsPath;
        $this->webDir = $webDir;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $plugins = array(
            'lineutils' => array(
                'path'     => $this->pluginsPath.'/lineutils/',
                'filename' => 'plugin.js',
            ),
            'widget' => array(
                'path'     => $this->pluginsPath.'/widget/',
                'filename' => 'plugin.js',
            ),
        );

        $extraPlugins = array(
            'lineutils',
            'widget',
        );

        foreach ($this->widgetPool->getAllWidgets() as $widget) {
            $path = $this->getWidgetPluginPath($widget);

            if (empty($path)) {
                continue;
            }

            $widgetName = $widget->getName();

            $plugins[$widgetName] = array(
                'path'     => $path,
                'filename' => $this->getWidgetPluginFilename($widget),
            );
            $extraPlugins[] = $widgetName;
        }

        $config = array(
            'extraPlugins' => implode(',', $extraPlugins),
        );

        $request = $this->requestStack->getCurrentRequest();

        if (!empty($request)) {
            $config['language'] = $request->getLocale();
        }

        $resolver->setDefaults(array(
            'config'  => $config,
            'plugins' => $plugins,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'darvin_admin_ckeditor';
    }

    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return 'Ivory\\CKEditorBundle\\Form\\Type\\CKEditorType';
    }

    /**
     * @param \Darvin\ContentBundle\Widget\WidgetInterface $widget Widget
     *
     * @return string
     */
    private function getWidgetPluginPath(WidgetInterface $widget)
    {
        $options = $widget->getOptions();

        if (!isset($options['ckeditor_plugin_path'])) {
            return null;
        }

        $path = $options['ckeditor_plugin_path'];

        if ('/' !== substr($path, -1)) {
            $path .= '/';
        }

        return $path;
    }

    /**
     * @param \Darvin\ContentBundle\Widget\WidgetInterface $widget Widget
     *
     * @return string
     */
    private function getWidgetPluginFilename(WidgetInterface $widget)
{'fraction_non_alphanumeric': 0.12626321291671508, 'fraction_numerical': 0.0011615750958299453, 'mean_word_length': 1.8220255653883972, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 66, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1043905', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2280, 'n_words': 450}