Repository navigation
Expand file tree
/
Copy pathApplicationFactory.php
More file actions
137 lines (113 loc) · 4.58 KB
/
Copy pathApplicationFactory.php
File metadata and controls
137 lines (113 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Framework;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Parser;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\Framework\Event\BootApplication;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Exception\InvalidArgumentException as SymfonyInvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class ApplicationFactory
{
public function __invoke(ContainerInterface $container)
{
if ($container->has(EventDispatcherInterface::class)) {
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new BootApplication());
}
$config = $container->get(ConfigInterface::class);
$commands = $config->get('commands', []);
// Append commands that defined by annotation.
$annotationCommands = [];
if (class_exists(AnnotationCollector::class) && class_exists(Command::class)) {
$annotationCommands = AnnotationCollector::getClassesByAnnotation(Command::class);
$annotationCommands = array_keys($annotationCommands);
}
$commands = array_unique(array_merge($commands, $annotationCommands));
$application = new Application();
if ($config->get('symfony.event.enable', false) && isset($eventDispatcher) && class_exists(SymfonyEventDispatcher::class)) {
$application->setDispatcher(new SymfonyEventDispatcher($eventDispatcher));
}
foreach ($commands as $command) {
$application->add(
$this->pendingCommand($container->get($command))
);
}
return $application;
}
/**
* @throws InvalidArgumentException
* @throws SymfonyInvalidArgumentException
* @throws LogicException
*/
protected function pendingCommand(SymfonyCommand $command): SymfonyCommand
{
/** @var null|Command $annotation */
$annotation = AnnotationCollector::getClassAnnotation($command::class, Command::class) ?? null;
if (! $annotation) {
return $command;
}
if ($annotation->signature) {
[$name, $arguments, $options] = Parser::parse($annotation->signature);
if ($name) {
$annotation->name = $name;
}
if ($arguments) {
$annotation->arguments = array_merge($annotation->arguments, $arguments);
}
if ($options) {
$annotation->options = array_merge($annotation->options, $options);
}
}
if ($annotation->name) {
$command->setName($annotation->name);
}
if ($annotation->arguments) {
$annotation->arguments = array_map(static function ($argument): InputArgument {
if ($argument instanceof InputArgument) {
return $argument;
}
if (is_array($argument)) {
return new InputArgument(...$argument);
}
throw new LogicException(sprintf('Invalid argument type: %s.', gettype($argument)));
}, $annotation->arguments);
$command->getDefinition()->addArguments($annotation->arguments);
}
if ($annotation->options) {
$annotation->options = array_map(static function ($option): InputOption {
if ($option instanceof InputOption) {
return $option;
}
if (is_array($option)) {
return new InputOption(...$option);
}
throw new LogicException(sprintf('Invalid option type: %s.', gettype($option)));
}, $annotation->options);
$command->getDefinition()->addOptions($annotation->options);
}
if ($annotation->description) {
$command->setDescription($annotation->description);
}
if ($annotation->aliases) {
$command->setAliases($annotation->aliases);
}
return $command;
}
}