1
|
|
|
<?php namespace Anomaly\Streams\Platform\Addon; |
2
|
|
|
|
3
|
|
|
use Composer\Autoload\ClassLoader; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AddonLoader |
7
|
|
|
* |
8
|
|
|
* @link http://pyrocms.com/ |
9
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
10
|
|
|
* @author Ryan Thompson <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class AddonLoader |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The class loader instance. |
17
|
|
|
* |
18
|
|
|
* @var ClassLoader |
19
|
|
|
*/ |
20
|
|
|
protected $loader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new AddonLoader instance. |
24
|
|
|
*/ |
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
foreach (spl_autoload_functions() as $loader) { |
28
|
|
|
if ($loader[0] instanceof ClassLoader) { |
|
|
|
|
29
|
|
|
$this->loader = $loader[0]; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!$this->loader) { |
34
|
|
|
throw new \Exception("The ClassLoader could not be found."); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Load the addon. |
40
|
|
|
* |
41
|
|
|
* @param $path |
42
|
|
|
*/ |
43
|
|
|
public function load($path) |
44
|
|
|
{ |
45
|
|
|
if (file_exists($autoload = $path . '/vendor/autoload.php')) { |
46
|
|
|
|
47
|
|
|
include $autoload; |
48
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!file_exists($path . '/composer.json')) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$composer = json_decode(file_get_contents($path . '/composer.json'), true)) { |
57
|
|
|
throw new \Exception("A JSON syntax error was encountered in {$path}/composer.json"); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!array_key_exists('autoload', $composer)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach (array_get($composer['autoload'], 'psr-4', []) as $namespace => $autoload) { |
65
|
|
|
$this->loader->addPsr4($namespace, $path . '/' . $autoload, false); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach (array_get($composer['autoload'], 'psr-0', []) as $namespace => $autoload) { |
69
|
|
|
$this->loader->add($namespace, $path . '/' . $autoload, false); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach (array_get($composer['autoload'], 'files', []) as $file) { |
73
|
|
|
include($path . '/' . $file); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($classmap = array_get($composer['autoload'], 'classmap')) { |
77
|
|
|
$this->loader->addClassMap($classmap); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Register the loader. |
83
|
|
|
*/ |
84
|
|
|
public function register() |
85
|
|
|
{ |
86
|
|
|
$this->loader->register(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.