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