CommandDiscovererProvider::provide()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.0777
c 0
b 0
f 0
cc 6
nc 8
nop 2
1
<?php
2
3
/*
4
 * This file is part of the jade/jade package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jade\Console;
13
14
use Jade\CommandProviderInterface;
15
use Psr\Container\ContainerInterface;
16
use Zend\Stdlib\Glob;
17
18
class CommandDiscovererProvider implements CommandProviderInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $dstDir;
24
25
    /**
26
     * @var string
27
     */
28
    protected $namespace;
29
30
    public function __construct($namespace, $dstDir)
31
    {
32
        $this->namespace = rtrim($namespace, '\\');
33
        $this->dstDir = rtrim($dstDir, '\\/');
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function provide(Application $app, ContainerInterface $container)
40
    {
41
        foreach (Glob::glob("{$this->dstDir}/*Command.php") as $file) {
42
            $commands = [];
43
            try {
44
                $class = $this->namespace . pathinfo($file, PATHINFO_FILENAME);
45
                $r = new \ReflectionClass($class);
46
                if ($r->isSubclassOf('Symfony\\Component\\Console\\Command\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
47
                    $command = $r->newInstance();
48
                    $commands[] = $command;
49
                }
50
            } catch (\ReflectionException $e) {
51
                // 忽略无法反射的命令
52
            }
53
            $app->addCommands($commands);
54
        }
55
    }
56
}