Autoloader::uninstall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
use Stratadox\PhpGenerics\Dissector\Error\InvalidTypeArgument;
9
use Stratadox\PhpGenerics\Dissector\MagicTypeDissector;
10
use Stratadox\PhpGenerics\Dissector\TypeDissector;
11
use function spl_autoload_register;
12
use function spl_autoload_unregister;
13
14
class Autoloader
15
{
16
    /** @var TypeDissector */
17
    private $dissector;
18
    /** @var Loader */
19
    private $loader;
20
    /** @var LoggerInterface */
21
    private $log;
22
23
    public function __construct(
24
        TypeDissector $dissector,
25
        Loader $loader,
26
        LoggerInterface $log
27
    ) {
28
        $this->dissector = $dissector;
29
        $this->loader = $loader;
30
        $this->log = $log;
31
    }
32
33
    /** @throws AnnotationException */
34
    public static function default(): self
35
    {
36
        return new self(
37
            MagicTypeDissector::default(),
38
            Loader::default(),
39
            new NullLogger()
40
        );
41
    }
42
43
    public function install(): void
44
    {
45
        spl_autoload_register([$this, 'load']);
46
    }
47
48
    public function uninstall(): void
49
    {
50
        spl_autoload_unregister([$this, 'load']);
51
    }
52
53
    public function load(string $class): void
54
    {
55
        try {
56
            $this->loader->generate($this->dissector->typesToGenerate($class, 2));
57
        } catch (InvalidTypeArgument $exception) {
58
            $this->log->info(
59
                'Not generically auto loading: ' . $exception->getMessage()
60
            );
61
        }
62
    }
63
}
64