CoreFiles by mySites.guru
ButtonsRegistry.php149 lines, 4,399 bytes, from Joomla_5.0.0-alpha4-Alpha-Full_Package.zip
Raw History Compare with 5.0.0-alpha3
md5
2b26c7f9fdaf521752ee6ebfe06577d1
sha1
256646e3fbf59bca5d7bae4b86b2220b5a43a9ad
sha256
b01f7ea01226d8c800ff92b026040ab81110e9dbd7389d0d93e2185e921dfa1b
<?php

/**
 * Joomla! Content Management System
 *
 * @copyright  (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Editor\Button;

use Joomla\CMS\Event\Editor\EditorButtonsSetupEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;
use Joomla\Event\SubscriberInterface;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * Buttons Registry class
 * @since   5.0.0
 */
final class ButtonsRegistry implements ButtonsRegistryInterface, DispatcherAwareInterface
{
    use DispatcherAwareTrait;

    /**
     * List of registered elements
     *
     * @var    array
     * @since   5.0.0
     */
    protected $registry = [];

    /**
     * Internal flag of initialisation
     *
     * @var    boolean
     * @since   5.0.0
     */
    private $initialised = false;

    /**
     * Register element in registry, add new or override existing.
     *
     * @param   ButtonInterface $instance  A button instance.
     *
     * @return  static
     * @since    5.0.0
     */
    public function add(ButtonInterface $instance): ButtonsRegistryInterface
    {
        $this->registry[$instance->getButtonName()] = $instance;

        return $this;
    }

    /**
     * Return list of all registered elements.
     *
     * @return ButtonInterface[]
     * @since    5.0.0
     */
    public function getAll(): array
    {
        return array_values($this->registry);
    }

    /**
     * Trigger event to allow to register the elements through plugins.
     *
     * @param array $options  Extra data with editor information.
     *
     * @return  ButtonsRegistryInterface
     * @since   5.0.0
     */
    public function initRegistry(array $options = []): ButtonsRegistryInterface
    {
        if ($this->initialised) {
            return $this;
        }

        $this->initialised = true;

        $options['subject']         = $this;
        $options['editorType']      = $options['editorType'] ?? '';
        $options['disabledButtons'] = $options['disabledButtons'] ?? [];

        $event      = new EditorButtonsSetupEvent('onEditorButtonsSetup', $options);
        $dispatcher = $this->getDispatcher();

        PluginHelper::importPlugin('editors-xtd', null, true, $dispatcher);
        $dispatcher->dispatch($event->getName(), $event);

        // Load legacy buttons for backward compatibility
        $plugins  = PluginHelper::getPlugin('editors-xtd');
        $editorId = $options['editorId'] ?? '';
        $asset    = (int) ($options['asset'] ?? 0);
        $author   = (int) ($options['author'] ?? 0);

        foreach ($plugins as $plugin) {
            $pluginInst = Factory::getApplication()->bootPlugin($plugin->name, 'editors-xtd');

            if (!$pluginInst || $pluginInst instanceof SubscriberInterface) {
                continue;
            }

            if (!method_exists($pluginInst, 'onDisplay')) {
                continue;
            }

            $legacyButton = $pluginInst->onDisplay($editorId, $asset, $author);

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

            @trigger_error('6.0 Button "' . $plugin->name . '" instance should be set up onEditorButtonsSetup event.', \E_USER_DEPRECATED);

            // Transform Legacy buttons to Button object
            if (\is_array($legacyButton)) {
                foreach ($legacyButton as $item) {
                    if ($item instanceof CMSObject) {
                        $props   = $item->getProperties();
                        $options = $props['options'] ?? [];
                        unset($props['options']);

                        $button = new Button($plugin->name, $props, $options);
                        $this->add($button);
                    }
                }
            } elseif ($legacyButton instanceof CMSObject) {
                $props   = $legacyButton->getProperties();
                $options = $props['options'] ?? [];
                unset($props['options']);

                $button = new Button($plugin->name, $props, $options);
                $this->add($button);
            }
        }

        return $this;
    }
}

The same content is in 2 releases, Joomla 5.0.0-alpha4 to 5.0.0-beta1. See them all, or every version of this file.