1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-server |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Server |
8
|
|
|
* @subpackage Apparat\Server |
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\Server; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\AbstractModule; |
40
|
|
|
use Apparat\Kernel\Ports\Contract\DependencyInjectionContainerInterface; |
41
|
|
|
use Apparat\Server\Application\Factory\PayloadFactory; |
42
|
|
|
use Apparat\Server\Domain\Contract\PayloadFactoryInterface; |
43
|
|
|
use Apparat\Server\Domain\Contract\RouterContainerInterface; |
44
|
|
|
use Apparat\Server\Domain\Model\Server; |
45
|
|
|
use Apparat\Server\Domain\Service\AbstractService; |
46
|
|
|
use Apparat\Server\Infrastructure\Route\AuraRouterAdapter; |
47
|
|
|
use Apparat\Server\Infrastructure\Traits\DateActionsTrait; |
48
|
|
|
use Apparat\Server\Infrastructure\Traits\ObjectActionsTrait; |
49
|
|
|
use Apparat\Server\Ports\Responder\AbstractResponder; |
50
|
|
|
use Apparat\Server\Ports\View\TYPO3FluidView; |
51
|
|
|
use Apparat\Server\Ports\View\ViewInterface; |
52
|
|
|
use Aura\Router\RouterContainer; |
53
|
|
|
use Dotenv\Dotenv; |
54
|
|
|
use Psr\Http\Message\ResponseInterface; |
55
|
|
|
use TYPO3Fluid\Fluid\View\AbstractTemplateView; |
56
|
|
|
use Zend\Diactoros\Response; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Server module |
60
|
|
|
* |
61
|
|
|
* @package Apparat\Server |
62
|
|
|
* @subpackage Apparat\Server |
63
|
|
|
*/ |
64
|
|
|
class Module extends AbstractModule |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* Use external dependency configurations |
68
|
|
|
*/ |
69
|
|
|
use DateActionsTrait, ObjectActionsTrait; |
70
|
|
|
/** |
71
|
|
|
* Module name |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
const NAME = 'server'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Validate the environment |
79
|
|
|
* |
80
|
|
|
* @param Dotenv $environment Environment |
81
|
|
|
* @return Dotenv Environment |
82
|
|
|
*/ |
83
|
1 |
|
protected static function validateEnvironment(Dotenv $environment) |
84
|
|
|
{ |
85
|
1 |
|
parent::validateEnvironment($environment); |
86
|
|
|
|
87
|
|
|
// Validate the Fluid cache directory (if given) |
88
|
1 |
|
putenv('FLUID_CACHE_DIRECTORY='.self::validateFluidCacheDirectory(getenv('FLUID_CACHE_DIRECTORY'))); |
89
|
|
|
|
90
|
1 |
|
return $environment; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validate the Fluid cache directory (if given) |
95
|
|
|
* |
96
|
|
|
* @param string $fluidCacheDirectory Fluid cache directory |
97
|
|
|
* @return string Validated fluid cache directory |
98
|
|
|
*/ |
99
|
2 |
|
public static function validateFluidCacheDirectory($fluidCacheDirectory) |
100
|
|
|
{ |
101
|
2 |
|
$fluidCacheDirectory = trim($fluidCacheDirectory); |
102
|
2 |
|
if (strlen($fluidCacheDirectory)) { |
103
|
2 |
|
if (!is_dir($fluidCacheDirectory)) { |
104
|
1 |
|
throw new \RuntimeException( |
105
|
1 |
|
sprintf('Invalid Fluid cache directory "%s"', $fluidCacheDirectory), |
106
|
|
|
1470500567 |
107
|
1 |
|
); |
108
|
|
|
} |
109
|
1 |
|
} |
110
|
1 |
|
return $fluidCacheDirectory; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Configure the dependency injection container |
115
|
|
|
* |
116
|
|
|
* @param DependencyInjectionContainerInterface $diContainer Dependency injection container |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
1 |
|
public function configureDependencyInjection(DependencyInjectionContainerInterface $diContainer) |
120
|
|
|
{ |
121
|
1 |
|
parent::configureDependencyInjection($diContainer); |
122
|
|
|
|
123
|
|
|
// Configure the server |
124
|
1 |
|
$diContainer->register(Server::class, [ |
125
|
1 |
|
'shared' => false, |
126
|
|
|
'substitutions' => [ |
127
|
|
|
RouterContainerInterface::class => [ |
128
|
1 |
|
'instance' => AuraRouterAdapter::class, |
129
|
|
|
] |
130
|
1 |
|
] |
131
|
1 |
|
]); |
132
|
|
|
|
133
|
|
|
// Configure the router |
134
|
1 |
|
$diContainer->register(RouterContainer::class, [ |
135
|
|
|
'constructParams' => [ |
136
|
1 |
|
rtrim(parse_url(getenv('APPARAT_BASE_URL'), PHP_URL_PATH), '/') ?: null |
137
|
1 |
|
] |
138
|
1 |
|
]); |
139
|
|
|
|
140
|
|
|
// Configure the service |
141
|
1 |
|
$diContainer->register(AbstractService::class, [ |
142
|
|
|
'substitutions' => [ |
143
|
|
|
PayloadFactoryInterface::class => [ |
144
|
1 |
|
'instance' => PayloadFactory::class, |
145
|
|
|
] |
146
|
1 |
|
] |
147
|
1 |
|
]); |
148
|
|
|
|
149
|
|
|
// Configure the responder: Diactoros Response and TYPO3Fluid template view |
150
|
1 |
|
$diContainer->register(AbstractResponder::class, [ |
151
|
|
|
'substitutions' => [ |
152
|
|
|
ResponseInterface::class => [ |
153
|
1 |
|
'instance' => Response::class, |
154
|
1 |
|
], |
155
|
|
|
ViewInterface::class => [ |
156
|
1 |
|
'instance' => TYPO3FluidView::class, |
157
|
|
|
] |
158
|
1 |
|
] |
159
|
1 |
|
]); |
160
|
|
|
|
161
|
|
|
// Configure the TYPO3Fluid template view |
162
|
1 |
|
$diContainer->register(AbstractTemplateView::class, [ |
163
|
1 |
|
'constructParams' => [null] |
164
|
1 |
|
]); |
165
|
|
|
|
166
|
|
|
// Configure the date action dependencies |
167
|
1 |
|
$this->configureDateActionDependencies($diContainer); |
168
|
|
|
|
169
|
|
|
// Configure the object action dependencies |
170
|
1 |
|
$this->configureObjectActionDependencies($diContainer); |
171
|
1 |
|
} |
172
|
|
|
} |
173
|
|
|
|