Completed
Push — master ( 748c89...d24f56 )
by Schlaefer
06:53 queued 03:34
created

PluginDirectory::newPluginInstance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
nc 3
nop 1
dl 0
loc 17
c 0
b 0
f 0
cc 3
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
1
<?php
2
/**
3
 * @author PhileCMS
4
 * @link https://philecms.github.io/
5
 * @license http://opensource.org/licenses/MIT
6
 * @package Phile\Plugin
7
 */
8
9
namespace Phile\Plugin;
10
11
use Phile\Exception\PluginException;
12
13
/**
14
 * Represents a dire directory with plugin folders
15
 */
16
class PluginDirectory
17
{
18
    /**
19
     * File path to directory
20
     *
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param string $path
29
     */
30 38
    public function __construct(string $path)
31
    {
32 38
        $this->path = $path;
33 38
        spl_autoload_register([$this, 'autoload']);
34 38
    }
35
36
    /**
37
     * Gets directory path
38
     *
39
     * @return string path
40
     */
41 36
    public function getPath(): string
42
    {
43 36
        return $this->path;
44
    }
45
46
    /**
47
     * Tries to create new a Plugin class from plugin in directory
48
     *
49
     * @param string $pluginKey
50
     * @return AbstractPlugin|null
51
     */
52 34
    public function newPluginInstance(string $pluginKey): ?AbstractPlugin
53
    {
54 34
        list($vendor, $pluginName) = explode('\\', $pluginKey);
55
        // uppercase first letter convention
56 34
        $className = 'Phile\\Plugin\\' . ucfirst($vendor) . '\\' . ucfirst($pluginName) . '\\Plugin';
57 34
        if (!file_exists($this->filenameForClass($className))) {
58 1
            return null;
59
        };
60
61 33
        $plugin = new $className;
62 33
        if (($plugin instanceof AbstractPlugin) === false) {
63 1
            throw new PluginException(
64 1
                "the plugin '{$pluginKey}' is not an instance of \\Phile\\Plugin\\AbstractPlugin",
65 1
                1398536526
66
            );
67
        }
68 32
        return $plugin;
69
    }
70
71
    /**
72
     * Class auto-loader plugin namespace
73
     *
74
     * @param string $className
75
     * @return void
76
     */
77 33
    public function autoload(string $className): void
78
    {
79 33
        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...
80 31
            return;
81
        }
82 3
        $filename = $this->filenameForClass($className);
83 3
        if (file_exists($filename)) {
84 3
            include $filename;
85
        }
86 3
    }
87
88
    /**
89
     * Creates file-path to class-file in plugin directory
90
     *
91
     * @param string $className
92
     * @return string the file path
93
     */
94 35
    protected function filenameForClass(string $className): string
95
    {
96 35
        $className = str_replace('Phile\\Plugin\\', '', $className);
97 35
        $classNameParts = explode('\\', $className);
98 35
        $pluginVendor = lcfirst(array_shift($classNameParts));
99 35
        $pluginName = lcfirst(array_shift($classNameParts));
100 35
        $classPath = array_merge(
101 35
            [$pluginVendor, $pluginName, 'Classes'],
102 35
            $classNameParts
103
        );
104
105 35
        return $this->path . implode('/', $classPath) . '.php';
106
    }
107
}
108