AutoloadRestorer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A restore() 0 8 2
A getUnregisteredLoaders() 0 13 3
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