AutoloadRestorer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * @author Tom Klingenberg <[email protected]>
4
 */
5
6
namespace N98\Util;
7
8
/**
9
 * Utility class to snapshot a set of autoloaders and restore any of the snapshot if removed.
10
 *
11
 * Based on SPL autoloader.
12
 *
13
 * @package N98\Util
14
 */
15
class AutoloadRestorer
16
{
17
    /**
18
     * @var array
19
     */
20
    private $snapshot;
21
22
    public function __construct()
23
    {
24
        $this->snapshot = spl_autoload_functions();
25
    }
26
27
    /**
28
     * restore all autoload callbacks that have been unregistered
29
     */
30
    public function restore()
31
    {
32
        $unregisteredLoaders = $this->getUnregisteredLoaders();
33
34
        foreach ($unregisteredLoaders as $callback) {
35
            spl_autoload_register($callback);
36
        }
37
    }
38
39
    private function getUnregisteredLoaders()
40
    {
41
        $unregistered = array();
42
        $current      = spl_autoload_functions();
43
        foreach ($this->snapshot as $callback) {
44
            if (in_array($callback, $current, true)) {
45
                continue;
46
            }
47
            $unregistered[] = $callback;
48
        }
49
50
        return $unregistered;
51
    }
52
}
53