Skip to content
This repository was archived by the owner on Aug 13, 2022. It is now read-only.

Commit 88033e1

Browse files
authored
Merge pull request #196 from zikula-modules/external-plugin-collections
Overhaul event handling for external editor plugins
2 parents a1f997d + 04b4b96 commit 88033e1

14 files changed

+183
-168
lines changed

Collection/HelperCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Zikula\ScribiteModule\Collection;
1515

1616
/**
17-
* This class is used as the subject of \Zikula\ScribiteModule\Event\EditorHelperEvent
17+
* This class is used as the subject of `\Zikula\ScribiteModule\Event\EditorHelperEvent`.
1818
* Any module that needs to add page assets (javascript, css) can use an event listener to automatically load their
1919
* helper every time a Scribite editor is loaded.
2020
*/

Editor/CKEditor/Collection/PluginCollection.php

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
<?php
22

33
declare(strict_types=1);
4-
/**
5-
* Zikula Application Framework
4+
5+
/*
6+
* This file is part of the Zikula package.
7+
*
8+
* Copyright Zikula Foundation - https://ziku.la/
69
*
7-
* @copyright (c) Zikula Development Team
8-
* @see https://ziku.la
9-
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
1012
*/
1113

1214
namespace Zikula\ScribiteModule\Editor\CKEditor\Collection;
1315

16+
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;
17+
1418
/**
15-
* This class is used as the subject of the event 'moduleplugin.ckeditor.externalplugins'.
16-
* Any module that needs to add *external* editor plugins can use an event listener to automatically load their
19+
* This class is used by the `Zikula\ScribiteModule\Editor\CKEditor\LoadExternalPluginsEvent`.
20+
* Any extension that needs to add *external* editor plugins can use an event listener to automatically load their
1721
* helper every time a Scribite editor is loaded.
1822
* @see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.resourceManager.html#addExternal
1923
* @see http://ckeditor.com/comment/47922#comment-47922
2024
*/
21-
class PluginCollection
25+
class PluginCollection implements EditorPluginCollectionInterface
2226
{
2327
/**
24-
* stack of plugins
25-
* @var array
28+
* Stack of plugins.
2629
*/
27-
private $plugins;
28-
29-
/**
30-
* PluginCollection constructor.
31-
*/
32-
public function __construct()
33-
{
34-
$this->plugins = [];
35-
}
30+
private $plugins = [];
3631

3732
/**
38-
* add a plugin to the stack
39-
* @param array $plugin
40-
* $helper must have array keys [name, path, file, img] set
33+
* Adds a plugin to the stack.
34+
*
35+
* $plugin must have array keys [name, path, file, img] set.
4136
*/
42-
public function add(array $plugin)
37+
public function add(array $plugin): void
4338
{
4439
if (isset($plugin['name'], $plugin['path'], $plugin['file'], $plugin['img'])) {
4540
$plugin['path'] = rtrim($plugin['path'], '/') . '/'; // ensure there is a trailing slash
@@ -48,10 +43,9 @@ public function add(array $plugin)
4843
}
4944

5045
/**
51-
* get the helper stack
52-
* @return array
46+
* Gets the plugins stack.
5347
*/
54-
public function getPlugins()
48+
public function getPlugins(): array
5549
{
5650
return $this->plugins;
5751
}

Editor/CKEditor/Helper/EditorHelper.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
namespace Zikula\ScribiteModule\Editor\CKEditor\Helper;
1515

1616
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17-
use Zikula\Bundle\CoreBundle\Event\GenericEvent;
1817
use Zikula\ScribiteModule\Editor\CKEditor\Collection\PluginCollection;
1918
use Zikula\ScribiteModule\Editor\EditorHelperInterface;
19+
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;
2020

2121
class EditorHelper implements EditorHelperInterface
2222
{
@@ -25,9 +25,6 @@ class EditorHelper implements EditorHelperInterface
2525
*/
2626
private $dispatcher;
2727

28-
/**
29-
* @param EventDispatcherInterface $dispatcher
30-
*/
3128
public function __construct(EventDispatcherInterface $dispatcher)
3229
{
3330
$this->dispatcher = $dispatcher;
@@ -44,14 +41,8 @@ public function getParameters(array $parameters = []): array
4441
/**
4542
* {@inheritdoc}
4643
*/
47-
public function getExternalPlugins(): array
44+
public function getPluginCollection(): EditorPluginCollectionInterface
4845
{
49-
if (null === $this->dispatcher) {
50-
throw new \RuntimeException('Dispatcher has not been set.');
51-
}
52-
$event = new GenericEvent(new PluginCollection());
53-
$plugins = $this->dispatcher->dispatch($event, 'moduleplugin.ckeditor.externalplugins')->getSubject()->getPlugins();
54-
55-
return $plugins;
46+
return new PluginCollection();
5647
}
5748
}

Editor/EditorHelperInterface.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ interface EditorHelperInterface
2222
public function getParameters(): array;
2323

2424
/**
25-
* An array of parameters which will be added to the template within the
26-
* property `externalEditorPlugins`
25+
* A collection containing definitions for external editor plugins
2726
*/
28-
public function getExternalPlugins(): array;
27+
public function getPluginCollection(): EditorPluginCollectionInterface;
2928
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Zikula package.
7+
*
8+
* Copyright Zikula Foundation - https://ziku.la/
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zikula\ScribiteModule\Editor;
15+
16+
interface EditorPluginCollectionInterface
17+
{
18+
/**
19+
* Add a plugin to the stack.
20+
* Array structure depends on the editor's requirements.
21+
*/
22+
public function add(array $plugin): void;
23+
24+
/**
25+
* Gets the plugins stack.
26+
*/
27+
public function getPlugins(): array;
28+
}

Editor/Factory.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Zikula\ScribiteModule\Collection\HelperCollection;
2121
use Zikula\ScribiteModule\Collector\EditorCollector;
2222
use Zikula\ScribiteModule\Event\EditorHelperEvent;
23+
use Zikula\ScribiteModule\Event\LoadExternalPluginsEvent;
2324
use Zikula\ScribiteModule\Helper\AssetHelper;
2425
use Zikula\ThemeModule\Api\ApiInterface\PageAssetApiInterface;
2526

@@ -115,7 +116,10 @@ public function load($moduleName)
115116
throw new \RuntimeException(sprintf('%s must implement %s', get_class($editorHelper), EditorHelperInterface::class));
116117
}
117118
$additionalEditorParameters = $editorHelper->getParameters();
118-
$additionalExternalEditorPlugins = $editorHelper->getExternalPlugins();
119+
/** @var EditorPluginCollectionInterface $additionalExternalEditorPlugins */
120+
$additionalExternalEditorPlugins = $editorHelper->getPluginCollection();
121+
$event = new LoadExternalPluginsEvent($additionalExternalEditorPlugins, $editorId);
122+
$additionalExternalEditorPlugins = $this->dispatcher->dispatch($event)->getPluginCollection()->getPlugins();
119123
}
120124

121125
// assign disabled textareas to template as a javascript array

Editor/Quill/Collection/PluginCollection.php

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
11
<?php
22

33
declare(strict_types=1);
4-
/**
5-
* Zikula Application Framework
4+
5+
/*
6+
* This file is part of the Zikula package.
7+
*
8+
* Copyright Zikula Foundation - https://ziku.la/
69
*
7-
* @copyright (c) Zikula Development Team
8-
* @see https://ziku.la
9-
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
1012
*/
1113

1214
namespace Zikula\ScribiteModule\Editor\Quill\Collection;
1315

16+
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;
17+
1418
/**
15-
* This class is used as the subject of the event 'moduleplugin.quill.externalplugins'.
16-
* Any module that needs to add *external* editor plugins can use an event listener to automatically load their
19+
* This class is used by the `Zikula\ScribiteModule\Editor\Quill\LoadExternalPluginsEvent`.
20+
* Any extension that needs to add *external* editor plugins can use an event listener to automatically load their
1721
* helper every time a Scribite editor is loaded.
1822
* @see https://quilljs.com/docs/modules/
1923
*/
20-
class PluginCollection
24+
class PluginCollection implements EditorPluginCollectionInterface
2125
{
2226
/**
23-
* stack of plugins
24-
* @var array
27+
* Stack of plugins.
2528
*/
26-
private $plugins;
27-
28-
/**
29-
* PluginCollection constructor.
30-
*/
31-
public function __construct()
32-
{
33-
$this->plugins = [];
34-
}
29+
private $plugins = [];
3530

3631
/**
37-
* add a plugin to the stack
38-
* @param array $plugin
39-
* $helper must have array keys [name, path] set
32+
* Adds a plugin to the stack.
33+
*
34+
* $plugin must have array keys [name, path] set.
4035
*/
41-
public function add(array $plugin)
36+
public function add(array $plugin): void
4237
{
4338
if (isset($plugin['name'], $plugin['path'])) {
4439
$this->plugins[] = $plugin;
4540
}
4641
}
4742

4843
/**
49-
* get the helper stack
50-
* @return array
44+
* Gets the plugins stack.
5145
*/
52-
public function getPlugins()
46+
public function getPlugins(): array
5347
{
5448
return $this->plugins;
5549
}

Editor/Quill/Helper/EditorHelper.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/*
56
* This file is part of the Zikula package.
67
*
@@ -13,8 +14,8 @@
1314
namespace Zikula\ScribiteModule\Editor\Quill\Helper;
1415

1516
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Zikula\Bundle\CoreBundle\Event\GenericEvent;
1717
use Zikula\ScribiteModule\Editor\EditorHelperInterface;
18+
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;
1819
use Zikula\ScribiteModule\Editor\Quill\Collection\PluginCollection;
1920

2021
class EditorHelper implements EditorHelperInterface
@@ -24,9 +25,6 @@ class EditorHelper implements EditorHelperInterface
2425
*/
2526
private $dispatcher;
2627

27-
/**
28-
* @param EventDispatcherInterface $dispatcher
29-
*/
3028
public function __construct(EventDispatcherInterface $dispatcher)
3129
{
3230
$this->dispatcher = $dispatcher;
@@ -43,14 +41,8 @@ public function getParameters(array $parameters = []): array
4341
/**
4442
* {@inheritdoc}
4543
*/
46-
public function getExternalPlugins(): array
44+
public function getPluginCollection(): EditorPluginCollectionInterface
4745
{
48-
if (null === $this->dispatcher) {
49-
throw new \RuntimeException('Dispatcher has not been set.');
50-
}
51-
$event = new GenericEvent(new PluginCollection());
52-
$plugins = $this->dispatcher->dispatch($event, 'moduleplugin.quill.externalplugins')->getSubject()->getPlugins();
53-
54-
return $plugins;
46+
return new PluginCollection();
5547
}
5648
}

Editor/Summernote/Collection/PluginCollection.php

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
11
<?php
22

33
declare(strict_types=1);
4-
/**
5-
* Zikula Application Framework
4+
5+
/*
6+
* This file is part of the Zikula package.
7+
*
8+
* Copyright Zikula Foundation - https://ziku.la/
69
*
7-
* @copyright (c) Zikula Development Team
8-
* @see https://ziku.la
9-
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
1012
*/
1113

1214
namespace Zikula\ScribiteModule\Editor\Summernote\Collection;
1315

16+
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;
17+
1418
/**
15-
* This class is used as the subject of the event 'moduleplugin.summernote.externalplugins'.
16-
* Any module that needs to add *external* editor plugins can use an event listener to automatically load their
19+
* This class is used by the `Zikula\ScribiteModule\Editor\Summernote\LoadExternalPluginsEvent`.
20+
* Any extension that needs to add *external* editor plugins can use an event listener to automatically load their
1721
* helper every time a Scribite editor is loaded.
1822
* @see http://summernote.org/deep-dive/#module-system
1923
*/
20-
class PluginCollection
24+
class PluginCollection implements EditorPluginCollectionInterface
2125
{
2226
/**
23-
* stack of plugins
24-
* @var array
27+
* Stack of plugins.
2528
*/
26-
private $plugins;
27-
28-
/**
29-
* PluginCollection constructor.
30-
*/
31-
public function __construct()
32-
{
33-
$this->plugins = [];
34-
}
29+
private $plugins = [];
3530

3631
/**
37-
* add a plugin to the stack
38-
* @param array $plugin
39-
* $helper must have array keys [name, path] set
32+
* Adds a plugin to the stack.
33+
*
34+
* $plugin must have array keys [name, path] set.
4035
*/
41-
public function add(array $plugin)
36+
public function add(array $plugin): void
4237
{
4338
if (isset($plugin['name'], $plugin['path'])) {
4439
$this->plugins[] = $plugin;
4540
}
4641
}
4742

4843
/**
49-
* get the helper stack
50-
* @return array
44+
* Gets the plugins stack.
5145
*/
52-
public function getPlugins()
46+
public function getPlugins(): array
5347
{
5448
return $this->plugins;
5549
}

0 commit comments

Comments
 (0)