Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

AddonLoader::load()   D

Complexity

Conditions 9
Paths 20

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 20
nop 1
dl 0
loc 37
rs 4.909
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug introduced by
The class Composer\Autoload\ClassLoader does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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