Passed
Push — 5.2 ( 64d1c2...bed7ed )
by
unknown
02:57
created

ServiceDiscover::execute()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 7
nop 2
dl 0
loc 23
rs 9.5555
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\console\command;
14
15
16
use think\console\Command;
17
use think\console\Input;
18
use think\console\Output;
19
use think\facade\App;
20
21
class ServiceDiscover extends Command
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
22
{
23
    public function configure()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
24
    {
25
        $this->setName('service:discover')
26
            ->setDescription('Discover Services for ThinkPHP');
27
    }
28
29
    public function execute(Input $input, Output $output)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
30
    {
31
        if (is_file($path = App::getRootPath() . 'vendor/composer/installed.json')) {
32
            $packages = json_decode(@file_get_contents($path), true);
33
34
            $services = [];
35
            foreach ($packages as $package) {
36
                if (!empty($package['extra']['think']['services'])) {
37
                    $services = array_merge($services, (array)$package['extra']['think']['services']);
38
                }
39
            }
40
41
            $header = '// This cache file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL . 'declare (strict_types = 1);' . PHP_EOL;
42
43
            $content = '<?php ' . PHP_EOL . $header . "return " . var_export($services, true) . ';';
44
45
            if (!is_dir($runtimePath = App::getRuntimePath())) {
46
                mkdir($runtimePath, 0755, true);
47
            }
48
49
            file_put_contents($runtimePath . 'services.php', $content);
50
51
            $output->writeln('<info>Succeed!</info>');
52
        }
53
54
    }
55
}