This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
RouteNotFoundStrategy.php
470 lines (411 loc) · 15.2 KB
/
RouteNotFoundStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\View\Console;
use Zend\Console\Adapter\AdapterInterface as ConsoleAdapter;
use Zend\Console\ColorInterface;
use Zend\Console\Response as ConsoleResponse;
use Zend\Console\Request as ConsoleRequest;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\ModuleManager\ModuleManagerInterface;
use Zend\ModuleManager\Feature\ConsoleBannerProviderInterface;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\Mvc\Application;
use Zend\Mvc\Exception\RuntimeException;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\StringUtils;
use Zend\Text\Table;
use Zend\Version\Version;
use Zend\View\Model\ConsoleModel;
class RouteNotFoundStrategy extends AbstractListenerAggregate
{
/**
* Whether or not to display the reason for routing failure
*
* @var bool
*/
protected $displayNotFoundReason = true;
/**
* The reason for a not-found condition
*
* @var bool|string
*/
protected $reason = false;
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'handleRouteNotFoundError']);
}
/**
* Set flag indicating whether or not to display the routing failure
*
* @param bool $displayNotFoundReason
* @return RouteNotFoundStrategy
*/
public function setDisplayNotFoundReason($displayNotFoundReason)
{
$this->displayNotFoundReason = (bool) $displayNotFoundReason;
return $this;
}
/**
* Do we display the routing failure?
*
* @return bool
*/
public function displayNotFoundReason()
{
return $this->displayNotFoundReason;
}
/**
* Detect if an error is a route not found condition
*
* If a "controller not found" or "invalid controller" error type is
* encountered, sets the response status code to 404.
*
* @param MvcEvent $e
* @throws RuntimeException
* @throws ServiceNotFoundException
* @return void
*/
public function handleRouteNotFoundError(MvcEvent $e)
{
$error = $e->getError();
if (empty($error)) {
return;
}
$response = $e->getResponse();
$request = $e->getRequest();
switch ($error) {
case Application::ERROR_CONTROLLER_NOT_FOUND:
case Application::ERROR_CONTROLLER_INVALID:
case Application::ERROR_ROUTER_NO_MATCH:
$this->reason = $error;
if (!$response) {
$response = new ConsoleResponse();
$e->setResponse($response);
}
$response->setMetadata('error', $error);
break;
default:
return;
}
$result = $e->getResult();
if ($result instanceof Response) {
// Already have a response as the result
return;
}
// Prepare Console View Model
$model = new ConsoleModel();
$model->setErrorLevel(1);
// Fetch service manager
$sm = $e->getApplication()->getServiceManager();
// Try to fetch module manager
$mm = null;
try {
$mm = $sm->get('ModuleManager');
} catch (ServiceNotFoundException $exception) {
// The application does not have or use module manager, so we cannot use it
}
// Try to fetch current console adapter
try {
$console = $sm->get('console');
if (!$console instanceof ConsoleAdapter) {
throw new ServiceNotFoundException();
}
} catch (ServiceNotFoundException $exception) {
// The application does not have console adapter
throw new RuntimeException('Cannot access Console adapter - is it defined in ServiceManager?');
}
// Retrieve the script's name (entry point)
$scriptName = '';
if ($request instanceof ConsoleRequest) {
$scriptName = basename($request->getScriptName());
}
// Get application banner
$banner = $this->getConsoleBanner($console, $mm);
// Get application usage information
$usage = $this->getConsoleUsage($console, $scriptName, $mm);
// Inject the text into view
$result = $banner ? rtrim($banner, "\r\n") : '';
$result .= $usage ? "\n\n" . trim($usage, "\r\n") : '';
$result .= "\n"; // to ensure we output a final newline
$result .= $this->reportNotFoundReason($e);
$model->setResult($result);
// Inject the result into MvcEvent
$e->setResult($model);
}
/**
* Build Console application banner text by querying currently loaded
* modules.
*
* @param ModuleManagerInterface $moduleManager
* @param ConsoleAdapter $console
* @return string
*/
protected function getConsoleBanner(ConsoleAdapter $console, ModuleManagerInterface $moduleManager = null)
{
/*
* Loop through all loaded modules and collect banners
*/
$banners = [];
if ($moduleManager !== null) {
foreach ($moduleManager->getLoadedModules(false) as $module) {
// Strict-type on ConsoleBannerProviderInterface, or duck-type
// on the method it defines
if (!$module instanceof ConsoleBannerProviderInterface
&& !method_exists($module, 'getConsoleBanner')
) {
continue; // this module does not provide a banner
}
// Don't render empty completely empty lines
$banner = $module->getConsoleBanner($console);
if ($banner == '') {
continue;
}
// We colorize each banners in blue for visual emphasis
$banners[] = $console->colorize($banner, ColorInterface::BLUE);
}
}
/*
* Handle an application with no defined banners
*/
if (!count($banners)) {
return sprintf("Zend Framework %s application\nUsage:\n", Version::VERSION);
}
/*
* Join the banners by a newline character
*/
return implode("\n", $banners);
}
/**
* Build Console usage information by querying currently loaded modules.
*
* @param ConsoleAdapter $console
* @param string $scriptName
* @param ModuleManagerInterface $moduleManager
* @return string
* @throws RuntimeException
*/
protected function getConsoleUsage(
ConsoleAdapter $console,
$scriptName,
ModuleManagerInterface $moduleManager = null
) {
/*
* Loop through all loaded modules and collect usage info
*/
$usageInfo = [];
if ($moduleManager !== null) {
foreach ($moduleManager->getLoadedModules(false) as $name => $module) {
// Strict-type on ConsoleUsageProviderInterface, or duck-type
// on the method it defines
if (!$module instanceof ConsoleUsageProviderInterface
&& !method_exists($module, 'getConsoleUsage')
) {
continue; // this module does not provide usage info
}
// We prepend the usage by the module name (printed in red), so that each module is
// clearly visible by the user
$moduleName = sprintf(
"%s\n%s\n%s\n",
str_repeat('-', $console->getWidth()),
$name,
str_repeat('-', $console->getWidth())
);
$moduleName = $console->colorize($moduleName, ColorInterface::RED);
$usage = $module->getConsoleUsage($console);
// Normalize what we got from the module or discard
if (is_array($usage) && !empty($usage)) {
array_unshift($usage, $moduleName);
$usageInfo[$name] = $usage;
} elseif (is_string($usage) && ($usage != '')) {
$usageInfo[$name] = [$moduleName, $usage];
}
}
}
/*
* Handle an application with no usage information
*/
if (!count($usageInfo)) {
// TODO: implement fetching available console routes from router
return '';
}
/*
* Transform arrays in usage info into columns, otherwise join everything together
*/
$result = '';
$table = false;
$tableCols = 0;
$tableType = 0;
foreach ($usageInfo as $moduleName => $usage) {
if (!is_string($usage) && !is_array($usage)) {
throw new RuntimeException(sprintf(
'Cannot understand usage info for module "%s"',
$moduleName
));
}
if (is_string($usage)) {
// It's a plain string - output as is
$result .= $usage . "\n";
continue;
}
// It's an array, analyze it
foreach ($usage as $a => $b) {
/*
* 'invocation method' => 'explanation'
*/
if (is_string($a) && is_string($b)) {
if (($tableCols !== 2 || $tableType != 1) && $table !== false) {
// render last table
$result .= $this->renderTable($table, $tableCols, $console->getWidth());
$table = false;
// add extra newline for clarity
$result .= "\n";
}
// Colorize the command
$a = $console->colorize($scriptName . ' ' . $a, ColorInterface::GREEN);
$tableCols = 2;
$tableType = 1;
$table[] = [$a, $b];
continue;
}
/*
* array('--param', '--explanation')
*/
if (is_array($b)) {
if ((count($b) != $tableCols || $tableType != 2) && $table !== false) {
// render last table
$result .= $this->renderTable($table, $tableCols, $console->getWidth());
$table = false;
// add extra newline for clarity
$result .= "\n";
}
$tableCols = count($b);
$tableType = 2;
$table[] = $b;
continue;
}
/*
* 'A single line of text'
*/
if ($table !== false) {
// render last table
$result .= $this->renderTable($table, $tableCols, $console->getWidth());
$table = false;
// add extra newline for clarity
$result .= "\n";
}
$tableType = 0;
$result .= $b . "\n";
}
}
// Finish last table
if ($table !== false) {
$result .= $this->renderTable($table, $tableCols, $console->getWidth());
}
return $result;
}
/**
* Render a text table containing the data provided, that will fit inside console window's width.
*
* @param $data
* @param $cols
* @param $consoleWidth
* @return string
*/
protected function renderTable($data, $cols, $consoleWidth)
{
$result = '';
$padding = 2;
// If there is only 1 column, just concatenate it
if ($cols == 1) {
foreach ($data as $row) {
if (! isset($row[0])) {
continue;
}
$result .= $row[0] . "\n";
}
return $result;
}
// Get the string wrapper supporting UTF-8 character encoding
$strWrapper = StringUtils::getWrapper('UTF-8');
// Determine max width for each column
$maxW = [];
for ($x = 1; $x <= $cols; $x += 1) {
$maxW[$x] = 0;
foreach ($data as $row) {
$maxW[$x] = max($maxW[$x], $strWrapper->strlen($row[$x-1]) + $padding * 2);
}
}
/*
* Check if the sum of x-1 columns fit inside console window width - 10
* chars. If columns do not fit inside console window, then we'll just
* concatenate them and output as is.
*/
$width = 0;
for ($x = 1; $x < $cols; $x += 1) {
$width += $maxW[$x];
}
if ($width >= $consoleWidth - 10) {
foreach ($data as $row) {
$result .= implode(" ", $row) . "\n";
}
return $result;
}
/*
* Use Zend\Text\Table to render the table.
* The last column will use the remaining space in console window
* (minus 1 character to prevent double wrapping at the edge of the
* screen).
*/
$maxW[$cols] = $consoleWidth - $width -1;
$table = new Table\Table();
$table->setColumnWidths($maxW);
$table->setDecorator(new Table\Decorator\Blank());
$table->setPadding(2);
foreach ($data as $row) {
$table->appendRow($row);
}
return $table->render();
}
/**
* Report the 404 reason and/or exceptions
*
* @param \Zend\EventManager\EventInterface $e
* @return string
*/
protected function reportNotFoundReason($e)
{
if (!$this->displayNotFoundReason()) {
return '';
}
$exception = $e->getParam('exception', false);
if (!$exception && !$this->reason) {
return '';
}
$reason = (!empty($this->reason)) ? $this->reason : 'unknown';
$reasons = [
Application::ERROR_CONTROLLER_NOT_FOUND => 'Could not match to a controller',
Application::ERROR_CONTROLLER_INVALID => 'Invalid controller specified',
Application::ERROR_ROUTER_NO_MATCH => 'Invalid arguments or no arguments provided',
'unknown' => 'Unknown',
];
$report = sprintf("\nReason for failure: %s\n", $reasons[$reason]);
// @TODO clean up once PHP 7 requirement is enforced
while ($exception instanceof \Exception || $exception instanceof \Throwable) {
$report .= sprintf("Exception: %s\nTrace:\n%s\n", $exception->getMessage(), $exception->getTraceAsString());
$exception = $exception->getPrevious();
}
return $report;
}
}