1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Web Controller |
4
|
|
|
* Simple convention based web controller to auto route actions |
5
|
|
|
* |
6
|
|
|
* @package erdiko/controllers |
7
|
|
|
* @copyright 2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com |
8
|
|
|
* @author John Arroyo <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
namespace erdiko\controllers; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Web extends \erdiko\Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* invoke |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param Response $response |
19
|
|
|
* @param array $args |
20
|
|
|
* @return string $action method name |
21
|
|
|
*/ |
22
|
|
|
public function __invoke($request, $response, $args) |
23
|
|
|
{ |
24
|
|
|
$action = $this->determineAction($request, $args); |
25
|
|
|
// $this->container->logger->debug("Controller action: ".print_r($action, true)); |
|
|
|
|
26
|
|
|
$this->$action($request, $response, $args); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* dertermin action |
31
|
|
|
* |
32
|
|
|
* @param Request $request |
33
|
|
|
* @param array $args |
34
|
|
|
* @return string $action method name |
35
|
|
|
*/ |
36
|
|
|
protected function determineAction($request, $args) : string |
37
|
|
|
{ |
38
|
|
|
// Request method |
39
|
|
|
$action = strtolower($request->getMethod()); |
40
|
|
|
// Action |
41
|
|
|
$action .= empty($args['action']) ? "" : ucfirst($args['action']); |
42
|
|
|
|
43
|
|
|
// @todo trigger 404 instead of throw exception |
44
|
|
|
if(!method_exists($this, $action)) |
45
|
|
|
throw new \Exception("action does not exist"); |
46
|
|
|
|
47
|
|
|
return $action; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* render |
52
|
|
|
* render page |
53
|
|
|
*/ |
54
|
|
|
public function render($response, $view, $application) |
55
|
|
|
{ |
56
|
|
|
if(empty($view)) |
57
|
|
|
$view = $application['theme']['defaults']['view']; |
58
|
|
|
|
59
|
|
|
return $this->container->theme->render($response, $view, $application); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.