Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

PluginRepository::autoload()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Phile\Plugin;
4
5
use Phile\Exception\PluginException;
6
7
/**
8
 * Class PluginRepository manages plugin loading
9
 *
10
 * @author  PhileCMS
11
 * @link    https://philecms.com
12
 * @license http://opensource.org/licenses/MIT
13
 * @package Phile\Core
14
 */
15
class PluginRepository
16
{
17
18
    /**
19
 * @var array of AbstractPlugin
20
*/
21
    protected $plugins = [];
22
23
    /**
24
 * @var array errors during load; keys: 'message' and 'code'
25
*/
26
    protected $loadErrors = [];
27
28
    /**
29
     * get load errors
30
     *
31
     * @return array
32
     */
33 4
    public function getLoadErrors()
34
    {
35 4
        return $this->loadErrors;
36
    }
37
38
    /**
39
     * loads all activated plugins from $settings
40
     *
41
     * @param  array $settings plugin-settings
42
     * @return array of AbstractPlugin
43
     * @throws PluginException
44
     */
45 4
    public function loadAll($settings)
46
    {
47 4
        $this->reset();
48 4
        foreach ($settings as $pluginKey => $config) {
49 4
            if (!isset($config['active']) || !$config['active']) {
50 1
                continue;
51
            }
52
            try {
53 4
                $this->plugins[$pluginKey] = $this->load($pluginKey);
54 1
            } catch (PluginException $e) {
55 1
                $this->loadErrors[] = [
56 1
                'message' => $e->getMessage(),
57 4
                'code' => $e->getCode()
58
                ];
59
            }
60
        }
61 4
        return $this->plugins;
62
    }
63
64
    /**
65
     * load and return single plugin
66
     *
67
     * @param  $pluginKey
68
     * @return AbstractPlugin
69
     * @throws PluginException
70
     */
71 4
    protected function load($pluginKey)
72
    {
73 4
        list($vendor, $pluginName) = explode('\\', $pluginKey);
74
        // uppercase first letter convention
75 4
        $pluginClassName = '\\Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin';
76
77 4
        if (!class_exists($pluginClassName)) {
78 1
            throw new PluginException(
79 1
                "the plugin '{$pluginKey}' could not be loaded!",
80 1
                1398536479
81
            );
82
        }
83
84
        /**
85
 * @var \Phile\Plugin\AbstractPlugin $plugin
86
*/
87 3
        $plugin = new $pluginClassName;
88 3
        if (($plugin instanceof AbstractPlugin) === false) {
89
            throw new PluginException(
90
                "the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin",
91
                1398536526
92
            );
93
        }
94
95 3
        $plugin->initializePlugin($pluginKey);
0 ignored issues
show
Deprecated Code introduced by
The method Phile\Plugin\AbstractPlugin::initializePlugin() has been deprecated with message: since 1.5.1 will be declared 'final'

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96 3
        return $plugin;
97
    }
98
99
    /**
100
     * clear out repository
101
     */
102 4
    protected function reset()
103
    {
104 4
        $this->loadErrors = [];
105 4
        $this->plugins = [];
106 4
    }
107
108
    /**
109
     * auto-loader plugin namespace
110
     *
111
     * @param $className
112
     */
113 3
    public static function autoload($className)
114
    {
115 3
        if (strpos($className, "Phile\\Plugin\\") !== 0) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Phile\\Plugin\\ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
116 1
            return;
117
        }
118
119 2
        $className = substr($className, 13);
120 2
        $classNameParts = explode('\\', $className);
121 2
        $pluginVendor = lcfirst(array_shift($classNameParts));
122 2
        $pluginName = lcfirst(array_shift($classNameParts));
123 2
        $classPath = array_merge(
124 2
            [$pluginVendor, $pluginName, 'Classes'],
125 2
            $classNameParts
126
        );
127
128 2
        $fileName = PLUGINS_DIR . implode(DIRECTORY_SEPARATOR, $classPath) . '.php';
129 2
        if (file_exists($fileName)) {
130 1
            include_once $fileName;
131
        }
132 2
    }
133
}
134