Completed
Pull Request — 1.0.x (#15)
by Koldo
04:52 queued 02:11
created

FileStructure::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
ccs 3
cts 3
cp 1
crap 1
rs 9.7998
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 getIndex(): string
92
    {
93
        $indexContent = <<<'PHP'
94 2
<?php
95
96
declare(strict_types=1);
97
98
use Antidot\Application\Http\Application;
99
use Antidot\Application\Http\Middleware\ErrorMiddleware;
100
use Antidot\Application\Http\Middleware\RouteDispatcherMiddleware;
101
use Antidot\Application\Http\Middleware\RouteNotFoundMiddleware;
102
use Laminas\Diactoros\Response\JsonResponse;
103
use Psr\Http\Message\ServerRequestInterface;
104
use Psr\Http\Server\RequestHandlerInterface;
105
use Psr\Http\Message\ResponseInterface;
106
107
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
108
    return false;
109
}
110
\chdir(\dirname(__DIR__));
111
require 'vendor/autoload.php';
112
/**
113
 * Self-called anonymous function that creates its own scope and keep the global namespace clean.
114
 */
115
\call_user_func(static function (): void {
116
    \error_reporting(E_ALL & ~E_USER_DEPRECATED & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
117
118
    /** @var \Psr\Container\ContainerInterface $container */
119
    $container = require 'config/container.php';
120
    /** @var Application $app */
121
    $app = $container->get(Application::class);
122
    
123
    // Global Pipeline Configuration
124
    $app->pipe(ErrorMiddleware::class);
125
    $app->pipe(RouteDispatcherMiddleware::class);
126
    $app->pipe(RouteNotFoundMiddleware::class);
127
128
    // Application Routes    
129
    $app->get('/', [
130
        static function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
131
            $request = $request->withAttribute('docs_url', 'https://antidotfw.io');
132
            return $handler->handle($request);
133
        },
134
        static function(ServerRequestInterface $request): ResponseInterface {
135
            return new JsonResponse([
136
                'message' => 'Welcome to Antidot Framework Micro HTTP App.',
137
                'docs' => $request->getAttribute('docs_url'),
138
            ]);
139
        }
140
    ], 'homepage');
141
142
    $app->run();
143
});
144
145
PHP;
146
147 2
        return $indexContent;
148
    }
149
150 2
    public static function getReadme(): string
151
    {
152
        $readmeContents = <<<'EOT'
153 2
# Antidot Framework Micro HTTP App
154
155
Lightweight PSR-15 middleware application.
156
157
## Routing
158
159
You can add your routes with it custom middlewares in `public/index.php` file, take a look at the example:
160
161
```php 
162
<?php
163
// public/index.php
164
declare(strict_types=1);
165
166
use Psr\Http\Message\ServerRequestInterface;
167
use Psr\Http\Server\RequestHandlerInterface;
168
use Psr\Http\Message\ResponseInterface;
169
...
170
171
    // Application Routes    
172
    $app->get('/', [
173
        static function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
174
            $request = $request->withAttribute('docs_url', 'https://antidotfw.io');
175
            return $handler->handle($request);
176
        },
177
        static function(ServerRequestInterface $request): ResponseInterface {
178
            return new JsonResponse([
179
                'message' => 'Welcome to Antidot Framework Micro HTTP App.',
180
                'docs' => $request->getAttribute('docs_url'),
181
            ]);
182
        }
183
    ], 'homepage');
184
...
185
186
```
187
188
## File structure
189
190
```
191
config/
192
    config.php
193
    container.php
194
    framework.prod.php
195
public/
196
    index.php
197
test/
198
var/
199
    cache/
200
.gitignore
201
composer.json
202
phpcs.xml.dist
203
phpunit.xml.dist
204
README.md        
205
```
206
207
208
EOT;
209
210 2
        return $readmeContents;
211
    }
212
213 2
    public static function getFrameworkConfig(): string
214
    {
215
        $frameworkConfigContents = <<<'PHP'
216 2
<?php
217
218
declare(strict_types=1);
219
220
return [
221
    'debug' => false,
222
    'config_cache_enabled' => true
223
];
224
225
PHP;
226
227 2
        return $frameworkConfigContents;
228
    }
229
}
230