Skip to content

Commit

Permalink
Allow FOSCKEditor dependency to 2.0 & start upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mazsudo authored and jordisala1991 committed Aug 19, 2020
1 parent c0322e4 commit bc09c4f
Show file tree
Hide file tree
Showing 11 changed files with 1,072 additions and 204 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -24,7 +24,7 @@
],
"require": {
"php": "^7.2",
"friendsofsymfony/ckeditor-bundle": "^1.0",
"friendsofsymfony/ckeditor-bundle": "^1.0 || ^2.0",
"knplabs/knp-markdown-bundle": "^1.7",
"psr/log": "^1.0",
"sonata-project/block-bundle": "^3.16.1",
Expand Down
45 changes: 45 additions & 0 deletions phpstan.neon.dist
Expand Up @@ -14,3 +14,48 @@ parameters:
- vendor/autoload.php

ignoreErrors:
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$pluginManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\FormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\PluginManagerInterface\\.$#"
count: 1
path: src/Form/Type/FormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$templateManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\FormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\TemplateManagerInterface\\.$#"
count: 1
path: src/Form/Type/FormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$toolbarManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\FormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\ToolbarManagerInterface\\.$#"
count: 1
path: src/Form/Type/FormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Class FOS\\\\CKEditorBundle\\\\Model\\\\ConfigManagerInterface not found\\.#"
count: 2
path: src/Form/Type/FormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$pluginManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\SimpleFormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\PluginManagerInterface\\.$#"
count: 1
path: src/Form/Type/SimpleFormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$templateManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\SimpleFormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\TemplateManagerInterface\\.$#"
count: 1
path: src/Form/Type/SimpleFormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$stylesSetManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\SimpleFormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\StylesSetManagerInterface\\.$#"
count: 1
path: src/Form/Type/SimpleFormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Parameter \\$toolbarManager of method Sonata\\\\FormatterBundle\\\\Form\\\\Type\\\\SimpleFormatterType\\:\\:__construct\\(\\) has invalid typehint type FOS\\\\CKEditorBundle\\\\Model\\\\ToolbarManagerInterface\\.$#"
count: 1
path: src/Form/Type/SimpleFormatterType.php
-
# will be fixed in v5. Currently BC break
message: "#^Class FOS\\\\CKEditorBundle\\\\Model\\\\ConfigManagerInterface not found\\.#"
count: 2
path: src/Form/Type/SimpleFormatterType.php
7 changes: 6 additions & 1 deletion src/DependencyInjection/SonataFormatterExtension.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\FormatterBundle\DependencyInjection;

use FOS\CKEditorBundle\Config\CKEditorConfigurationInterface;
use Sonata\FormatterBundle\Formatter\ExtendableFormatter;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -48,7 +49,11 @@ public function load(array $configs, ContainerBuilder $container): void
$bundles = $container->getParameter('kernel.bundles');

if (isset($bundles['FOSCKEditorBundle'])) {
$loader->load('form.xml');
if (interface_exists(CKEditorConfigurationInterface::class)) {
$loader->load('form.xml');
} else {//NEXT_MAJOR: Remove this case
$loader->load('form_fcke1.xml');
}
}

if (isset($bundles['SonataBlockBundle'])) {
Expand Down
95 changes: 74 additions & 21 deletions src/Form/Type/FormatterType.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\FormatterBundle\Form\Type;

use FOS\CKEditorBundle\Config\CKEditorConfigurationInterface;
use FOS\CKEditorBundle\Model\ConfigManagerInterface;
use FOS\CKEditorBundle\Model\PluginManagerInterface;
use FOS\CKEditorBundle\Model\TemplateManagerInterface;
Expand Down Expand Up @@ -45,6 +46,8 @@ final class FormatterType extends AbstractType

/**
* @var ConfigManagerInterface
*
* @deprecated since sonata-project/formatter bundle 4.x and will be removed in version 5.0.
*/
protected $configManager;

Expand All @@ -53,6 +56,11 @@ final class FormatterType extends AbstractType
*/
protected $pluginManager;

/**
* @var CKEditorConfigurationInterface
*/
private $ckEditorConfiguration;

/**
* @var TemplateManagerInterface
*/
Expand All @@ -63,17 +71,46 @@ final class FormatterType extends AbstractType
*/
private $toolbarManager;

/**
* NEXT_MAJOR: Change signature for
* (PoolInterface $pool, TranslatorInterface $translator, CKEditorConfigurationInterface $ckEditorConfiguration).
*
* @param ConfigManagerInterface|CKEditorConfigurationInterface $configManagerOrCkEditorConfiguration
*/
public function __construct(
PoolInterface $pool,
TranslatorInterface $translator,
ConfigManagerInterface $configManager,
PluginManagerInterface $pluginManager,
TemplateManagerInterface $templateManager,
ToolbarManagerInterface $toolbarManager
object $configManagerOrCkEditorConfiguration,
?PluginManagerInterface $pluginManager = null,
?TemplateManagerInterface $templateManager = null,
?ToolbarManagerInterface $toolbarManager = null
) {
if (!$configManagerOrCkEditorConfiguration instanceof CKEditorConfigurationInterface) {
@trigger_error(sprintf(
'Passing %s as argument 3 to %s() is deprecated since sonata-project/formatter-bundle 4.x'
.' and will throw a \TypeError in version 5.0. You must pass an instance of %s instead.',
\get_class($configManagerOrCkEditorConfiguration),
__METHOD__,
CKEditorConfigurationInterface::class
), E_USER_DEPRECATED);

if (!$configManagerOrCkEditorConfiguration instanceof ConfigManagerInterface) {
throw new \TypeError(sprintf(
'Argument 3 passed to %s() must be an instance of %s or %s, %s given.',
__METHOD__,
CKEditorConfigurationInterface::class,
ConfigManagerInterface::class,
\get_class($configManagerOrCkEditorConfiguration)
));
}

$this->configManager = $configManagerOrCkEditorConfiguration;
} else {
$this->ckEditorConfiguration = $configManagerOrCkEditorConfiguration;
}

$this->pool = $pool;
$this->translator = $translator;
$this->configManager = $configManager;
$this->pluginManager = $pluginManager;
$this->templateManager = $templateManager;
$this->toolbarManager = $toolbarManager;
Expand Down Expand Up @@ -156,37 +193,53 @@ public function buildView(FormView $view, FormInterface $form, array $options):

$view->vars['format_field_options'] = $options['format_field_options'];

$defaultConfig = $this->configManager->getDefaultConfig();
if (null !== $this->ckEditorConfiguration) {
$defaultConfig = $this->ckEditorConfiguration->getDefaultConfig();
$ckeditorConfiguration = $this->ckEditorConfiguration->getConfig($defaultConfig);
} else {//NEXT_MAJOR: Remove this case
$defaultConfig = $this->configManager->getDefaultConfig();

if ($this->configManager->hasConfig($defaultConfig)) {
$ckeditorConfiguration = $this->configManager->getConfig($defaultConfig);
} else {
$ckeditorConfiguration = [];
if ($this->configManager->hasConfig($defaultConfig)) {
$ckeditorConfiguration = $this->configManager->getConfig($defaultConfig);
} else {
$ckeditorConfiguration = [];
}
}

if (!\array_key_exists('toolbar', $ckeditorConfiguration)) {
$ckeditorConfiguration['toolbar'] = array_values($options['ckeditor_toolbar_icons']);
}

if ($options['ckeditor_context']) {
$contextConfig = $this->configManager->getConfig($options['ckeditor_context']);
if (null !== $this->configManager) {//NEXT_MAJOR: Remove this case
$contextConfig = $this->configManager->getConfig($options['ckeditor_context']);
} else {
$contextConfig = $this->ckEditorConfiguration->getConfig($options['ckeditor_context']);
}

$ckeditorConfiguration = array_merge($ckeditorConfiguration, $contextConfig);
}

if ($options['ckeditor_image_format']) {
$ckeditorConfiguration['filebrowserImageUploadRouteParameters']['format'] = $options['ckeditor_image_format'];
}

if ($this->pluginManager->hasPlugins()) {
$options['ckeditor_plugins'] = $this->pluginManager->getPlugins();
}

if ($this->templateManager->hasTemplates()) {
$options['ckeditor_templates'] = $this->templateManager->getTemplates();
}

if (\is_string($ckeditorConfiguration['toolbar'])) {
$ckeditorConfiguration['toolbar'] = $this->toolbarManager->resolveToolbar($ckeditorConfiguration['toolbar']);
if (null !== $this->ckEditorConfiguration) {
$options['ckeditor_plugins'] = $this->ckEditorConfiguration->getPlugins();
$options['ckeditor_templates'] = $this->ckEditorConfiguration->getTemplates();
if (\is_string($ckeditorConfiguration['toolbar'])) {
$ckeditorConfiguration['toolbar'] = $this->ckEditorConfiguration->getToolbar($ckeditorConfiguration['toolbar']);
}
} else {//NEXT_MAJOR: Remove this case
if ($this->pluginManager->hasPlugins()) {
$options['ckeditor_plugins'] = $this->pluginManager->getPlugins();
}
if ($this->templateManager->hasTemplates()) {
$options['ckeditor_templates'] = $this->templateManager->getTemplates();
}
if (\is_string($ckeditorConfiguration['toolbar'])) {
$ckeditorConfiguration['toolbar'] = $this->toolbarManager->resolveToolbar($ckeditorConfiguration['toolbar']);
}
}

$view->vars['ckeditor_configuration'] = $ckeditorConfiguration;
Expand Down
113 changes: 85 additions & 28 deletions src/Form/Type/SimpleFormatterType.php
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\FormatterBundle\Form\Type;

use FOS\CKEditorBundle\Config\CKEditorConfigurationInterface;
use FOS\CKEditorBundle\Model\ConfigManagerInterface;
use FOS\CKEditorBundle\Model\PluginManagerInterface;
use FOS\CKEditorBundle\Model\StylesSetManagerInterface;
Expand All @@ -28,6 +29,8 @@ final class SimpleFormatterType extends AbstractType
{
/**
* @var ConfigManagerInterface
*
* @deprecated since sonata-project/formatter bundle 4.x and will be removed in version 5.0.
*/
protected $configManager;

Expand All @@ -36,6 +39,11 @@ final class SimpleFormatterType extends AbstractType
*/
protected $pluginManager;

/**
* @var CKEditorConfigurationInterface
*/
private $ckEditorConfiguration;

/**
* @var StylesSetManagerInterface
*/
Expand All @@ -51,14 +59,42 @@ final class SimpleFormatterType extends AbstractType
*/
private $toolbarManager;

/**
* NEXT_MAJOR: Change signature for (CKEditorConfigurationInterface $ckEditorConfiguration).
*
* @param ConfigManagerInterface|CKEditorConfigurationInterface $configManagerOrCkEditorConfiguration
*/
public function __construct(
ConfigManagerInterface $configManager,
PluginManagerInterface $pluginManager,
TemplateManagerInterface $templateManager,
StylesSetManagerInterface $stylesSetManager,
ToolbarManagerInterface $toolbarManager
object $configManagerOrCkEditorConfiguration,
?PluginManagerInterface $pluginManager = null,
?TemplateManagerInterface $templateManager = null,
?StylesSetManagerInterface $stylesSetManager = null,
?ToolbarManagerInterface $toolbarManager = null
) {
$this->configManager = $configManager;
if (!$configManagerOrCkEditorConfiguration instanceof CKEditorConfigurationInterface) {
@trigger_error(sprintf(
'Passing %s as argument 1 to %s() is deprecated since sonata-project/formatter-bundle 4.x'
.' and will throw a \TypeError in version 5.0. You must pass an instance of %s instead.',
\get_class($configManagerOrCkEditorConfiguration),
__METHOD__,
CKEditorConfigurationInterface::class
), E_USER_DEPRECATED);

if (!$configManagerOrCkEditorConfiguration instanceof ConfigManagerInterface) {
throw new \TypeError(sprintf(
'Argument 3 passed to %s() must be an instance of %s or %s, %s given.',
__METHOD__,
CKEditorConfigurationInterface::class,
ConfigManagerInterface::class,
\get_class($configManagerOrCkEditorConfiguration)
));
}

$this->configManager = $configManagerOrCkEditorConfiguration;
} else {
$this->ckEditorConfiguration = $configManagerOrCkEditorConfiguration;
}

$this->pluginManager = $pluginManager;
$this->templateManager = $templateManager;
$this->stylesSetManager = $stylesSetManager;
Expand All @@ -67,42 +103,63 @@ public function __construct(

public function buildView(FormView $view, FormInterface $form, array $options): void
{
$defaultConfig = $this->configManager->getDefaultConfig();
if ($this->configManager->hasConfig($defaultConfig)) {
$ckeditorConfiguration = $this->configManager->getConfig($defaultConfig);
} else {
$ckeditorConfiguration = [];
if (null !== $this->ckEditorConfiguration) {
$defaultConfig = $this->ckEditorConfiguration->getDefaultConfig();
$ckeditorConfiguration = $this->ckEditorConfiguration->getConfig($defaultConfig);
} else {//NEXT_MAJOR: Remove this case
$defaultConfig = $this->configManager->getDefaultConfig();

if ($this->configManager->hasConfig($defaultConfig)) {
$ckeditorConfiguration = $this->configManager->getConfig($defaultConfig);
} else {
$ckeditorConfiguration = [];
}
}

if (!\array_key_exists('toolbar', $ckeditorConfiguration)) {
$ckeditorConfiguration['toolbar'] = array_values($options['ckeditor_toolbar_icons']);
}

if ($options['ckeditor_context']) {
$contextConfig = $this->configManager->getConfig($options['ckeditor_context']);
if (null !== $this->configManager) {//NEXT_MAJOR: Remove this case
$contextConfig = $this->configManager->getConfig($options['ckeditor_context']);
} else {
$contextConfig = $this->ckEditorConfiguration->getConfig($options['ckeditor_context']);
}

$ckeditorConfiguration = array_merge($ckeditorConfiguration, $contextConfig);
}

if ($options['ckeditor_image_format']) {
$ckeditorConfiguration['filebrowserImageUploadRouteParameters']['format'] = $options['ckeditor_image_format'];
}

if ($this->pluginManager->hasPlugins()) {
$options['ckeditor_plugins'] = $this->pluginManager->getPlugins();
}

if ($this->templateManager->hasTemplates()) {
$options['ckeditor_templates'] = $this->templateManager->getTemplates();
}

if ($this->stylesSetManager->hasStylesSets()) {
$options['ckeditor_style_sets'] = $this->stylesSetManager->getStylesSets();
} else {
$options['ckeditor_style_sets'] = [];
}

if (\is_string($ckeditorConfiguration['toolbar'])) {
$ckeditorConfiguration['toolbar'] = $this->toolbarManager->resolveToolbar($ckeditorConfiguration['toolbar']);
if (null !== $this->ckEditorConfiguration) {
$options['ckeditor_plugins'] = $this->ckEditorConfiguration->getPlugins();
$options['ckeditor_templates'] = $this->ckEditorConfiguration->getTemplates();
$options['ckeditor_style_sets'] = $this->ckEditorConfiguration->getStyles();

if (\is_string($ckeditorConfiguration['toolbar'])) {
$ckeditorConfiguration['toolbar'] = $this->ckEditorConfiguration->getToolbar($ckeditorConfiguration['toolbar']);
}
} else {//NEXT_MAJOR: Remove this case
if ($this->pluginManager->hasPlugins()) {
$options['ckeditor_plugins'] = $this->pluginManager->getPlugins();
}

if ($this->templateManager->hasTemplates()) {
$options['ckeditor_templates'] = $this->templateManager->getTemplates();
}

if ($this->stylesSetManager->hasStylesSets()) {
$options['ckeditor_style_sets'] = $this->stylesSetManager->getStylesSets();
} else {
$options['ckeditor_style_sets'] = [];
}

if (\is_string($ckeditorConfiguration['toolbar'])) {
$ckeditorConfiguration['toolbar'] = $this->toolbarManager->resolveToolbar($ckeditorConfiguration['toolbar']);
}
}

$view->vars['ckeditor_configuration'] = $ckeditorConfiguration;
Expand Down

0 comments on commit bc09c4f

Please sign in to comment.