PluginList.php 4.15 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Developer\Model\Di;

use Magento\Framework\Interception;
use Magento\Framework\Interception\DefinitionInterface;

/**
 * Provides plugin list configuration
 */
class PluginList extends Interception\PluginList\PluginList
{
    /**#@+
     * Constants for the plugin types
     */
    const PLUGIN_TYPE_BEFORE = 'before';
    const PLUGIN_TYPE_AROUND = 'around';
    const PLUGIN_TYPE_AFTER = 'after';
    /**#@-*/

    /**#@-*/
    private $pluginList = [
       self::PLUGIN_TYPE_BEFORE => [],
       self::PLUGIN_TYPE_AROUND => [],
       self::PLUGIN_TYPE_AFTER  => []
    ];

    /**
     * Mapping of plugin type codes to plugin types
     * @var array
     */
    private $pluginTypeMapping = [
        DefinitionInterface::LISTENER_AROUND => self::PLUGIN_TYPE_AROUND,
        DefinitionInterface::LISTENER_BEFORE => self::PLUGIN_TYPE_BEFORE,
        DefinitionInterface::LISTENER_AFTER => self::PLUGIN_TYPE_AFTER
    ];

    /**
     * Returns plugins config
     *
     * @return array
     */
    public function getPluginsConfig()
    {
        $this->_loadScopedData();

        return $this->_inherited;
    }

    /**
     * Sets scope priority scheme
     *
     * @param array $areaCodes
     *
     * @return void
     */
    public function setScopePriorityScheme($areaCodes)
    {
        $this->_scopePriorityScheme = $areaCodes;
    }

    /**
     * Whether scope code is current scope code
     *
     * @param string $scopeCode
     *
     * @return bool
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function isCurrentScope($scopeCode)
    {
        return false;
    }

    /**
     * Load the plugins information
     *
     * @param string $type
     * @return array
     */
    private function getPlugins($type)
    {
        $this->_loadScopedData();
        if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) {
            $this->_inheritPlugins($type);
        }
        return $this->_inherited[$type];
    }

    /**
     * Return the list of plugins for the class
     *
     * @param string $className
     * @return array
     * @throws \InvalidArgumentException
     */
    public function getPluginsListByClass($className)
    {
        $this->getPlugins($className);
        if (!isset($this->_inherited[$className])) {
            return $this->pluginList;
        }

        foreach ($this->_inherited[$className] as $plugin) {
            foreach ($this->_definitions->getMethodList($plugin['instance']) as $pluginMethod => $methodTypes) {
                $this->addPluginToList(
                    $plugin['instance'],
                    $pluginMethod,
                    $methodTypes,
                    DefinitionInterface::LISTENER_AROUND
                );
                $this->addPluginToList(
                    $plugin['instance'],
                    $pluginMethod,
                    $methodTypes,
                    DefinitionInterface::LISTENER_BEFORE
                );
                $this->addPluginToList(
                    $plugin['instance'],
                    $pluginMethod,
                    $methodTypes,
                    DefinitionInterface::LISTENER_AFTER
                );
            }
        }
        return $this->pluginList;
    }

    /**
     * Add plugin to the appropriate type bucket
     *
     * @param string $pluginInstance
     * @param string $pluginMethod
     * @param int $methodTypes
     * @param int $typeCode
     * @return void
     */
    private function addPluginToList($pluginInstance, $pluginMethod, $methodTypes, $typeCode)
    {
        if ($methodTypes & $typeCode) {
            if (!array_key_exists($pluginInstance, $this->pluginList[$this->pluginTypeMapping[$typeCode]])) {
                $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance] = [];
            }

            if (!in_array($pluginMethod, $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance])) {
                $this->pluginList[$this->pluginTypeMapping[$typeCode]][$pluginInstance][] = $pluginMethod;
            }
        }
    }
}