ConsoleCommand
2 years ago
Dimension
1 year ago
API.php
1 year ago
AggregatedMetric.php
2 years ago
ArchivedMetric.php
1 year ago
Archiver.php
1 year ago
Categories.php
2 years ago
ComponentFactory.php
2 years ago
ComputedMetric.php
1 year ago
ConsoleCommand.php
1 year ago
Controller.php
1 year ago
ControllerAdmin.php
1 year ago
Dependency.php
1 year ago
LogTablesProvider.php
2 years ago
Manager.php
1 year ago
Menu.php
1 year ago
MetadataLoader.php
1 year ago
Metric.php
1 year ago
PluginException.php
1 year ago
ProcessedMetric.php
1 year ago
ReleaseChannels.php
2 years ago
Report.php
1 year ago
ReportsProvider.php
2 years ago
RequestProcessors.php
2 years ago
Segment.php
1 year ago
SettingsProvider.php
2 years ago
Tasks.php
1 year ago
ThemeStyles.php
2 years ago
ViewDataTable.php
1 year ago
Visualization.php
1 year ago
WidgetsProvider.php
2 years ago
ComponentFactory.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Plugin; |
| 10 | |
| 11 | use Piwik\Log; |
| 12 | use Piwik\Plugin\Manager as PluginManager; |
| 13 | use Exception; |
| 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)", __FUNCTION__, $pluginName, $componentClassSimpleName); |
| 41 | return null; |
| 42 | } |
| 43 | $plugin = self::getActivatedPlugin(__FUNCTION__, $pluginName); |
| 44 | if (empty($plugin)) { |
| 45 | return null; |
| 46 | } |
| 47 | $subnamespace = $componentTypeClass::COMPONENT_SUBNAMESPACE; |
| 48 | $desiredComponentClass = 'Piwik\\Plugins\\' . $pluginName . '\\' . $subnamespace . '\\' . $componentClassSimpleName; |
| 49 | $components = $plugin->findMultipleComponents($subnamespace, $componentTypeClass); |
| 50 | foreach ($components as $class) { |
| 51 | if ($class === $desiredComponentClass) { |
| 52 | return new $class(); |
| 53 | } |
| 54 | } |
| 55 | Log::debug("ComponentFactory::%s: Could not find requested component (args = ['%s', '%s', '%s']).", __FUNCTION__, $pluginName, $componentClassSimpleName, $componentTypeClass); |
| 56 | return null; |
| 57 | } |
| 58 | /** |
| 59 | * Finds a component instance that satisfies a given predicate. |
| 60 | * |
| 61 | * @param string $componentTypeClass The fully qualified class name of the component type, eg, |
| 62 | * `"Piwik\Plugin\Report"`. |
| 63 | * @param string $pluginName|false The name of the plugin the component is expected to belong to, |
| 64 | * eg, `'DevicesDetection'`. |
| 65 | * @param callback $predicate |
| 66 | * @return mixed The component that satisfies $predicate or null if not found. |
| 67 | */ |
| 68 | public static function getComponentIf($componentTypeClass, $pluginName, $predicate) |
| 69 | { |
| 70 | $pluginManager = PluginManager::getInstance(); |
| 71 | // get components to search through |
| 72 | $subnamespace = $componentTypeClass::COMPONENT_SUBNAMESPACE; |
| 73 | if (empty($pluginName)) { |
| 74 | $components = $pluginManager->findMultipleComponents($subnamespace, $componentTypeClass); |
| 75 | } else { |
| 76 | $plugin = self::getActivatedPlugin(__FUNCTION__, $pluginName); |
| 77 | if (empty($plugin)) { |
| 78 | return null; |
| 79 | } |
| 80 | $components = $plugin->findMultipleComponents($subnamespace, $componentTypeClass); |
| 81 | } |
| 82 | // find component that satisfieds predicate |
| 83 | foreach ($components as $class) { |
| 84 | $component = new $class(); |
| 85 | if ($predicate($component)) { |
| 86 | return $component; |
| 87 | } |
| 88 | } |
| 89 | Log::debug("ComponentFactory::%s: Could not find component that satisfies predicate (args = ['%s', '%s', '%s']).", __FUNCTION__, $componentTypeClass, $pluginName, get_class($predicate)); |
| 90 | return null; |
| 91 | } |
| 92 | /** |
| 93 | * @param string $function |
| 94 | * @param string $pluginName |
| 95 | * @return null|\Piwik\Plugin |
| 96 | */ |
| 97 | private static function getActivatedPlugin($function, $pluginName) |
| 98 | { |
| 99 | $pluginManager = PluginManager::getInstance(); |
| 100 | try { |
| 101 | if (!$pluginManager->isPluginActivated($pluginName)) { |
| 102 | Log::debug("ComponentFactory::%s: component for deactivated plugin ('%s') requested.", $function, $pluginName); |
| 103 | return null; |
| 104 | } |
| 105 | return $pluginManager->getLoadedPlugin($pluginName); |
| 106 | } catch (Exception $e) { |
| 107 | Log::debug($e); |
| 108 | return null; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 |