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