1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Antidot\Installer\Template\Micro; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
use function file_put_contents; |
10
|
|
|
use function is_dir; |
11
|
|
|
use function mkdir; |
12
|
|
|
use function scandir; |
13
|
|
|
use function sprintf; |
14
|
|
|
|
15
|
|
|
class FileStructure |
16
|
|
|
{ |
17
|
|
|
private const FILES = [ |
18
|
|
|
'getGitignore' => '.gitignore', |
19
|
|
|
'getConfig' => 'config/config.php', |
20
|
|
|
'getFrameworkConfig' => 'config/framework.prod.php', |
21
|
|
|
'getContainer' => 'config/container.php', |
22
|
|
|
'getIndex' => 'public/index.php', |
23
|
|
|
'getReadme' => 'README.md', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
private const DIRECTORIES = [ |
27
|
|
|
'public', |
28
|
|
|
'config', |
29
|
|
|
'var/cache', |
30
|
|
|
'test', |
31
|
|
|
]; |
32
|
|
|
|
33
|
3 |
|
public function create(string $installationPath): void |
34
|
|
|
{ |
35
|
3 |
|
if (!is_dir($installationPath) && !mkdir($dir = $installationPath, 0755, true) && !is_dir($dir)) { |
36
|
|
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
if (!is_readable($installationPath) && 2 !== count((array) scandir($installationPath))) { |
40
|
|
|
throw new RuntimeException(sprintf( |
41
|
|
|
'Given directory "%s" is not readable or is not empty.', |
42
|
|
|
$installationPath |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
foreach (self::DIRECTORIES as $directory) { |
47
|
3 |
|
if (!mkdir($dir = sprintf('%s/%s', $installationPath, $directory), 0755, true) && !is_dir($dir)) { |
48
|
1 |
|
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
foreach (self::FILES as $method => $filename) { |
53
|
2 |
|
file_put_contents(sprintf('%s/%s', $installationPath, $filename), $this->$method()); |
54
|
|
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
public static function getGitignore(): string |
58
|
|
|
{ |
59
|
|
|
$gitignoreContents = <<<EOT |
60
|
2 |
|
/composer.lock |
61
|
|
|
|
62
|
|
|
/config/*.dev.php |
63
|
|
|
/config/*.local.php |
64
|
|
|
/config/development.config.php |
65
|
|
|
|
66
|
|
|
/vendor |
67
|
|
|
/var/cache/* |
68
|
|
|
!/var/cache/.gitkeep |
69
|
|
|
/var/log/* |
70
|
|
|
!/var/log/.gitkeep |
71
|
|
|
EOT; |
72
|
|
|
|
73
|
2 |
|
return $gitignoreContents; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
public static function getConfig(): string |
77
|
|
|
{ |
78
|
|
|
$configContent = <<<'PHP' |
79
|
2 |
|
<?php |
80
|
|
|
|
81
|
|
|
declare(strict_types=1); |
82
|
|
|
|
83
|
|
|
use Antidot\DevTools\Container\Config\ConfigProvider as DevToolsConfigProvider; |
84
|
|
|
use Laminas\ConfigAggregator\ConfigAggregator; |
85
|
|
|
use Laminas\ConfigAggregator\ArrayProvider; |
86
|
|
|
use Laminas\ConfigAggregator\PhpFileProvider; |
87
|
|
|
|
88
|
|
|
// To enable or disable caching, set the `ConfigAggregator::ENABLE_CACHE` boolean in |
89
|
|
|
// `config/autoload/local.php`. |
90
|
|
|
$cacheConfig = [ |
91
|
|
|
'config_cache_path' => 'var/cache/config-cache.php', |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$aggregator = new ConfigAggregator([ |
95
|
|
|
class_exists(DevToolsConfigProvider::class) ? DevToolsConfigProvider::class : fn() => [], |
96
|
|
|
// Load application config in a pre-defined order in such a way that local settings |
97
|
|
|
// overwrite global settings. (Loaded as first to last): |
98
|
|
|
// - `*.php` |
99
|
|
|
// - `*.global.php` |
100
|
|
|
// - `*.local.php` |
101
|
|
|
// - `*.dev.php` |
102
|
|
|
new PhpFileProvider(realpath(__DIR__).'/{{,*.}prod,{,*.}local,{,*.}dev}.php'), |
103
|
|
|
new ArrayProvider($cacheConfig), |
104
|
|
|
], $cacheConfig['config_cache_path']); |
105
|
|
|
|
106
|
|
|
return $aggregator->getMergedConfig(); |
107
|
|
|
|
108
|
|
|
PHP; |
109
|
|
|
|
110
|
2 |
|
return $configContent; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
public static function getContainer(): string |
114
|
|
|
{ |
115
|
|
|
$containerContent = <<<'PHP' |
116
|
2 |
|
<?php |
117
|
|
|
|
118
|
|
|
declare(strict_types=1); |
119
|
|
|
|
120
|
|
|
// Load configuration |
121
|
|
|
use Antidot\Container\Builder; |
122
|
|
|
|
123
|
|
|
$config = require __DIR__ . '/../config/config.php'; |
124
|
|
|
|
125
|
|
|
return Builder::build($config, true); |
126
|
|
|
|
127
|
|
|
PHP; |
128
|
|
|
|
129
|
2 |
|
return $containerContent; |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
public static function getIndex(): string |
133
|
|
|
{ |
134
|
|
|
$indexContent = <<<'PHP' |
135
|
2 |
|
<?php |
136
|
|
|
|
137
|
|
|
declare(strict_types=1); |
138
|
|
|
|
139
|
|
|
use Antidot\Application\Http\Application; |
140
|
|
|
use Antidot\Application\Http\Middleware\ErrorMiddleware; |
141
|
|
|
use Antidot\Application\Http\Middleware\RouteDispatcherMiddleware; |
142
|
|
|
use Antidot\Application\Http\Middleware\RouteNotFoundMiddleware; |
143
|
|
|
use Laminas\Diactoros\Response\JsonResponse; |
144
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
145
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
146
|
|
|
use Psr\Http\Message\ResponseInterface; |
147
|
|
|
|
148
|
|
|
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) { |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
\chdir(\dirname(__DIR__)); |
152
|
|
|
require 'vendor/autoload.php'; |
153
|
|
|
/** |
154
|
|
|
* Self-called anonymous function that creates its own scope and keep the global namespace clean. |
155
|
|
|
*/ |
156
|
|
|
\call_user_func(static function (): void { |
157
|
|
|
\error_reporting(E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE); |
158
|
|
|
|
159
|
|
|
/** @var \Psr\Container\ContainerInterface $container */ |
160
|
|
|
$container = require 'config/container.php'; |
161
|
|
|
/** @var Application $app */ |
162
|
|
|
$app = $container->get(Application::class); |
163
|
|
|
|
164
|
|
|
// Global Pipeline Configuration |
165
|
|
|
$app->pipe(ErrorMiddleware::class); |
166
|
|
|
$app->pipe(RouteDispatcherMiddleware::class); |
167
|
|
|
$app->pipe(RouteNotFoundMiddleware::class); |
168
|
|
|
|
169
|
|
|
// Application Routes |
170
|
|
|
$app->get('/', [ |
171
|
|
|
static function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { |
172
|
|
|
$request = $request->withAttribute('docs_url', 'https://antidotfw.io'); |
173
|
|
|
return $handler->handle($request); |
174
|
|
|
}, |
175
|
|
|
static function(ServerRequestInterface $request): ResponseInterface { |
176
|
|
|
return new JsonResponse([ |
177
|
|
|
'message' => 'Welcome to Antidot Framework Micro HTTP App.', |
178
|
|
|
'docs' => $request->getAttribute('docs_url'), |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
], 'homepage'); |
182
|
|
|
|
183
|
|
|
$app->run(); |
184
|
|
|
}); |
185
|
|
|
|
186
|
|
|
PHP; |
187
|
|
|
|
188
|
2 |
|
return $indexContent; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
public static function getReadme(): string |
192
|
|
|
{ |
193
|
|
|
$readmeContents = <<<'EOT' |
194
|
2 |
|
# Antidot Framework Micro HTTP App |
195
|
|
|
|
196
|
|
|
Lightweight PSR-15 middleware application. |
197
|
|
|
|
198
|
|
|
## Routing |
199
|
|
|
|
200
|
|
|
You can add your routes with it custom middlewares in `public/index.php` file, take a look at the example: |
201
|
|
|
|
202
|
|
|
```php |
203
|
|
|
<?php |
204
|
|
|
// public/index.php |
205
|
|
|
declare(strict_types=1); |
206
|
|
|
|
207
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
208
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
209
|
|
|
use Psr\Http\Message\ResponseInterface; |
210
|
|
|
... |
211
|
|
|
|
212
|
|
|
// Application Routes |
213
|
|
|
$app->get('/', [ |
214
|
|
|
static function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { |
215
|
|
|
$request = $request->withAttribute('docs_url', 'https://antidotfw.io'); |
216
|
|
|
return $handler->handle($request); |
217
|
|
|
}, |
218
|
|
|
static function(ServerRequestInterface $request): ResponseInterface { |
219
|
|
|
return new JsonResponse([ |
220
|
|
|
'message' => 'Welcome to Antidot Framework Micro HTTP App.', |
221
|
|
|
'docs' => $request->getAttribute('docs_url'), |
222
|
|
|
]); |
223
|
|
|
} |
224
|
|
|
], 'homepage'); |
225
|
|
|
... |
226
|
|
|
|
227
|
|
|
``` |
228
|
|
|
|
229
|
|
|
## File structure |
230
|
|
|
|
231
|
|
|
``` |
232
|
|
|
config/ |
233
|
|
|
config.php |
234
|
|
|
container.php |
235
|
|
|
framework.prod.php |
236
|
|
|
public/ |
237
|
|
|
index.php |
238
|
|
|
test/ |
239
|
|
|
var/ |
240
|
|
|
cache/ |
241
|
|
|
.gitignore |
242
|
|
|
composer.json |
243
|
|
|
phpcs.xml.dist |
244
|
|
|
phpunit.xml.dist |
245
|
|
|
README.md |
246
|
|
|
``` |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
EOT; |
250
|
|
|
|
251
|
2 |
|
return $readmeContents; |
252
|
|
|
} |
253
|
|
|
|
254
|
2 |
|
public static function getFrameworkConfig(): string |
255
|
|
|
{ |
256
|
|
|
$frameworkConfigContents = <<<'PHP' |
257
|
2 |
|
<?php |
258
|
|
|
|
259
|
|
|
declare(strict_types=1); |
260
|
|
|
|
261
|
|
|
return [ |
262
|
|
|
'debug' => false, |
263
|
|
|
'config_cache_enabled' => true |
264
|
|
|
]; |
265
|
|
|
|
266
|
|
|
PHP; |
267
|
|
|
|
268
|
2 |
|
return $frameworkConfigContents; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|