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