Dimension
6 years ago
API.php
6 years ago
AggregatedMetric.php
6 years ago
ArchivedMetric.php
6 years ago
Archiver.php
6 years ago
Categories.php
6 years ago
ComponentFactory.php
6 years ago
ComputedMetric.php
6 years ago
ConsoleCommand.php
6 years ago
Controller.php
6 years ago
ControllerAdmin.php
6 years ago
Dependency.php
6 years ago
LogTablesProvider.php
6 years ago
Manager.php
6 years ago
Menu.php
6 years ago
MetadataLoader.php
6 years ago
Metric.php
6 years ago
PluginException.php
6 years ago
ProcessedMetric.php
6 years ago
ReleaseChannels.php
6 years ago
Report.php
6 years ago
ReportsProvider.php
6 years ago
RequestProcessors.php
6 years ago
Segment.php
6 years ago
SettingsProvider.php
6 years ago
Tasks.php
6 years ago
ThemeStyles.php
6 years ago
ViewDataTable.php
6 years ago
Visualization.php
6 years ago
WidgetsProvider.php
6 years ago
ComponentFactory.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | namespace Piwik\Plugin; |
| 9 | |
| 10 | use Piwik\Log; |
| 11 | use Piwik\Plugin\Manager as PluginManager; |
| 12 | use Exception; |
| 13 | |
| 14 | /** |
| 15 | * Factory class with methods to find and instantiate Plugin components. |
| 16 | */ |
| 17 | class ComponentFactory |
| 18 | { |
| 19 | /** |
| 20 | * Create a component instance that exists within a specific plugin. Uses the component's |
| 21 | * unqualified class name and expected base type. |
| 22 | * |
| 23 | * This method will only create a class if it is located within the component type's |
| 24 | * associated subdirectory. |
| 25 | * |
| 26 | * @param string $pluginName The name of the plugin the component is expected to belong to, |
| 27 | * eg, `'DevicesDetection'`. |
| 28 | * @param string $componentClassSimpleName The component's class name w/o namespace, eg, |
| 29 | * `"GetKeywords"`. |
| 30 | * @param string $componentTypeClass The fully qualified class name of the component type, eg, |
| 31 | * `"Piwik\Plugin\Report"`. |
| 32 | * @return mixed|null A new instance of the desired component or null if not found. If the |
| 33 | * plugin is not loaded or activated or the component is not located in |
| 34 | * in the sub-namespace specified by `$componentTypeClass::COMPONENT_SUBNAMESPACE`, |
| 35 | * this method will return null. |
| 36 | */ |
| 37 | public static function factory($pluginName, $componentClassSimpleName, $componentTypeClass) |
| 38 | { |
| 39 | if (empty($pluginName) || empty($componentClassSimpleName)) { |
| 40 | Log::debug("ComponentFactory::%s: empty plugin name or component simple name requested (%s, %s)", |
| 41 | __FUNCTION__, $pluginName, $componentClassSimpleName); |
| 42 | |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | $plugin = self::getActivatedPlugin(__FUNCTION__, $pluginName); |
| 47 | if (empty($plugin)) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | $subnamespace = $componentTypeClass::COMPONENT_SUBNAMESPACE; |
| 52 | $desiredComponentClass = 'Piwik\\Plugins\\' . $pluginName . '\\' . $subnamespace . '\\' . $componentClassSimpleName; |
| 53 | |
| 54 | $components = $plugin->findMultipleComponents($subnamespace, $componentTypeClass); |
| 55 | foreach ($components as $class) { |
| 56 | if ($class == $desiredComponentClass) { |
| 57 | return new $class(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Log::debug("ComponentFactory::%s: Could not find requested component (args = ['%s', '%s', '%s']).", |
| 62 | __FUNCTION__, $pluginName, $componentClassSimpleName, $componentTypeClass); |
| 63 | |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Finds a component instance that satisfies a given predicate. |
| 69 | * |
| 70 | * @param string $componentTypeClass The fully qualified class name of the component type, eg, |
| 71 | * `"Piwik\Plugin\Report"`. |
| 72 | * @param string $pluginName|false The name of the plugin the component is expected to belong to, |
| 73 | * eg, `'DevicesDetection'`. |
| 74 | * @param callback $predicate |
| 75 | * @return mixed The component that satisfies $predicate or null if not found. |
| 76 | */ |
| 77 | public static function getComponentIf($componentTypeClass, $pluginName, $predicate) |
| 78 | { |
| 79 | $pluginManager = PluginManager::getInstance(); |
| 80 | |
| 81 | // get components to search through |
| 82 | $subnamespace = $componentTypeClass::COMPONENT_SUBNAMESPACE; |
| 83 | if (empty($pluginName)) { |
| 84 | $components = $pluginManager->findMultipleComponents($subnamespace, $componentTypeClass); |
| 85 | } else { |
| 86 | $plugin = self::getActivatedPlugin(__FUNCTION__, $pluginName); |
| 87 | if (empty($plugin)) { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | $components = $plugin->findMultipleComponents($subnamespace, $componentTypeClass); |
| 92 | } |
| 93 | |
| 94 | // find component that satisfieds predicate |
| 95 | foreach ($components as $class) { |
| 96 | $component = new $class(); |
| 97 | if ($predicate($component)) { |
| 98 | return $component; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Log::debug("ComponentFactory::%s: Could not find component that satisfies predicate (args = ['%s', '%s', '%s']).", |
| 103 | __FUNCTION__, $componentTypeClass, $pluginName, get_class($predicate)); |
| 104 | |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param string $function |
| 110 | * @param string $pluginName |
| 111 | * @return null|\Piwik\Plugin |
| 112 | */ |
| 113 | private static function getActivatedPlugin($function, $pluginName) |
| 114 | { |
| 115 | $pluginManager = PluginManager::getInstance(); |
| 116 | try { |
| 117 | if (!$pluginManager->isPluginActivated($pluginName)) { |
| 118 | Log::debug("ComponentFactory::%s: component for deactivated plugin ('%s') requested.", |
| 119 | $function, $pluginName); |
| 120 | |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | return $pluginManager->getLoadedPlugin($pluginName); |
| 125 | } catch (Exception $e) { |
| 126 | Log::debug($e); |
| 127 | |
| 128 | return null; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 |