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