1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nastoletni\Code; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Configuration; |
8
|
|
|
use Doctrine\DBAL\DriverManager; |
9
|
|
|
use Monolog\Handler\StreamHandler; |
10
|
|
|
use Monolog\Logger; |
11
|
|
|
use Nastoletni\Code\Infrastructure\AES256Crypter; |
12
|
|
|
use Nastoletni\Code\Infrastructure\Dbal\DbalPasteMapper; |
13
|
|
|
use Nastoletni\Code\Infrastructure\Dbal\DbalPasteRepository; |
14
|
|
|
use Nastoletni\Code\Infrastructure\HttpsXkcdRepository; |
15
|
|
|
use Nastoletni\Code\Slim\DecoratingCallableResolver; |
16
|
|
|
use Nastoletni\Code\Slim\Middleware\SymfonySessionMiddleware; |
17
|
|
|
use Nastoletni\Code\Twig\SymfonyValidatorExtension; |
18
|
|
|
use Nastoletni\Code\Twig\TransExtension; |
19
|
|
|
use Nastoletni\Code\UserInterface\Controller\ControllerDecorator; |
20
|
|
|
use Nastoletni\Code\UserInterface\Web\Controller\ErrorController; |
21
|
|
|
use Nastoletni\Code\UserInterface\Web\Controller\PasteController; |
22
|
|
|
use Slim\App; |
23
|
|
|
use Slim\Container; |
24
|
|
|
use Slim\Handlers\Error; |
25
|
|
|
use Slim\Handlers\PhpError; |
26
|
|
|
use Slim\Handlers\Strategies\RequestResponseArgs; |
27
|
|
|
use Slim\Views\Twig; |
28
|
|
|
use Slim\Views\TwigExtension; |
29
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
30
|
|
|
use Symfony\Component\Translation\Loader\PhpFileLoader; |
31
|
|
|
use Symfony\Component\Translation\Translator; |
32
|
|
|
use Symfony\Component\Yaml\Yaml; |
33
|
|
|
|
34
|
|
|
class AppKernel |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var App |
38
|
|
|
*/ |
39
|
|
|
private $slim; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* AppKernel constructor. |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
$this->slim = new App(); |
47
|
|
|
|
48
|
|
|
$this->setupConfig(); |
49
|
|
|
$this->setupServices(); |
|
|
|
|
50
|
|
|
$this->setupRoutes(); |
51
|
|
|
|
52
|
|
|
// Middlewares |
53
|
|
|
$this->slim->add(new SymfonySessionMiddleware($this->slim->getContainer()['session'])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets up config in container. |
58
|
|
|
*/ |
59
|
|
|
private function setupConfig(): void |
60
|
|
|
{ |
61
|
|
|
$config = Yaml::parse(file_get_contents(__DIR__.'/config.yml')); |
62
|
|
|
|
63
|
|
|
$this->slim->getContainer()['config'] = $config; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets up dependencies in container. |
68
|
|
|
*/ |
69
|
|
|
private function setupServices(): void |
70
|
|
|
{ |
71
|
|
|
$container = $this->slim->getContainer(); |
72
|
|
|
$container['settings']['displayErrorDetails'] = $container['config']['debug']; |
73
|
|
|
$container['logger'] = function () { |
74
|
|
|
return new Logger('application', [ |
75
|
|
|
new StreamHandler(__DIR__.'/../logs/logs.log'), |
76
|
|
|
]); |
77
|
|
|
}; |
78
|
|
|
$container['foundHandler'] = function () { |
79
|
|
|
return new RequestResponseArgs(); |
80
|
|
|
}; |
81
|
|
|
$container['notFoundHandler'] = function (Container $container) { |
82
|
|
|
return [$container[ErrorController::class], 'notFound']; |
83
|
|
|
}; |
84
|
|
View Code Duplication |
$container['errorHandler'] = function (Container $container) { |
|
|
|
|
85
|
|
|
// Show pretty error page on production and Slim debug info on development. |
86
|
|
|
$next = $container['config']['debug'] ? |
87
|
|
|
new Error($container['config']['debug']) : |
88
|
|
|
[$container[ErrorController::class], 'error']; |
89
|
|
|
|
90
|
|
|
return new Slim\Handler\LoggingErrorHandler( |
91
|
|
|
$container->get('logger'), |
92
|
|
|
$next |
93
|
|
|
); |
94
|
|
|
}; |
95
|
|
View Code Duplication |
$container['phpErrorHandler'] = function (Container $container) { |
|
|
|
|
96
|
|
|
// Show pretty error page on production and Slim debug info on development. |
97
|
|
|
$next = $container['config']['debug'] ? |
98
|
|
|
new PhpError($container['config']['debug']) : |
99
|
|
|
[$container[ErrorController::class], 'error']; |
100
|
|
|
|
101
|
|
|
return new Slim\Handler\LoggingErrorHandler( |
102
|
|
|
$container->get('logger'), |
103
|
|
|
$next |
104
|
|
|
); |
105
|
|
|
}; |
106
|
|
|
$container['translator'] = function (Container $container) { |
107
|
|
|
$translator = new Translator($container['config']['locale']); |
108
|
|
|
$translator->setFallbackLocales(['en']); |
109
|
|
|
|
110
|
|
|
$translator->addLoader('php', new PhpFileLoader()); |
111
|
|
|
$translator->addResource('php', __DIR__.'/../resources/translations/messages.php', 'en'); |
112
|
|
|
$translator->addResource('php', __DIR__.'/../resources/translations/messages.pl.php', 'pl'); |
113
|
|
|
|
114
|
|
|
return $translator; |
115
|
|
|
}; |
116
|
|
|
$container['twig'] = function (Container $container) { |
117
|
|
|
$twig = new Twig(__DIR__.'/../resources/views/', [ |
118
|
|
|
'debug' => $container['config']['debug'], |
119
|
|
|
]); |
120
|
|
|
$twig->addExtension(new TwigExtension($container['router'], $container['config']['base_url'])); |
121
|
|
|
$twig->addExtension(new SymfonyValidatorExtension()); |
122
|
|
|
$twig->addExtension(new TransExtension($container['translator'])); |
123
|
|
|
|
124
|
|
|
return $twig; |
125
|
|
|
}; |
126
|
|
|
$container['session'] = function () { |
127
|
|
|
return new Session(); |
128
|
|
|
}; |
129
|
|
|
$container['controllerDecorator'] = function (Container $container) { |
130
|
|
|
return new ControllerDecorator( |
131
|
|
|
$container['twig'], |
132
|
|
|
$container['router'], |
133
|
|
|
$container['session'] |
134
|
|
|
); |
135
|
|
|
}; |
136
|
|
|
$container['callableResolver'] = function (Container $container) { |
137
|
|
|
return new DecoratingCallableResolver( |
138
|
|
|
$container, |
139
|
|
|
$container['controllerDecorator'] |
140
|
|
|
); |
141
|
|
|
}; |
142
|
|
|
$container['dbal'] = function (Container $container) { |
143
|
|
|
$config = new Configuration(); |
144
|
|
|
|
145
|
|
|
return DriverManager::getConnection([ |
146
|
|
|
'driver' => 'pdo_mysql', |
147
|
|
|
'host' => $container['config']['database']['host'], |
148
|
|
|
'port' => $container['config']['database']['port'], |
149
|
|
|
'dbname' => $container['config']['database']['name'], |
150
|
|
|
'user' => $container['config']['database']['user'], |
151
|
|
|
'password' => $container['config']['database']['password'], |
152
|
|
|
'charset' => $container['config']['database']['charset'], |
153
|
|
|
], $config); |
154
|
|
|
}; |
155
|
|
|
|
156
|
|
|
// Controllers |
157
|
|
|
$container[PasteController::class] = function (Container $container) { |
158
|
|
|
$pasteRepository = new DbalPasteRepository($container['dbal'], new DbalPasteMapper()); |
159
|
|
|
|
160
|
|
|
return new PasteController($pasteRepository, new AES256Crypter()); |
161
|
|
|
}; |
162
|
|
|
$container[ErrorController::class] = function (Container $container) { |
163
|
|
|
/** @var ControllerDecorator $controllerDecorator */ |
164
|
|
|
$controllerDecorator = $container['controllerDecorator']; |
165
|
|
|
|
166
|
|
|
$errorController = new ErrorController( |
167
|
|
|
new HttpsXkcdRepository() |
168
|
|
|
); |
169
|
|
|
$controllerDecorator->decorate($errorController); |
170
|
|
|
|
171
|
|
|
return $errorController; |
172
|
|
|
}; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets up routes. |
177
|
|
|
*/ |
178
|
|
|
private function setupRoutes(): void |
179
|
|
|
{ |
180
|
|
|
$routes = Yaml::parse(file_get_contents(__DIR__.'/routes.yml')); |
181
|
|
|
|
182
|
|
|
foreach ($routes as $routeName => $route) { |
183
|
|
|
$this->slim->map([$route['method']], $route['path'], $route['controller'])->setName($routeName); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Sends response to the client. |
189
|
|
|
*/ |
190
|
|
|
public function handle(): void |
191
|
|
|
{ |
192
|
|
|
$this->slim->run(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: