Completed
Push — master ( 880b7a...33b151 )
by Tom
09:34 queued 06:03
created

AutoloadHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 2
cbo 0
dl 0
loc 142
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 11 3
A register() 0 5 1
A unregister() 0 5 1
B __invoke() 0 19 5
A getCleanupCallback() 0 11 2
A reset() 0 5 1
A isEnabled() 0 4 1
A setEnabled() 0 4 1
A disable() 0 4 1
A enable() 0 4 1
A setCallback() 0 4 1
1
<?php
2
/**
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Util;
9
10
/**
11
 * Autloader with self-registration, de-registration, muting and implementation switching
12
 *
13
 * @package N98\Util
14
 */
15
final class AutoloadHandler
16
{
17
    /**
18
     * Throw exception if the autoload implementation is not callable (default). If no exception is thrown,
19
     * autoload callback is just ignored
20
     */
21
    const NO_EXCEPTION = 1;
22
23
    /**
24
     *
25
     */
26
    const NO_AUTO_REGISTER = 2;
27
28
    /**
29
     * @var integer
30
     */
31
    private $flags;
32
33
    private $callback;
34
35
    private $splRegistered;
36
37
    /**
38
     * @var bool
39
     */
40
    private $enabled;
41
42
    /**
43
     * @param $callback
44
     * @param integer $flags [optional]
45
     * @return AutoloadHandler
46
     */
47
    public static function create($callback, $flags = null)
48
    {
49
        return new self($callback, $flags);
50
    }
51
52
    /**
53
     * AutoloadHandler constructor.
54
     *
55
     * @param $callback
56
     * @param integer $flags [optional]
57
     */
58
    public function __construct($callback, $flags = null)
59
    {
60
        if (null === $flags) {
61
            $flags = 0;
62
        }
63
64
        $this->flags = $flags;
65
        $this->enabled = true;
66
        $this->callback = $callback;
67
        $this->flags & self::NO_AUTO_REGISTER || $this->register();
68
    }
69
70
    public function register()
71
    {
72
        spl_autoload_register($this);
73
        $this->splRegistered = true;
74
    }
75
76
    public function unregister()
77
    {
78
        spl_autoload_unregister($this);
79
        $this->splRegistered = false;
80
    }
81
82
    public function __invoke($className)
83
    {
84
        if (!$this->splRegistered) {
85
            return false;
86
        }
87
88
        if (!$this->enabled) {
89
            return false;
90
        }
91
92
        if (!is_callable($this->callback)) {
93
            if ($this->flags & self::NO_EXCEPTION) {
94
                return false;
95
            }
96
            throw new \BadMethodCallException('Autoload callback is not callable');
97
        }
98
99
        return call_user_func($this->callback, $className);
100
    }
101
102
    public function getCleanupCallback()
103
    {
104
        $self = (object) array('ref' => $this);
105
106
        return function () use ($self) {
107
            if (isset($self->ref)) {
108
                $self->ref->reset();
109
                unset($self->ref);
110
            }
111
        };
112
    }
113
114
    /**
115
     * Unregister from SPL Stack and destroy callback reference.
116
     */
117
    public function reset()
118
    {
119
        $this->unregister();
120
        $this->callback = null;
121
    }
122
123
    /**
124
     * @return boolean
125
     */
126
    public function isEnabled()
127
    {
128
        return $this->enabled;
129
    }
130
131
    /**
132
     * @param boolean $flagEnabled
133
     */
134
    public function setEnabled($flagEnabled)
135
    {
136
        $this->enabled = (bool) $flagEnabled;
137
    }
138
139
    public function disable()
140
    {
141
        $this->enabled = false;
142
    }
143
144
    public function enable()
145
    {
146
        $this->enabled = true;
147
    }
148
149
    /**
150
     * @param mixed $callback
151
     */
152
    public function setCallback($callback)
153
    {
154
        $this->callback = $callback;
155
    }
156
}
157