|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\App\Route; |
|
4
|
|
|
|
|
5
|
|
|
// Dependencies from `PHP` |
|
6
|
|
|
use \InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
// PSR-7 (http messaging) dependencies |
|
9
|
|
|
use \Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use \Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
// From `charcoal-config` |
|
13
|
|
|
use \Charcoal\Config\ConfigInterface; |
|
14
|
|
|
use \Charcoal\Config\ConfigurableInterface; |
|
15
|
|
|
use \Charcoal\Config\ConfigurableTrait; |
|
16
|
|
|
|
|
17
|
|
|
// Intra-module (`charcoal-app`) dependencies |
|
18
|
|
|
use \Charcoal\App\LoggerAwareInterface; |
|
19
|
|
|
use \Charcoal\App\LoggerAwareTrait; |
|
20
|
|
|
|
|
21
|
|
|
// Local namespace dependencies |
|
22
|
|
|
use \Charcoal\App\Route\RouteInterface; |
|
23
|
|
|
use \Charcoal\App\Route\ScriptRouteConfig; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
class ScriptRoute implements |
|
|
|
|
|
|
29
|
|
|
ConfigurableInterface, |
|
30
|
|
|
LoggerAwareInterface, |
|
31
|
|
|
RouteInterface |
|
32
|
|
|
{ |
|
33
|
|
|
use ConfigurableTrait; |
|
34
|
|
|
use LoggerAwareTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \Slim\App $app |
|
38
|
|
|
*/ |
|
39
|
|
|
private $app; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* ## Required dependencies |
|
43
|
|
|
* - `config` ScriptRouteConfig |
|
44
|
|
|
* - `app` SlimApp |
|
45
|
|
|
* |
|
46
|
|
|
* ## Optional dependencies |
|
47
|
|
|
* - `logger` |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $data Dependencies. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(array $data) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->set_config($data['config']); |
|
54
|
|
|
$this->set_app($data['app']); |
|
55
|
|
|
|
|
56
|
|
|
// Reuse app logger, if it's not directly set in data dependencies |
|
57
|
|
|
$logger = isset($data['logger']) ? $data['logger'] : $this->app->logger; |
|
58
|
|
|
$this->set_logger($logger); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set the manager's reference to the Slim App. |
|
63
|
|
|
* |
|
64
|
|
|
* @param SlimApp $app The Slim Application instance. |
|
65
|
|
|
* @return TemplateRoute Chainable |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function set_app(SlimApp $app) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->app = $app; |
|
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the manager's reference to the Slim App |
|
75
|
|
|
* |
|
76
|
|
|
* @return SlimApp |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function app() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->app; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* ConfigurableTrait > create_config() |
|
85
|
|
|
* |
|
86
|
|
|
* @param mixed|null $data Optional config data. |
|
87
|
|
|
* @return ConfigInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function create_config($data = null) |
|
90
|
|
|
{ |
|
91
|
|
|
return new ScriptRouteConfig($data); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param RequestInterface $request A PSR-7 compatible Request instance. |
|
96
|
|
|
* @param ResponseInterface $response A PSR-7 compatible Response instance. |
|
97
|
|
|
* @return ResponseInterface |
|
98
|
|
|
*/ |
|
99
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response) |
|
100
|
|
|
{ |
|
101
|
|
|
unset($request); |
|
102
|
|
|
return $response; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.