PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.1.4
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.1.4
5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Plugin / ComponentFactory.php
matomo / app / core / Plugin Last commit date
ConsoleCommand 2 years ago Dimension 2 years ago API.php 2 years ago AggregatedMetric.php 2 years ago ArchivedMetric.php 2 years ago Archiver.php 2 years ago Categories.php 2 years ago ComponentFactory.php 2 years ago ComputedMetric.php 2 years ago ConsoleCommand.php 2 years ago Controller.php 2 years ago ControllerAdmin.php 2 years ago Dependency.php 2 years ago LogTablesProvider.php 2 years ago Manager.php 2 years ago Menu.php 2 years ago MetadataLoader.php 2 years ago Metric.php 2 years ago PluginException.php 2 years ago ProcessedMetric.php 2 years ago ReleaseChannels.php 2 years ago Report.php 2 years ago ReportsProvider.php 2 years ago RequestProcessors.php 2 years ago Segment.php 2 years ago SettingsProvider.php 2 years ago Tasks.php 2 years ago ThemeStyles.php 2 years ago ViewDataTable.php 2 years ago Visualization.php 2 years 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