1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-kernel |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Kernel |
8
|
|
|
* @subpackage Apparat\Kernel\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Kernel\Ports; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Contract\DependencyInjectionContainerInterface; |
40
|
|
|
use Apparat\Kernel\Ports\Contract\ModuleInterface; |
41
|
|
|
use Dotenv\Dotenv; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Abstract base module |
45
|
|
|
* |
46
|
|
|
* @package Apparat\Kernel |
47
|
|
|
* @subpackage Apparat\Kernel\Ports |
48
|
|
|
*/ |
49
|
|
|
abstract class AbstractModule implements ModuleInterface |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Module name |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
const NAME = 'abstract'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Auto-run |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
2 |
|
public static function autorun() |
64
|
|
|
{ |
65
|
|
|
// Validate the environment |
66
|
2 |
|
$reflectionClass = new \ReflectionClass(static::class); |
67
|
2 |
|
static::validateEnvironment(static::environment(dirname($reflectionClass->getFileName()))); |
68
|
|
|
|
69
|
|
|
// Register the module |
70
|
2 |
|
Kernel::register(new static); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Validate the environment |
75
|
|
|
* |
76
|
|
|
* @param Dotenv $environment Environment |
77
|
|
|
* @return Dotenv Environment |
78
|
|
|
*/ |
79
|
2 |
|
protected static function validateEnvironment(Dotenv $environment) |
80
|
|
|
{ |
81
|
|
|
// Overwrite in module implementations |
82
|
2 |
|
return $environment; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Instantiate the environment |
87
|
|
|
* |
88
|
|
|
* @param string $directory Directory with .env file |
89
|
|
|
* @return Dotenv Environment instance |
90
|
|
|
*/ |
91
|
2 |
|
protected static function environment($directory) |
92
|
|
|
{ |
93
|
|
|
// Collect all valid environment files |
94
|
2 |
|
$envDirectories = []; |
95
|
2 |
|
$envDirectory = $directory; |
96
|
|
|
do { |
97
|
2 |
|
if (@is_file($envDirectory.DIRECTORY_SEPARATOR.'.env')) { |
98
|
2 |
|
$envDirectories[] = $envDirectory; |
99
|
2 |
|
} |
100
|
2 |
|
$upEnvDirectory = $envDirectory; |
101
|
2 |
|
$envDirectory = dirname($envDirectory); |
102
|
2 |
|
} while (strlen($upEnvDirectory) && ($upEnvDirectory != $envDirectory)); |
103
|
|
|
|
104
|
|
|
// Load all collected environment files |
105
|
2 |
|
if (getenv('APP_ENV') === 'development') { |
106
|
2 |
|
foreach (array_reverse($envDirectories) as $envDirectory) { |
107
|
2 |
|
(new Dotenv($envDirectory))->overload(); |
108
|
2 |
|
} |
109
|
2 |
|
} |
110
|
|
|
|
111
|
2 |
|
return new Dotenv($directory); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Configure the dependency injection container |
116
|
|
|
* |
117
|
|
|
* @param DependencyInjectionContainerInterface $diContainer Dependency injection container |
118
|
|
|
* @return DependencyInjectionContainerInterface Dependency injection container |
119
|
|
|
*/ |
120
|
2 |
|
public function configureDependencyInjection(DependencyInjectionContainerInterface $diContainer) |
121
|
|
|
{ |
122
|
|
|
// Overwrite in module implementations |
123
|
2 |
|
return $diContainer; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return the module name |
128
|
|
|
* |
129
|
|
|
* @return string Module name |
130
|
|
|
*/ |
131
|
1 |
|
public function getName() |
132
|
|
|
{ |
133
|
1 |
|
return static::NAME; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|