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