Completed
Push — 5.0 ( d49403...9430f0 )
by Marc André
01:47
created

Autoloader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 20 4
1
<?php
2
3
4
/**
5
 *
6
 * Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without modification, are
9
 * permitted provided that the following conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above copyright notice, this list of
12
 *       conditions and the following disclaimer.
13
 *
14
 *   2. Redistributions in binary form must reproduce the above copyright notice, this list
15
 *       of conditions and the following disclaimer in the documentation and/or other materials
16
 *       provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 */
30
31
32
namespace Cervo;
33
34
35
/**
36
 * Autoloader for Cervo.
37
 *
38
 * @author Marc André Audet <[email protected]>
39
 */
40
final class Autoloader
41
{
42
    /** @var Context The current context */
43
    private $context;
44
45
    /**
46
     * Autoloader constructor.
47
     *
48
     * @param Context $context
49
     */
50
    public function __construct(Context $context)
51
    {
52
        $this->context = $context;
53
54
        spl_autoload_register($this);
55
    }
56
57
    public function __invoke($class)
58
    {
59
        $ex = explode('\\', $class);
60
61
        if (count($ex) < 3) {
62
            return;
63
        }
64
65
        $path = $this->context->getModulesManager()->getModulePath($ex[0], $ex[1]);
66
67
        if (strlen($path) <= 0) {
68
            return;
69
        }
70
71
        $file_path = $path . \DIRECTORY_SEPARATOR . implode(\DIRECTORY_SEPARATOR, array_slice($ex, 2)) . '.php';
72
73
        if (file_exists($file_path)) {
74
            require $file_path;
75
        }
76
    }
77
}
78