1
|
|
|
<?php |
2
|
|
|
namespace samsonphp\core; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\RequestInterface; |
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use samsonframework\core\SystemInterface; |
7
|
|
|
use samsonphp\config\Scheme; |
8
|
|
|
use samsonphp\core\exception\CannotLoadModule; |
9
|
|
|
use samsonphp\event\Event; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Core |
13
|
|
|
* |
14
|
|
|
* @package samsonphp/core |
15
|
|
|
* @author Vitaly Iegorov <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Core implements CoreInterface |
18
|
|
|
{ |
19
|
|
|
/** @var string Current system environment */ |
20
|
|
|
protected $environment; |
21
|
|
|
|
22
|
|
|
/** @var Module[] Loaded modules collection */ |
23
|
|
|
protected $modules = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Core constructor |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
// Fire core creation event |
31
|
|
|
Event::fire(self::E_CREATED, [&$this]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Change current system working environment. |
36
|
|
|
* |
37
|
|
|
* @param string $environment Environment identifier |
38
|
|
|
* |
39
|
|
|
* @return $this Chaining |
40
|
|
|
*/ |
41
|
|
|
public function environment($environment = Scheme::BASE) |
42
|
|
|
{ |
43
|
|
|
// Fire core environment change |
44
|
|
|
Event::fire(self::E_ENVIRONMENT, [&$this, &$environment]); |
45
|
|
|
|
46
|
|
|
$this->environment = $environment; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Load module. |
53
|
|
|
* |
54
|
|
|
* @param ModuleInterface $instance Module ancestor for loading |
55
|
|
|
* |
56
|
|
|
* @param string|null $alias Module alias |
57
|
|
|
* |
58
|
|
|
* @return $this Chaining |
59
|
|
|
* |
60
|
|
|
* @throws CannotLoadModule On alias duplication |
61
|
|
|
*/ |
62
|
|
|
public function load($instance, $alias = null) |
63
|
|
|
{ |
64
|
|
|
// If no alias is passed - use fully qualified class name |
65
|
|
|
$alias = $alias ?: get_class($instance); |
66
|
|
|
|
67
|
|
|
// Check for duplicating aliases |
68
|
|
|
if (array_key_exists($alias, $this->modules)) { |
69
|
|
|
throw new CannotLoadModule($alias.' - alias already in use'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Fire core before module loading |
73
|
|
|
Event::fire(self::E_BEFORE_LOADED, [&$this, &$instance, &$alias]); |
74
|
|
|
|
75
|
|
|
// Store module instance by alias or class name |
76
|
|
|
$this->modules[$alias] = $instance; |
77
|
|
|
|
78
|
|
|
// Fire core before module loading |
79
|
|
|
Event::fire(self::E_AFTER_LOADED, [&$this, &$instance, &$alias]); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Process request. |
86
|
|
|
* Method supports PSR middleware approach. |
87
|
|
|
* |
88
|
|
|
* @param RequestInterface $request Request instance |
89
|
|
|
* @param ResponseInterface $response Response instance |
90
|
|
|
* @param callable|null $next Next callable middleware |
91
|
|
|
* |
92
|
|
|
* @return ResponseInterface Processed response instance |
93
|
|
|
*/ |
94
|
|
|
public function process(RequestInterface $request, ResponseInterface $response, callable $next = null) |
95
|
|
|
{ |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|