Passed
Push — develop ( 91c1d2...446210 )
by Schlaefer
41s
created

PluginRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
     * @var directory plug-in root directory
30
     */
31
    protected $directory;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param string $directory root directory for plug-in folder
37
     */
38 28
    public function __construct($directory)
39
    {
40 28
        $this->directory = $directory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $directory of type string is incompatible with the declared type object<Phile\Plugin\directory> of property $directory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 28
        spl_autoload_register([$this, 'autoload']);
42 28
    }
43
44
    /**
45
     * get load errors
46
     *
47
     * @return array
48
     */
49 28
    public function getLoadErrors()
50
    {
51 28
        return $this->loadErrors;
52
    }
53
54
    /**
55
     * loads all activated plugins from $settings
56
     *
57
     * @param  array $settings plugin-settings
58
     * @return array of AbstractPlugin
59
     * @throws PluginException
60
     */
61 28
    public function loadAll($settings)
62
    {
63 28
        $this->reset();
64 28
        foreach ($settings as $pluginKey => $config) {
65 28
            if (!isset($config['active']) || !$config['active']) {
66 1
                continue;
67
            }
68
            try {
69 28
                $this->plugins[$pluginKey] = $this->load($pluginKey);
70 1
            } catch (PluginException $e) {
71 1
                $this->loadErrors[] = [
72 1
                'message' => $e->getMessage(),
73 28
                'code' => $e->getCode()
74
                ];
75
            }
76
        }
77 28
        return $this->plugins;
78
    }
79
80
    /**
81
     * load and return single plugin
82
     *
83
     * @param  $pluginKey
84
     * @return AbstractPlugin
85
     * @throws PluginException
86
     */
87 28
    protected function load($pluginKey)
88
    {
89 28
        list($vendor, $pluginName) = explode('\\', $pluginKey);
90
        // uppercase first letter convention
91 28
        $pluginClassName = '\\Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin';
92
93 28
        if (!class_exists($pluginClassName)) {
94 1
            throw new PluginException(
95 1
                "the plugin '{$pluginKey}' could not be loaded!",
96 1
                1398536479
97
            );
98
        }
99
100
        /**
101
 * @var \Phile\Plugin\AbstractPlugin $plugin
102
*/
103 27
        $plugin = new $pluginClassName;
104 27
        if (($plugin instanceof AbstractPlugin) === false) {
105
            throw new PluginException(
106
                "the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin",
107
                1398536526
108
            );
109
        }
110
111 27
        $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...
112 27
        return $plugin;
113
    }
114
115
    /**
116
     * clear out repository
117
     */
118 28
    protected function reset()
119
    {
120 28
        $this->loadErrors = [];
121 28
        $this->plugins = [];
122 28
    }
123
124
    /**
125
     * auto-loader plugin namespace
126
     *
127
     * @param $className
128
     */
129 5
    public function autoload($className)
130
    {
131 5
        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...
132 2
            return;
133
        }
134
135 4
        $className = substr($className, 13);
136 4
        $classNameParts = explode('\\', $className);
137 4
        $pluginVendor = lcfirst(array_shift($classNameParts));
138 4
        $pluginName = lcfirst(array_shift($classNameParts));
139 4
        $classPath = array_merge(
140 4
            [$pluginVendor, $pluginName, 'Classes'],
141 4
            $classNameParts
142
        );
143
144 4
        $fileName = $this->directory . implode(DS, $classPath) . '.php';
145 4
        if (file_exists($fileName)) {
146 3
            include_once $fileName;
147
        }
148 4
    }
149
}
150