Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiosmosis <benaka@piwik.pro>2014-09-15 22:10:16 +0400
committerdiosmosis <benaka@piwik.pro>2014-09-15 22:10:16 +0400
commit718c1bb9c6bb94b8342c82fe99486ad88dffd041 (patch)
tree6164eb0aeced9df2069c5f4ba2afa8cd6d31a803 /core/Plugin/ComponentFactory.php
parent451f607b3d69e80092426e054d4124e037a77ba8 (diff)
Refs #6078, extract factory logic from Report::factory and move to new ComponentFactory utility class and reuse in Dimension to allow creating Dimension instances by human readable string IDs.
Diffstat (limited to 'core/Plugin/ComponentFactory.php')
-rw-r--r--core/Plugin/ComponentFactory.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/core/Plugin/ComponentFactory.php b/core/Plugin/ComponentFactory.php
new file mode 100644
index 0000000000..02cb6083e1
--- /dev/null
+++ b/core/Plugin/ComponentFactory.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+namespace Piwik\Plugin;
+
+use Piwik\Log;
+use Piwik\Plugin\Manager as PluginManager;
+use Exception;
+
+/**
+ * Factory class with methods to find and instantiate Plugin components.
+ */
+class ComponentFactory
+{
+ /**
+ * Create a component instance that exists within a specific plugin. Uses the component's
+ * unqualified class name and expected base type.
+ *
+ * This method will only create a class if it is located within the component type's
+ * associated subdirectory.
+ *
+ * @param string $pluginName The name of the plugin the component is expected to belong to,
+ * eg, `'UserSettings'`.
+ * @param string $componentClassSimpleName The component's class name w/o namespace, eg,
+ * `"GetKeywords"`.
+ * @param string $componentTypeClass The fully qualified class name of the component type, eg,
+ * `"Piwik\Plugin\Report"`.
+ * @return mixed|null A new instance of the desired component or null if not found. If the
+ * plugin is not loaded or activated or the component is not located in
+ * in the sub-namespace specified by `$componentTypeClass::COMPONENT_SUBNAMESPACE`,
+ * this method will return null.
+ */
+ public static function factory($pluginName, $componentClassSimpleName, $componentTypeClass)
+ {
+ if (empty($pluginName) || empty($componentClassSimpleName)) {
+ return null;
+ }
+
+ $pluginManager = PluginManager::getInstance();
+
+ try {
+ if (!$pluginManager->isPluginActivated($pluginName)) {
+ return null;
+ }
+
+ $plugin = $pluginManager->getLoadedPlugin($pluginName);
+ } catch (Exception $e) {
+ Log::debug($e);
+
+ return null;
+ }
+
+ $subnamespace = $componentTypeClass::COMPONENT_SUBNAMESPACE;
+ $desiredComponentClass = 'Piwik\\Plugins\\' . $pluginName . '\\' . $subnamespace . '\\' . $componentClassSimpleName;
+
+ $components = $plugin->findMultipleComponents($subnamespace, $componentTypeClass);
+ foreach ($components as $class) {
+ if ($class == $desiredComponentClass) {
+ return new $class();
+ }
+ }
+ return null;
+ }
+} \ No newline at end of file