1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\Rad\Prototype\DataCollector; |
4
|
|
|
|
5
|
|
|
use Knp\Rad\Prototype\Prototype; |
6
|
|
|
use Knp\Rad\Prototype\Prototype\Container; |
7
|
|
|
use phpDocumentor\Reflection\DocBlock; |
8
|
|
|
use phpDocumentor\Reflection\DocBlock\Context; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
12
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
13
|
|
|
|
14
|
|
|
class PrototypeCollector extends DataCollector |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Container |
18
|
|
|
*/ |
19
|
|
|
private $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Container $container |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Container $container) |
25
|
|
|
{ |
26
|
|
|
$this->container = $container; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function onController(FilterControllerEvent $event) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$controller = $event->getController(); |
32
|
|
|
|
33
|
|
|
if (true === is_array($controller)) { |
34
|
|
|
$controller = current($controller); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (false === $controller instanceof Prototype) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->data['controller'] = get_class($controller); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
48
|
|
|
{ |
49
|
|
|
foreach ($this->container->getMethods() as $domain => $methods) { |
50
|
|
|
foreach ($methods as $alias => $method) { |
51
|
|
|
$rflMethod = $method->getReflection(); |
52
|
|
|
$rflClass = $rflMethod->getDeclaringClass(); |
53
|
|
|
|
54
|
|
|
$context = new Context($rflClass->getNamespaceName(), [], $rflClass->getShortName()); |
55
|
|
|
$documentation = new DocBlock($rflMethod, $context); |
56
|
|
|
|
57
|
|
|
$this->data['methods'][$alias] = [ |
58
|
|
|
'alias' => $alias, |
59
|
|
|
'documentation' => $documentation, |
60
|
|
|
'domain' => $domain, |
61
|
|
|
'method' => $rflMethod->getName(), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getMethods() |
71
|
|
|
{ |
72
|
|
|
return $this->data['methods']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getPrototypeController() |
76
|
|
|
{ |
77
|
|
|
if (false === array_key_exists('controller', $this->data)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->data['controller']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getName() |
88
|
|
|
{ |
89
|
|
|
return 'knp_rad_prototype'; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.