Completed
Push — master ( 78b3b4...1185ef )
by Fumio
02:21
created

ClassLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A unregister() 0 6 2
B load() 0 31 6
A register() 0 8 1
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Jumilla\Addomnipot\Laravel\Environment as AddonEnvironment;
6
7
class ClassLoader
8
{
9
    /**
10
     * @var static
11
     */
12
    protected static $instance;
13
14
    /**
15
     * @param array $addons
16
     */
17 2
    public static function register(AddonEnvironment $env, $addons)
18
    {
19 2
        static::$instance = new static($env, $addons);
20
21
        // TODO check addon configuration
22
23 2
        spl_autoload_register([static::$instance, 'load'], true, false);
24 2
    }
25
26
    /**
27
     */
28 1
    public static function unregister()
29
    {
30 1
        if (static::$instance) {
31 1
            spl_autoload_unregister([static::$instance, 'load']);
32 1
        }
33 1
    }
34
35
    protected $env;
36
37
    protected $addons;
38
39
    /**
40
     * @param Jumilla\Addomnipot\Laravel\Environment $env
41
     * @param array $addons
42
     */
43 3
    public function __construct(AddonEnvironment $env, array $addons)
44
    {
45 3
        $this->env = $env;
46 3
        $this->addons = $addons;
47 3
    }
48
49
    /**
50
     * @param string $className
51
     *
52
     * @return bool
53
     */
54 1
    public function load($className)
55
    {
56 1
        foreach ($this->addons as $addon) {
57 1
            $namespace = $addon->phpNamespace();
58
59 1
            $namespacePrefix = $namespace ? $namespace.'\\' : '';
60
61
            // アドオンの名前空間下のクラスでないなら
62 1
            if (!starts_with($className, $namespacePrefix)) {
63 1
                continue;
64
            }
65
66
            // 名前空間を削る
67 1
            $relativeClassName = substr($className, strlen($namespacePrefix));
68
69
            // クラスの相対パスを作成する(PSR-4)
70 1
            $relativePath = $this->env->classToPath($relativeClassName);
71
72
            // 全ディレクトリ下を探索する (PSR-4)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73 1
            foreach ($addon->config('addon.directories') as $directory) {
74 1
                $path = $addon->path($directory.'/'.$relativePath);
75 1
                if (file_exists($path)) {
76
                    require_once $path;
77
78
                    return true;
79
                }
80 1
            }
81 1
        }
82
83 1
        return false;
84
    }
85
}
86