1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\SaxulumControllerProvider\Provider; |
4
|
|
|
|
5
|
|
|
use Saxulum\SaxulumControllerProvider\Map\Controller; |
6
|
|
|
use Saxulum\SaxulumControllerProvider\Map\Map; |
7
|
|
|
use Silex\Application; |
8
|
|
|
use Silex\ServiceProviderInterface; |
9
|
|
|
|
10
|
|
|
class SaxulumControllerProvider implements ServiceProviderInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
public function register(Application $app) |
16
|
|
|
{ |
17
|
|
|
$app['controller.map'] = $app->share(function () { |
18
|
|
|
return new Map(); |
19
|
|
|
}); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function boot(Application $app) |
26
|
|
|
{ |
27
|
|
|
foreach ($app['controller.map']->getControllers() as $controller) { |
28
|
|
|
/** @var Controller $controller */ |
29
|
|
|
$controllerNamespace = $controller->getNamespace(); |
30
|
|
|
$controllerReflection = new \ReflectionClass($controllerNamespace); |
31
|
|
|
if($controllerReflection->implementsInterface('Saxulum\SaxulumControllerProvider\Controller\ControllerRouteInterface') && |
32
|
|
|
!$controllerReflection->isAbstract() && |
33
|
|
|
!$controllerReflection->isInterface()) { |
34
|
|
|
$app[$controller->getServiceId()] = $app->share(function () use ($app, $controller, $controllerNamespace, $controllerReflection) { |
35
|
|
|
if ($controller->isInjectContainer()) { |
36
|
|
|
return new $controllerNamespace($app); |
37
|
|
|
} |
38
|
|
|
$controllerInstance = SaxulumControllerProvider::constructController($app, $controller, $controllerReflection); |
39
|
|
|
SaxulumControllerProvider::methodInjections($app, $controller, $controllerReflection, $controllerInstance); |
40
|
|
|
|
41
|
|
|
return $controllerInstance; |
42
|
|
|
}); |
43
|
|
|
$controllerNamespace::addRoutes($app, $controller->getServiceId()); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Application $app |
50
|
|
|
* @param Controller $controller |
51
|
|
|
* @param \ReflectionClass $controllerReflection |
52
|
|
|
* @return object |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public static function constructController(Application $app, Controller $controller, \ReflectionClass $controllerReflection) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if ($controller->hasInjectionKeys()) { |
57
|
|
|
$args = array(); |
58
|
|
|
foreach ($controller->getInjectionKeys() as $injectionKey) { |
59
|
|
|
if (isset($app[$injectionKey])) { |
60
|
|
|
$args[] = $app[$injectionKey]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
$controllerInstance = $controllerReflection->newInstanceArgs($args); |
64
|
|
|
} else { |
65
|
|
|
$controllerInstance = $controllerReflection->newInstance(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $controllerInstance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Application $app |
73
|
|
|
* @param Controller $controller |
74
|
|
|
* @param \ReflectionClass $controllerReflection |
75
|
|
|
* @param $controllerInstance |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public static function methodInjections(Application $app, Controller $controller, \ReflectionClass $controllerReflection, $controllerInstance) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
foreach ($controller->getMethods() as $method) { |
80
|
|
|
$methodReflection = $controllerReflection->getMethod($method->getName()); |
81
|
|
|
$args = array(); |
82
|
|
|
foreach ($method->getInjectionKeys() as $injectionKey) { |
83
|
|
|
if (isset($app[$injectionKey])) { |
84
|
|
|
$args[] = $app[$injectionKey]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
$methodReflection->invokeArgs($controllerInstance, $args); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.