Passed
Push — 5.0 ( 6887da...0ee044 )
by Marc André
02:11
created

Core::get()   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\ControllerReflection\AlreadyInitialisedException;
22
23
24
/**
25
 * Core class for Cervo.
26
 *
27
 * @author Marc André Audet <[email protected]>
28
 */
29
final class Core
30
{
31
    /** @var bool */
32
    private $isInit = false;
33
34
    /** @var Context|null */
35
    private $context = null;
36
37
    /** @var Core|null */
38
    private static $core = null;
39
40
    public function __construct(?BaseConfig $config = null)
41
    {
42
        $this->context = new Context($config);
43
        self::$core = $this;
44
    }
45
46
    public static function get(): ?Core
47
    {
48
        return self::$core;
49
    }
50
51
    public function start()
52
    {
53
        if ($this->isInit === true) {
54
            throw new AlreadyInitialisedException;
55
        }
56
57
        $this->isInit = true;
58
59
        /** @var Events $events */
60
        $events = $this->getContext()->getSingletons()->get(Events::class);
61
62
        /** @var Router $router */
63
        $router = $this->getContext()->getSingletons()->get(Router::class);
64
65
        foreach ($this->getContext()->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) {
66
            $events->loadPath($path);
67
            $router->loadPath($path);
68
        }
69
70
        $events->fire('Cervo/System/Before');
71
72
        $route = $router->dispatch();
73
74
        $events->fire('Cervo/Route/Before');
75
        (new ControllerReflection($this->getContext(), $route))();
0 ignored issues
show
Bug introduced by
It seems like $this->getContext() can also be of type null; however, parameter $context of Cervo\ControllerReflection::__construct() does only seem to accept Cervo\Context, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        (new ControllerReflection(/** @scrutinizer ignore-type */ $this->getContext(), $route))();
Loading history...
76
        $events->fire('Cervo/Route/After');
77
78
        $events->fire('Cervo/System/After');
79
    }
80
81
    public function getContext(): ?Context
82
    {
83
        return $this->context;
84
    }
85
}
86