1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Console\Converter; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Console\Command\ConsoleCommandInterface; |
13
|
|
|
use Cubiche\Core\Cqrs\Command\CommandConverterInterface; |
14
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
15
|
|
|
use Webmozart\Console\Api\Args\Args; |
16
|
|
|
use Webmozart\Console\Api\Args\Format\ArgsFormat; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ConsoleArgsToCommand class. |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ConsoleArgsToCommand implements CommandConverterInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Args |
27
|
|
|
*/ |
28
|
|
|
protected $args; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ArgsFormat |
32
|
|
|
*/ |
33
|
|
|
protected $format; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Args $args |
37
|
|
|
*/ |
38
|
|
|
public function setArgs(Args $args) |
39
|
|
|
{ |
40
|
|
|
$this->args = $args; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ArgsFormat $format |
45
|
|
|
*/ |
46
|
|
|
public function setFormat(ArgsFormat $format) |
47
|
|
|
{ |
48
|
|
|
$this->format = $format; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getCommandFrom($className) |
55
|
|
|
{ |
56
|
|
|
if (class_exists($className)) { |
57
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
58
|
|
|
$reflector = new \ReflectionClass($className); |
59
|
|
|
$instance = $reflector->newInstanceWithoutConstructor(); |
60
|
|
|
|
61
|
|
|
foreach ($reflector->getProperties() as $property) { |
62
|
|
|
if ($instance instanceof ConsoleCommandInterface && $property->getName() == 'io') { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$this->format->hasArgument($property->getName()) && |
67
|
|
|
!$this->format->hasOption($property->getName()) |
68
|
|
|
) { |
69
|
|
|
throw new \InvalidArgumentException(sprintf( |
70
|
|
|
"There is not '%s' argument defined in the %s command", |
71
|
|
|
$property->getName(), |
72
|
|
|
$className |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$value = null; |
77
|
|
|
if ($this->format->hasArgument($property->getName())) { |
78
|
|
|
$value = $this->args->getArgument($property->getName()); |
79
|
|
|
} elseif ($this->format->hasOption($property->getName())) { |
80
|
|
|
$value = $this->args->getOption($property->getName()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$accessor->setValue( |
84
|
|
|
$instance, |
85
|
|
|
$property->getName(), |
86
|
|
|
$value |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $instance; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|