|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
This pre-boot script looks for the symphony_preboot_config path environment |
|
5
|
|
|
variable which is loaded and parsed. Currently it only supports |
|
6
|
|
|
including additional files but in future it might include other tasks. |
|
7
|
|
|
|
|
8
|
|
|
To use the pre-boot behaviour, follow these steps: |
|
9
|
|
|
|
|
10
|
|
|
1. Set `symphony_enable_preboot` to 1 either via apache envvars or .htaccess |
|
11
|
|
|
2. Set the path to the pre-boot JSON file with the `symphony_preboot_config` |
|
12
|
|
|
environment variable. |
|
13
|
|
|
3. Create the file `symphony_preboot_config` and specify files to include |
|
14
|
|
|
|
|
15
|
|
|
E.g. In the Symphony .htaccess file |
|
16
|
|
|
|
|
17
|
|
|
SetEnv symphony_enable_preboot 1 |
|
18
|
|
|
SetEnv symphony_preboot_config "/path/to/the/preboot.json" |
|
19
|
|
|
|
|
20
|
|
|
Here is an example of the pre-boot config: |
|
21
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
"includes": [ |
|
24
|
|
|
"manifest/preboot/01_test.php", |
|
25
|
|
|
"/var/www/html/symphony/manifest/preboot/02_test.php" |
|
26
|
|
|
] |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
Note, at this stage the Symphony core has not been initialised. There is |
|
30
|
|
|
no database connection and the main autoloader has not been included. |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
declare(strict_types=1); |
|
34
|
|
|
|
|
35
|
|
|
use pointybeard\Helpers\Functions\Json; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$isPrebootEnabled = false != getenv('symphony_enable_preboot') |
|
|
|
|
|
|
38
|
|
|
? (bool) intval(getenv('symphony_enable_preboot')) |
|
|
|
|
|
|
39
|
|
|
: false |
|
40
|
|
|
; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
// If pre-booting is not enabled, then just return. |
|
43
|
|
|
if (false == $isPrebootEnabled) { |
|
|
|
|
|
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/* |
|
48
|
|
|
* Helper method for creating defines from environment variables |
|
49
|
|
|
* @param string $name The name of the environment variables |
|
50
|
|
|
* @param string $default Optional default value if hasn't been set |
|
51
|
|
|
* @param string $customDefineName Optonally set the name of the define. |
|
52
|
|
|
* Default is to use $name |
|
53
|
|
|
* @throws Exception If $name is not a valid environment variable |
|
54
|
|
|
* and there is no $default value set, an |
|
55
|
|
|
* exception is thrown |
|
56
|
|
|
*/ |
|
57
|
|
|
if (!function_exists('defineFromEnv')) { |
|
58
|
|
|
function defineFromEnv(string $name, string $default = null, string $customDefineName = null): void |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$value = false !== getenv($name) |
|
61
|
|
|
? getenv($name) |
|
|
|
|
|
|
62
|
|
|
: $default |
|
63
|
|
|
; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
if (null == $value) { |
|
|
|
|
|
|
66
|
|
|
throw new Exception("Environment variable {$name} has not been set and there is no default value. Set {$name} with either `export {$name}=xxx` or a line in Apache envvars."); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
define( |
|
70
|
|
|
(null != $customDefineName ? $customDefineName : $name), |
|
|
|
|
|
|
71
|
|
|
$value |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
defineFromEnv('symphony_preboot_config', null, 'SYMPHONY_PREBOOT_CONFIG'); |
|
78
|
|
|
} catch (Exception $ex) { |
|
79
|
|
|
// There was no environment variable set, so we dont need to do anything |
|
80
|
|
|
// else. |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
try { |
|
85
|
|
|
// Load the pre-boot config file. Expected to be valid JSON. |
|
86
|
|
|
$config = Json\json_decode_file(SYMPHONY_PREBOOT_CONFIG); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
// Make sure 'includes' item is set to avoid errors further down |
|
89
|
|
|
$config->includes = $config->includes ?? []; |
|
90
|
|
|
|
|
91
|
|
|
// Check each include to make sure its valid |
|
92
|
|
|
foreach ($config->includes as $ii => $path) { |
|
93
|
|
|
$path = realpath($path); |
|
94
|
|
|
|
|
95
|
|
|
if (false == $path) { |
|
|
|
|
|
|
96
|
|
|
throw new Exception('Pre-boot config contains an invalid include: %s'.$config->includes[$ii]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$config->includes[$ii] = $path; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Check for duplicates |
|
103
|
|
|
if (count($config->includes) > count(array_unique($config->includes))) { |
|
104
|
|
|
throw new Exception('Duplicate include detected in pre-boot config '.SYMPHONY_PREBOOT_CONFIG); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// All checks have passed. Include each file. |
|
108
|
|
|
foreach ($config->includes as $path) { |
|
109
|
|
|
include $path; |
|
110
|
|
|
} |
|
111
|
|
|
} catch (Exception $ex) { |
|
112
|
|
|
// Failed to load the pre-boot config. We need to handle this here |
|
113
|
|
|
echo $ex->getMessage().PHP_EOL; |
|
114
|
|
|
exit; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// All done. Resume rest of the Symphony boot process |
|
118
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: