1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Controller; |
4
|
|
|
|
5
|
|
|
use DI\FactoryInterface; |
6
|
|
|
use \DI\InvokerInterface as DIInvokerInterface; |
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use PhpBoot\Cache\LocalCacheInterface; |
9
|
|
|
use PhpBoot\Controller\Annotations\BindAnnotationHandler; |
10
|
|
|
use PhpBoot\Controller\Annotations\ClassAnnotationHandler; |
11
|
|
|
use PhpBoot\Controller\Annotations\HookAnnotationHandler; |
12
|
|
|
use PhpBoot\Controller\Annotations\ParamAnnotationHandler; |
13
|
|
|
use PhpBoot\Controller\Annotations\PathAnnotationHandler; |
14
|
|
|
use PhpBoot\Controller\Annotations\ReturnAnnotationHandler; |
15
|
|
|
use PhpBoot\Controller\Annotations\RouteAnnotationHandler; |
16
|
|
|
use PhpBoot\Controller\Annotations\ThrowsAnnotationHandler; |
17
|
|
|
use PhpBoot\Controller\Annotations\ValidateAnnotationHandler; |
18
|
|
|
use PhpBoot\Annotation\ContainerBuilder; |
19
|
|
|
|
20
|
|
|
class ControllerContainerBuilder extends ContainerBuilder |
21
|
|
|
{ |
22
|
|
|
static $DEFAULT_ANNOTATIONS=[ |
23
|
|
|
[ClassAnnotationHandler::class, 'class'], |
24
|
|
|
[PathAnnotationHandler::class, "class.children[?name=='path']"], |
25
|
|
|
[RouteAnnotationHandler::class, "methods.*.children[?name=='route'][]"], |
26
|
|
|
[ParamAnnotationHandler::class, "methods.*.children[?name=='param'][]"], |
27
|
|
|
[ReturnAnnotationHandler::class, "methods.*.children[?name=='return'][]"], |
28
|
|
|
[BindAnnotationHandler::class, "methods.*.children[].children[?name=='bind'][]"], |
29
|
|
|
[ThrowsAnnotationHandler::class, "methods.*.children[?name=='throws'][]"], |
30
|
|
|
[ValidateAnnotationHandler::class, "methods.*.children[].children[?name=='v'][]"], |
31
|
|
|
[HookAnnotationHandler::class, "methods.*.children[?name=='hook'][]"], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ControllerContainerBuilder constructor. |
36
|
|
|
* @param FactoryInterface $factory |
37
|
|
|
* @param DIInvokerInterface $diInvoker |
38
|
|
|
* @param Cache $cache |
39
|
|
|
* @param array $annotations |
40
|
|
|
*/ |
41
|
89 |
View Code Duplication |
public function __construct(FactoryInterface $factory, |
|
|
|
|
42
|
|
|
DIInvokerInterface $diInvoker, |
43
|
|
|
Cache $cache, |
44
|
|
|
array $annotations = null) |
45
|
|
|
{ |
46
|
89 |
|
if($annotations){ |
47
|
|
|
parent::__construct($annotations, $cache); |
48
|
|
|
}else{ |
49
|
89 |
|
parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache); |
50
|
|
|
} |
51
|
|
|
|
52
|
89 |
|
$this->factory = $factory; |
53
|
89 |
|
$this->diInvoker = $diInvoker; |
54
|
89 |
|
} |
55
|
|
|
/** |
56
|
|
|
* load from class with local cache |
57
|
|
|
* @param string $className |
58
|
|
|
* @return ControllerContainer |
59
|
|
|
*/ |
60
|
7 |
|
public function build($className) |
61
|
|
|
{ |
62
|
7 |
|
return parent::build($className); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $className |
67
|
|
|
* @return ControllerContainer |
68
|
|
|
*/ |
69
|
4 |
|
public function buildWithoutCache($className) |
70
|
|
|
{ |
71
|
4 |
|
return parent::buildWithoutCache($className); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $className |
76
|
|
|
* @return object |
77
|
|
|
*/ |
78
|
4 |
|
protected function createContainer($className) |
79
|
|
|
{ |
80
|
4 |
|
return $this->factory->make(ControllerContainer::class, ['className'=>$className]); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
protected function handleAnnotation($handlerName, $container, $ann) |
84
|
|
|
{ |
85
|
4 |
|
$handler = $this->factory->make($handlerName); |
86
|
4 |
|
return $this->diInvoker->call($handler, [$container, $ann]); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var FactoryInterface |
92
|
|
|
*/ |
93
|
|
|
private $factory; |
94
|
|
|
/** |
95
|
|
|
* @var DIInvokerInterface |
96
|
|
|
*/ |
97
|
|
|
private $diInvoker; |
98
|
|
|
} |
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.