Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

ClassmapController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 74
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\build\controllers;
9
10
use yii\console\Controller;
11
use yii\console\Exception;
12
use yii\helpers\FileHelper;
13
14
/**
15
 * Creates a class map for the core Yii classes.
16
 *
17
 * @author Qiang Xue <[email protected]>
18
 * @since 2.0
19
 */
20
class ClassmapController extends Controller
21
{
22
    public $defaultAction = 'create';
23
24
    /**
25
     * Creates a class map for the core Yii classes.
26
     * @param string $root    the root path of Yii framework. Defaults to YII2_PATH.
27
     * @param string $mapFile the file to contain the class map. Defaults to YII2_PATH . '/classes.php'.
28
     */
29
    public function actionCreate($root = null, $mapFile = null)
30
    {
31
        if ($root === null) {
32
            $root = YII2_PATH;
33
        }
34
        $root = FileHelper::normalizePath($root);
35
        if ($mapFile === null) {
36
            $mapFile = YII2_PATH . '/classes.php';
37
        }
38
        $options = [
39
            'filter' => function ($path) {
40
                if (is_file($path)) {
41
                    $file = basename($path);
42
                    if ($file[0] < 'A' || $file[0] > 'Z') {
43
                        return false;
44
                    }
45
                }
46
47
                return null;
48
            },
49
            'only' => ['*.php'],
50
            'except' => [
51
                '/Yii.php',
52
                '/BaseYii.php',
53
                '/console/',
54
                '/requirements/',
55
            ],
56
        ];
57
        $files = FileHelper::findFiles($root, $options);
58
        $map = [];
59
        foreach ($files as $file) {
60
            if (strpos($file, $root) !== 0) {
61
                throw new Exception("Something wrong: $file\n");
62
            }
63
            $path = str_replace('\\', '/', substr($file, \strlen($root)));
64
            $map[$path] = "  'yii" . substr(str_replace('/', '\\', $path), 0, -4) . "' => YII2_PATH . '$path',";
65
        }
66
        ksort($map);
67
        $map = implode("\n", $map);
68
        $output = <<<EOD
69
<?php
70
/**
71
 * Yii core class map.
72
 *
73
 * This file is automatically generated by the "build classmap" command under the "build" folder.
74
 * Do not modify it directly.
75
 *
76
 * @link http://www.yiiframework.com/
77
 * @copyright Copyright (c) 2008 Yii Software LLC
78
 * @license http://www.yiiframework.com/license/
79
 */
80
81
return [
82
$map
83
];
84
85
EOD;
86
        if (is_file($mapFile) && file_get_contents($mapFile) === $output) {
87
            echo "Nothing changed.\n";
88
        } else {
89
            file_put_contents($mapFile, $output);
90
            echo "Class map saved in $mapFile\n";
91
        }
92
    }
93
}
94