1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: caoyangmin |
5
|
|
|
* Date: 2018/6/14 |
6
|
|
|
* Time: 下午6:03 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PhpBoot\Console; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use DI\FactoryInterface; |
13
|
|
|
use PhpBoot\Entity\ArrayContainer; |
14
|
|
|
use PhpBoot\Entity\EntityContainer; |
15
|
|
|
use PhpBoot\Metas\ParamMeta; |
16
|
|
|
use PhpBoot\Utils\ArrayAdaptor; |
17
|
|
|
use PhpBoot\Validator\Validator; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
22
|
|
|
|
23
|
|
|
class Command extends \Symfony\Component\Console\Command\Command |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ParamMeta[] |
28
|
|
|
*/ |
29
|
|
|
private $paramMetas; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $actionName; |
35
|
|
|
|
36
|
1 |
|
public function __construct($actionName, $name) |
37
|
|
|
{ |
38
|
1 |
|
parent::__construct($name?:$actionName); |
39
|
1 |
|
$this->actionName = $actionName; |
40
|
1 |
|
} |
41
|
|
|
/** |
42
|
|
|
* @param ParamMeta[] $paramMetas |
43
|
|
|
*/ |
44
|
1 |
|
public function setParamMetas($paramMetas) |
45
|
|
|
{ |
46
|
1 |
|
$this->paramMetas = $paramMetas; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
public function postCreate(ConsoleContainer $container) |
50
|
|
|
{ |
51
|
1 |
|
if($container->getModuleName()){ |
52
|
1 |
|
$this->setName($container->getModuleName().'.'.$this->getName()); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
foreach ($this->paramMetas as $paramMeta){ |
56
|
1 |
|
$mode = $paramMeta->isOptional?InputArgument::OPTIONAL:InputArgument::REQUIRED; |
57
|
1 |
|
if($paramMeta->container instanceof ArrayContainer || $paramMeta->container instanceof EntityContainer){ |
58
|
1 |
|
$mode = $mode|InputArgument::IS_ARRAY; |
59
|
1 |
|
} |
60
|
1 |
|
$this->addArgument($paramMeta->name, |
61
|
1 |
|
$mode, |
62
|
1 |
|
$paramMeta->description, |
63
|
1 |
|
$paramMeta->default |
64
|
1 |
|
); |
65
|
1 |
|
} |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function getParamMeta($name) |
69
|
|
|
{ |
70
|
1 |
|
foreach ($this->paramMetas as $meta){ |
71
|
1 |
|
if($meta->name == $name){ |
72
|
1 |
|
return $meta; |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param FactoryInterface $factory |
80
|
|
|
* @param ConsoleContainer $container |
81
|
|
|
* @param InputInterface $input |
82
|
|
|
* @param OutputInterface $output |
83
|
|
|
* @return mixed |
84
|
|
|
* @throws \DI\DependencyException |
85
|
|
|
* @throws \DI\NotFoundException |
86
|
|
|
*/ |
87
|
1 |
|
public function invoke(FactoryInterface $factory,ConsoleContainer $container, InputInterface $input, OutputInterface $output) |
|
|
|
|
88
|
|
|
{ |
89
|
1 |
|
$params = []; |
90
|
1 |
|
$reference = []; |
91
|
1 |
|
$this->bindInput($input, $params,$reference); |
92
|
1 |
|
$code = call_user_func_array([$container->getInstance($factory), $this->actionName], $params); |
93
|
1 |
|
return $code; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function bindInput(InputInterface $input, array &$params, array &$reference){ |
|
|
|
|
97
|
1 |
|
$vld = new Validator(); |
98
|
1 |
|
$req = ['argv'=>$input->getArguments()]; |
99
|
1 |
|
$requestArray = new ArrayAdaptor($req); |
100
|
1 |
|
$inputs = []; |
101
|
1 |
View Code Duplication |
foreach ($this->paramMetas as $k=>$meta){ |
|
|
|
|
102
|
1 |
|
if($meta->isPassedByReference){ |
103
|
|
|
// param PassedByReference is used to output |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
1 |
|
$source = \JmesPath\search($meta->source, $requestArray); |
107
|
1 |
|
if ($source !== null){ |
108
|
1 |
|
$source = ArrayAdaptor::strip($source); |
109
|
1 |
|
if($source instanceof ParameterBag){ |
110
|
|
|
$source = $source->all(); |
111
|
|
|
} |
112
|
1 |
|
if($meta->container){ |
113
|
1 |
|
$inputs[$meta->name] = $meta->container->make($source); |
114
|
1 |
|
}else{ |
115
|
|
|
$inputs[$meta->name] = $source; |
116
|
|
|
} |
117
|
1 |
|
if($meta->validation){ |
118
|
|
|
$vld->rule($meta->validation, $meta->name); |
119
|
|
|
} |
120
|
1 |
|
}else{ |
121
|
|
|
$meta->isOptional or \PhpBoot\abort(new \InvalidArgumentException("the parameter \"{$meta->source}\" is missing")); |
122
|
|
|
$inputs[$meta->name] = $meta->default; |
123
|
|
|
} |
124
|
1 |
|
} |
125
|
1 |
|
$vld = $vld->withData($inputs); |
126
|
1 |
|
$vld->validate() or \PhpBoot\abort( |
127
|
|
|
new \InvalidArgumentException( |
128
|
|
|
json_encode( |
129
|
|
|
$vld->errors(), |
130
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
131
|
|
|
) |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
|
135
|
1 |
|
$pos = 0; |
136
|
1 |
|
foreach ($this->paramMetas as $meta){ |
137
|
1 |
|
if($meta->isPassedByReference){ |
138
|
|
|
$params[$pos] = null; |
139
|
|
|
}else{ |
140
|
1 |
|
$params[$pos] = $inputs[$meta->name]; |
141
|
|
|
} |
142
|
1 |
|
$pos++; |
143
|
1 |
|
} |
144
|
|
|
} |
145
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.