|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* Validation class for an app instance. |
|
6
|
|
|
* Called before booting, to ensure all required properties are set. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Glynn Quelch <[email protected]> |
|
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
10
|
|
|
* @package PinkCrab\Perique |
|
11
|
|
|
* @since 0.4.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace PinkCrab\Perique\Application; |
|
15
|
|
|
|
|
16
|
|
|
use PinkCrab\Perique\Application\App; |
|
17
|
|
|
use Reflection; |
|
18
|
|
|
use ReflectionProperty; |
|
19
|
|
|
|
|
20
|
|
|
final class App_Validation { |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
public const ERROR_MESSAGE_TEMPLATE = '%s was not set in App'; |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
public const ERROR_MESSAGE_APP_BOOTED = 'App already booted'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Required properties |
|
29
|
|
|
* Key is the property and value is if "static" |
|
30
|
|
|
* |
|
31
|
|
|
* @var array<string,bool> |
|
32
|
|
|
*/ |
|
33
|
|
|
private array $required_properties = array( |
|
34
|
|
|
'container' => true, |
|
35
|
|
|
'app_config' => true, |
|
36
|
|
|
'module_manager' => false, |
|
37
|
|
|
'loader' => false, |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** @var array<string> */ |
|
41
|
|
|
public array $errors = array(); |
|
42
|
|
|
|
|
43
|
|
|
/** @var App */ |
|
44
|
|
|
private App $app; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct( App $app ) { |
|
47
|
|
|
$this->app = $app; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Checks all properties are set and app isn't already booted |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function validate(): bool { |
|
56
|
|
|
$this->already_booted(); |
|
57
|
|
|
$this->validate_properties_set(); |
|
58
|
|
|
return count( $this->errors ) === 0; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Check all properties are not default values |
|
63
|
|
|
* Sets an entry in the error array if not set. |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
private function validate_properties_set(): void { |
|
68
|
|
|
foreach ( $this->required_properties as $property => $is_static ) { |
|
69
|
|
|
$property_reflection = new ReflectionProperty( $this->app, $property ); |
|
70
|
|
|
$property_reflection->setAccessible( true ); |
|
71
|
|
|
if ( empty( $property_reflection->getValue( $this->app ) ) ) { |
|
72
|
|
|
$this->errors[] = \sprintf( self::ERROR_MESSAGE_TEMPLATE, $property ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if the app has already been booted. |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
private function already_booted(): void { |
|
83
|
|
|
if ( $this->app->is_booted() === true ) { |
|
84
|
|
|
$this->errors[] = self::ERROR_MESSAGE_APP_BOOTED; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|