1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: caoyangmin |
5
|
|
|
* Date: 2018/6/14 |
6
|
|
|
* Time: 下午2:10 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PhpBoot\Console; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use DI\FactoryInterface; |
13
|
|
|
use Doctrine\Common\Cache\Cache; |
14
|
|
|
use PhpBoot\Annotation\ContainerBuilder; |
15
|
|
|
use DI\InvokerInterface as DIInvokerInterface; |
16
|
|
|
use PhpBoot\Console\Annotations\ClassAnnotationHandler; |
17
|
|
|
use PhpBoot\Console\Annotations\CommandAnnotationHandler; |
18
|
|
|
use PhpBoot\Console\Annotations\CommandNameAnnotationHandler; |
19
|
|
|
use PhpBoot\Console\Annotations\ParamAnnotationHandler; |
20
|
|
|
use PhpBoot\Console\Annotations\ValidateAnnotationHandler; |
21
|
|
|
|
22
|
|
|
class ConsoleContainerBuilder extends ContainerBuilder |
23
|
|
|
{ |
24
|
|
|
static $DEFAULT_ANNOTATIONS=[ |
25
|
|
|
[ClassAnnotationHandler::class, 'class'], |
26
|
|
|
[CommandNameAnnotationHandler::class, "class.children[?name=='command']"], |
27
|
|
|
[CommandAnnotationHandler::class, "methods.*.children[?name=='command'][]"], |
28
|
|
|
[ParamAnnotationHandler::class, "methods.*.children[?name=='param'][]"], |
29
|
|
|
[ValidateAnnotationHandler::class, "methods.*.children[].children[?name=='v'][]"], |
30
|
|
|
]; |
31
|
|
|
/** |
32
|
|
|
* @var FactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $factory; |
35
|
|
|
/** |
36
|
|
|
* @var DIInvokerInterface |
37
|
|
|
*/ |
38
|
|
|
private $diInvoker; |
39
|
|
|
|
40
|
1 |
View Code Duplication |
public function __construct(FactoryInterface $factory, |
|
|
|
|
41
|
|
|
DIInvokerInterface $diInvoker, |
42
|
|
|
Cache $cache, |
43
|
|
|
array $annotations = null |
44
|
|
|
) |
45
|
|
|
{ |
46
|
1 |
|
if($annotations){ |
47
|
|
|
parent::__construct($annotations, $cache); |
48
|
|
|
}else{ |
49
|
1 |
|
parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache); |
50
|
|
|
} |
51
|
1 |
|
$this->factory = $factory; |
52
|
1 |
|
$this->diInvoker = $diInvoker; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $className |
57
|
|
|
* @return ConsoleContainer |
58
|
|
|
*/ |
59
|
1 |
|
protected function createContainer($className) |
60
|
|
|
{ |
61
|
1 |
|
return $this->factory->make(ConsoleContainer::class, ['className'=>$className]); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
protected function handleAnnotation($handlerName, $container, $ann) |
65
|
|
|
{ |
66
|
1 |
|
$handler = $this->factory->make($handlerName); |
67
|
1 |
|
return $this->diInvoker->call($handler, [$container, $ann]); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
protected function postCreateContainer($object) |
71
|
|
|
{ |
72
|
1 |
|
parent::postCreateContainer($object); |
73
|
|
|
/**@var ConsoleContainer $object*/ |
74
|
1 |
|
$object->postCreate(); |
75
|
|
|
} |
76
|
|
|
} |
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.