Passed
Push — 5.0 ( c16c04...6887da )
by Marc André
02:29
created

Context::getAutoloader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Cervo package.
5
 *
6
 * Copyright (c) 2010-2019 Nevraxe inc. & Marc André Audet <[email protected]>.
7
 *
8
 * @package   Cervo
9
 * @author    Marc André Audet <[email protected]>
10
 * @copyright 2010 - 2019 Nevraxe inc. & Marc André Audet
11
 * @license   See LICENSE.md  MIT
12
 * @link      https://github.com/Nevraxe/Cervo
13
 * @since     5.0.0
14
 */
15
16
declare(strict_types=1);
17
18
namespace Cervo;
19
20
use Cervo\Config\BaseConfig;
21
use Cervo\Exceptions\Router\InvalidProviderException;
22
use Cervo\Interfaces\ProviderInterface;
23
use Cervo\Utils\ClassUtils;
24
25
/**
26
 * Context class for Cervo.
27
 *
28
 * @author Marc André Audet <[email protected]>
29
 */
30
final class Context
31
{
32
    private $config;
33
    private $modulesManager;
34
    private $singletons;
35
36
    /**
37
     * Context constructor.
38
     *
39
     * @param BaseConfig|null $config
40
     */
41
    public function __construct(?BaseConfig $config = null) {
42
43
        $this->config = $config ?? new BaseConfig();
44
45
        $this->modulesManager = new ModulesManager();
46
        $this->singletons = new Singletons($this);
47
48
    }
49
50
    /**
51
     * Register and run a Provider class
52
     *
53
     * @param string $providerClass The Provider class
54
     *
55
     * @return Context
56
     */
57
    public function register(string $providerClass): self
58
    {
59
        if (!ClassUtils::implements($providerClass, ProviderInterface::class)) {
60
            throw new InvalidProviderException;
61
        }
62
63
        (new $providerClass($this))();
64
65
        return $this;
66
    }
67
68
    public function getConfig(): BaseConfig
69
    {
70
        return $this->config;
71
    }
72
73
    public function getModulesManager(): ModulesManager
74
    {
75
        return $this->modulesManager;
76
    }
77
78
    public function getSingletons(): Singletons
79
    {
80
        return $this->singletons;
81
    }
82
}
83