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