|
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 PhpBoot\Console\ConsoleContainer; |
|
12
|
|
|
use PhpBoot\Console\ConsoleContainerBuilder; |
|
13
|
|
|
use PhpBoot\DI\Traits\EnableDIAnnotations; |
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
|
|
18
|
|
|
class Console extends \Symfony\Component\Console\Application |
|
19
|
|
|
{ |
|
20
|
|
|
use EnableDIAnnotations; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inject |
|
24
|
|
|
* @var ConsoleContainerBuilder |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $consoleContainerBuilder; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $className |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function loadCommandsFromClass($className) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$console = null; |
|
|
|
|
|
|
36
|
1 |
|
$container = null; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
1 |
|
$container = $this->consoleContainerBuilder->build($className); |
|
39
|
|
|
/**@var ConsoleContainer $container*/ |
|
40
|
1 |
|
foreach ($container->getCommands() as $name => $command) { |
|
41
|
|
|
$command->setCode(function (InputInterface $input, OutputInterface $output)use ($container, $command){ |
|
42
|
1 |
|
return $command->invoke($container, $input, $output); |
|
43
|
1 |
|
}); |
|
44
|
1 |
|
$this->add($command); |
|
45
|
1 |
|
} |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $fromPath |
|
50
|
|
|
* @param string $namespace |
|
51
|
|
|
* @throws \Exception |
|
52
|
|
|
*/ |
|
53
|
|
View Code Duplication |
public function loadRoutesFromPath($fromPath, $namespace = '') |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$dir = @dir($fromPath) or abort("dir $fromPath not exist"); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$getEach = function () use ($dir) { |
|
58
|
|
|
$name = $dir->read(); |
|
59
|
|
|
if (!$name) { |
|
60
|
|
|
return $name; |
|
61
|
|
|
} |
|
62
|
|
|
return $name; |
|
63
|
|
|
}; |
|
64
|
|
|
|
|
65
|
|
|
while (!!($entry = $getEach())) { |
|
66
|
|
|
if ($entry == '.' || $entry == '..') { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
$path = $fromPath . '/' . str_replace('\\', '/', $entry); |
|
70
|
|
|
if (is_file($path) && substr_compare($entry, '.php', strlen($entry) - 4, 4, true) == 0) { |
|
71
|
|
|
$class_name = $namespace . '\\' . substr($entry, 0, strlen($entry) - 4); |
|
72
|
|
|
$this->loadCommandsFromClass($class_name); |
|
73
|
|
|
} else { |
|
|
|
|
|
|
74
|
|
|
//\Log::debug($path.' ignored'); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.