1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea Module |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @copyright 2017-2018 LibreWorks contributors |
20
|
|
|
* @license Apache-2.0 |
21
|
|
|
*/ |
22
|
|
|
namespace Caridea\Module; |
23
|
|
|
|
24
|
|
|
use Caridea\Container\Properties; |
25
|
|
|
use Caridea\Container\Objects; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A bootstrapper that reads configuration and creates backend and frontend containers. |
29
|
|
|
* |
30
|
|
|
* Once constructed, this object is immutable. |
31
|
|
|
*/ |
32
|
|
|
class System extends Configuration |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Objects The backend container |
36
|
|
|
*/ |
37
|
|
|
protected $backend; |
38
|
|
|
/** |
39
|
|
|
* @var Objects The frontend container |
40
|
|
|
*/ |
41
|
|
|
protected $frontend; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a new System. |
45
|
|
|
* |
46
|
|
|
* This constructor expects an array full of class names in the `$modules` |
47
|
|
|
* parameter. Each class name *must* extend `Caridea\Module\Module` or an |
48
|
|
|
* `UnexpectedValueException` will be thrown. |
49
|
|
|
* |
50
|
|
|
* @param array<string> $modules An array of module class names |
51
|
|
|
* @param array<string,mixed> $config The system configuration |
52
|
|
|
* @throws \UnexpectedValueException if a module class doesn't extend `Minotaur\Module` |
53
|
|
|
*/ |
54
|
1 |
|
public function __construct(array $modules, array $config) |
55
|
|
|
{ |
56
|
1 |
|
parent::__construct($modules, $config); |
57
|
1 |
|
$this->backend = $this->createBackendContainer($this->config); |
58
|
1 |
|
$this->frontend = $this->createFrontendContainer($this->backend); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
private function createBackendContainer(Properties $parent): Objects |
62
|
|
|
{ |
63
|
1 |
|
$builder = Objects::builder(); |
64
|
1 |
|
foreach ($this->modules as $module) { |
65
|
1 |
|
$module->setupBackend($builder, $parent); |
66
|
|
|
} |
67
|
1 |
|
return $builder->build($parent); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
private function createFrontendContainer(Objects $parent): Objects |
71
|
|
|
{ |
72
|
1 |
|
$builder = Objects::builder(); |
73
|
1 |
|
foreach ($this->modules as $module) { |
74
|
1 |
|
$module->setupFrontend($builder, $this->config); |
75
|
|
|
} |
76
|
1 |
|
return $builder->build($parent); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Gets the container with backend classes. |
81
|
|
|
* |
82
|
|
|
* @return \Caridea\Container\Objects The backend container |
83
|
|
|
*/ |
84
|
1 |
|
public function getBackendContainer(): Objects |
85
|
|
|
{ |
86
|
1 |
|
return $this->backend; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets the container with frontend classes. |
91
|
|
|
* |
92
|
|
|
* @return \Caridea\Container\Objects The frontend container |
93
|
|
|
*/ |
94
|
1 |
|
public function getFrontendContainer(): Objects |
95
|
|
|
{ |
96
|
1 |
|
return $this->frontend; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|