1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Bone\Mvc;
|
4
|
|
|
|
5
|
|
|
use Barnacle\Container;
|
6
|
|
|
use Barnacle\RegistrationInterface;
|
7
|
|
|
use Bone\Http\Middleware\Stack;
|
8
|
|
|
use Bone\Mvc\Router;
|
9
|
|
|
use Bone\Mvc\Router\Decorator\ExceptionDecorator;
|
10
|
|
|
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
|
11
|
|
|
use Bone\Mvc\Router\NotFoundException;
|
12
|
|
|
use Bone\Mvc\Router\PlatesStrategy;
|
13
|
|
|
use Bone\Mvc\Router\RouterConfigInterface;
|
14
|
|
|
use Bone\Mvc\View\PlatesEngine;
|
15
|
|
|
use Bone\Server\Environment;
|
16
|
|
|
use Bone\Server\I18nHandler;
|
17
|
|
|
use Bone\Server\SiteConfig;
|
18
|
|
|
use Del\SessionManager;
|
19
|
|
|
use League\Route\RouteGroup;
|
20
|
|
|
use League\Route\Strategy\ApplicationStrategy;
|
21
|
|
|
use League\Route\Strategy\JsonStrategy;
|
22
|
|
|
use PDO;
|
23
|
|
|
use Psr\Http\Message\ResponseInterface;
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
25
|
|
|
use Laminas\Diactoros\ResponseFactory;
|
26
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
27
|
|
|
use Laminas\Diactoros\Response;
|
28
|
|
|
use Laminas\Diactoros\Response\RedirectResponse;
|
29
|
|
|
use Laminas\Diactoros\Uri;
|
30
|
|
|
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
|
31
|
|
|
use Laminas\I18n\Translator\Translator;
|
32
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
33
|
|
|
|
34
|
|
|
class Application
|
35
|
|
|
{
|
36
|
|
|
/** @var Container $registry */
|
37
|
|
|
private $treasureChest;
|
38
|
|
|
|
39
|
|
|
/** @var string $configFolder */
|
40
|
|
|
private $configFolder = 'config';
|
41
|
|
|
|
42
|
|
|
/** @var string $environment */
|
43
|
|
|
private $environment = 'production';
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* There be nay feckin wi' constructors on board this ship
|
47
|
|
|
* There be nay copyin' o' th'ship either
|
48
|
|
|
* This ship is a singleton!
|
49
|
|
|
*/
|
50
|
|
|
private function __construct(){}
|
51
|
|
|
|
52
|
|
|
private function __clone(){}
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Ahoy! There nay be boardin without yer configuration
|
57
|
|
|
*
|
58
|
|
|
* @param array $config
|
|
|
|
|
59
|
|
|
* @return Application
|
60
|
|
|
*/
|
61
|
10 |
|
public static function ahoy()
|
62
|
|
|
{
|
63
|
10 |
|
static $inst = null;
|
64
|
10 |
|
if ($inst === null) {
|
65
|
1 |
|
$inst = new Application();
|
66
|
1 |
|
$session = SessionManager::getInstance();
|
67
|
1 |
|
SessionManager::sessionStart('app');
|
68
|
1 |
|
$inst->treasureChest = new Container();
|
69
|
1 |
|
$inst->treasureChest[SessionManager::class] = $session;
|
70
|
1 |
|
$env = getenv('APPLICATION_ENV');
|
71
|
1 |
|
if ($env) {
|
72
|
1 |
|
$inst->setEnvironment($env);
|
73
|
|
|
}
|
74
|
|
|
}
|
75
|
10 |
|
return $inst;
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* Use this to bootstrap Bone without dispatching any request
|
80
|
|
|
* i.e. for when using the framework in a CLI application
|
81
|
|
|
*/
|
82
|
8 |
|
public function bootstrap(): Container
|
83
|
|
|
{
|
84
|
8 |
|
$env = new Environment($_SERVER);
|
85
|
8 |
|
$router = $this->treasureChest[Router::class] = new Router();
|
86
|
|
|
|
87
|
8 |
|
$config = $env->fetchConfig($this->configFolder, $this->environment);
|
88
|
8 |
|
$config[Environment::class] = $env;
|
89
|
8 |
|
$config[SiteConfig::class] = new SiteConfig($config, $env);
|
90
|
|
|
|
91
|
8 |
|
$package = new ApplicationPackage($config, $router);
|
92
|
8 |
|
$package->addToContainer($this->treasureChest);
|
93
|
|
|
|
94
|
8 |
|
return $this->treasureChest;
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
*
|
100
|
|
|
* T' the high seas! Garrr!
|
101
|
|
|
*
|
102
|
|
|
* @return bool
|
103
|
|
|
* @throws \Exception
|
104
|
|
|
*/
|
105
|
8 |
|
public function setSail()
|
106
|
|
|
{
|
107
|
|
|
// load in the config and set up the dependency injection container
|
108
|
8 |
|
$this->bootstrap();
|
109
|
8 |
|
$request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
|
110
|
|
|
/** @var RequestHandlerInterface $stack */
|
111
|
8 |
|
$stack = $this->treasureChest->get(Stack::class);
|
112
|
|
|
|
113
|
8 |
|
if ($this->isMultilingual()) {
|
114
|
|
|
|
115
|
|
|
try {
|
116
|
7 |
|
$request = $this->i18nRequestCheck($request);
|
117
|
7 |
|
$response = $stack->handle($request);
|
118
|
|
|
} catch (NotFoundException $e) {
|
119
|
|
|
$response = new RedirectResponse($e->getMessage());
|
120
|
|
|
if ($e->getRequest()->getMethod() !== 'GET') {
|
121
|
7 |
|
$response = $stack->handle($request);
|
122
|
|
|
}
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
} else {
|
126
|
1 |
|
$request = $this->i18nRequestCheck($request, false);
|
127
|
1 |
|
$response = $stack->handle($request);
|
128
|
|
|
}
|
129
|
|
|
|
130
|
8 |
|
(new SapiEmitter)->emit($response);
|
131
|
|
|
|
132
|
8 |
|
return true;
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
/**
|
136
|
|
|
* @return bool
|
137
|
|
|
*/
|
138
|
8 |
|
public function isMultilingual(): bool
|
139
|
|
|
{
|
140
|
8 |
|
$i18n = $this->treasureChest->get('i18n');
|
141
|
8 |
|
return $i18n['enabled'];
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/**
|
146
|
|
|
* @param ServerRequestInterface $request
|
147
|
|
|
* @param bool $handle
|
148
|
|
|
* @return ServerRequestInterface
|
149
|
|
|
* @throws NotFoundException
|
150
|
|
|
*/
|
151
|
8 |
|
private function i18nRequestCheck(ServerRequestInterface $request, bool $handle = true): ServerRequestInterface
|
152
|
|
|
{
|
153
|
8 |
|
$i18n = $this->treasureChest->get('i18n');
|
154
|
8 |
|
$translator = $this->treasureChest->get(Translator::class);
|
155
|
8 |
|
$i18nHandler = new I18nHandler($translator, $i18n['supported_locales'], $i18n['default_locale']);
|
156
|
8 |
|
if ($handle) {
|
157
|
7 |
|
$request = $i18nHandler->handleI18n($request);
|
158
|
|
|
} else {
|
159
|
1 |
|
$request = $i18nHandler->removeI18n($request);
|
160
|
|
|
}
|
161
|
|
|
|
162
|
8 |
|
return $request;
|
163
|
|
|
}
|
164
|
|
|
|
165
|
|
|
/**
|
166
|
|
|
* @return Container
|
167
|
|
|
*/
|
168
|
1 |
|
public function getContainer(): Container
|
169
|
|
|
{
|
170
|
1 |
|
return $this->treasureChest;
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
/**
|
174
|
|
|
* @param string $configFolder
|
175
|
|
|
*/
|
176
|
8 |
|
public function setConfigFolder(string $configFolder)
|
177
|
|
|
{
|
178
|
8 |
|
$this->configFolder = $configFolder;
|
179
|
8 |
|
}
|
180
|
|
|
|
181
|
|
|
/**
|
182
|
|
|
* @param string $environment
|
183
|
|
|
*/
|
184
|
1 |
|
public function setEnvironment(string $environment)
|
185
|
|
|
{
|
186
|
1 |
|
$this->environment = $environment;
|
187
|
|
|
}
|
188
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.