1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: caoyangmin |
5
|
|
|
* Date: 2018/6/14 |
6
|
|
|
* Time: 下午6:11 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PhpBoot; |
10
|
|
|
|
11
|
|
|
use DI\FactoryInterface; |
12
|
|
|
use DI\InvokerInterface; |
13
|
|
|
use PhpBoot\Console\ConsoleContainer; |
14
|
|
|
use PhpBoot\Console\ConsoleContainerBuilder; |
15
|
|
|
use PhpBoot\DI\Traits\EnableDIAnnotations; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class Console extends \Symfony\Component\Console\Application |
21
|
|
|
{ |
22
|
|
|
use EnableDIAnnotations; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inject |
26
|
|
|
* @var ConsoleContainerBuilder |
27
|
|
|
*/ |
28
|
|
|
protected $consoleContainerBuilder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inject |
32
|
|
|
* @var InvokerInterface |
33
|
|
|
*/ |
34
|
|
|
private $diInvoker; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param FactoryInterface $factory |
38
|
|
|
* @return Console |
39
|
|
|
* @throws \DI\DependencyException |
40
|
|
|
* @throws \DI\NotFoundException |
41
|
|
|
*/ |
42
|
|
|
static public function create(FactoryInterface $factory) |
43
|
|
|
{ |
44
|
|
|
return $factory->make(self::class); |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* @param $className |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
1 |
|
public function loadCommandsFromClass($className) |
51
|
|
|
{ |
52
|
1 |
|
$console = null; |
53
|
1 |
|
$container = null; |
54
|
|
|
|
55
|
1 |
|
$container = $this->consoleContainerBuilder->build($className); |
56
|
|
|
/**@var ConsoleContainer $container*/ |
57
|
1 |
|
foreach ($container->getCommands() as $name => $command) { |
58
|
|
|
$command->setCode(function (InputInterface $input, OutputInterface $output)use ($container, $command){ |
59
|
1 |
|
return $this->diInvoker->call([$command, 'invoke'], ['container'=>$container, 'input'=>$input, 'output'=>$output]); |
60
|
1 |
|
}); |
61
|
1 |
|
$this->add($command); |
62
|
1 |
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $fromPath |
67
|
|
|
* @param string $namespace |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function loadCommandsFromPath($fromPath, $namespace = '') |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$dir = @dir($fromPath) or abort("dir $fromPath not exist"); |
73
|
|
|
|
74
|
|
|
$getEach = function () use ($dir) { |
75
|
|
|
$name = $dir->read(); |
76
|
|
|
if (!$name) { |
77
|
|
|
return $name; |
78
|
|
|
} |
79
|
|
|
return $name; |
80
|
|
|
}; |
81
|
|
|
|
82
|
|
|
while (!!($entry = $getEach())) { |
83
|
|
|
if ($entry == '.' || $entry == '..') { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
$path = $fromPath . '/' . str_replace('\\', '/', $entry); |
87
|
|
|
if (is_file($path) && substr_compare($entry, '.php', strlen($entry) - 4, 4, true) == 0) { |
88
|
|
|
$class_name = $namespace . '\\' . substr($entry, 0, strlen($entry) - 4); |
89
|
|
|
$this->loadCommandsFromClass($class_name); |
90
|
|
|
} else { |
91
|
|
|
//\Log::debug($path.' ignored'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
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.