ClassmapController.php 2.56 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\build\controllers;

use yii\console\Controller;
11
use yii\console\Exception;
Qiang Xue committed
12 13 14
use yii\helpers\FileHelper;

/**
15 16
 * Creates a class map for the core Yii classes.
 *
Qiang Xue committed
17 18 19
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
20
class ClassmapController extends Controller
Qiang Xue committed
21
{
22
    public $defaultAction = 'create';
Qiang Xue committed
23

24 25
    /**
     * Creates a class map for the core Yii classes.
26 27
     * @param string $root    the root path of Yii framework. Defaults to YII2_PATH.
     * @param string $mapFile the file to contain the class map. Defaults to YII2_PATH . '/classes.php'.
28 29 30 31
     */
    public function actionCreate($root = null, $mapFile = null)
    {
        if ($root === null) {
32
            $root = YII2_PATH;
33 34 35
        }
        $root = FileHelper::normalizePath($root);
        if ($mapFile === null) {
36
            $mapFile = YII2_PATH . '/classes.php';
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        }
        $options = [
            'filter' => function ($path) {
                if (is_file($path)) {
                    $file = basename($path);
                    if ($file[0] < 'A' || $file[0] > 'Z') {
                        return false;
                    }
                }

                return null;
            },
            'only' => ['*.php'],
            'except' => [
                '/Yii.php',
                '/BaseYii.php',
                '/console/',
            ],
        ];
        $files = FileHelper::findFiles($root, $options);
        $map = [];
        foreach ($files as $file) {
            if (strpos($file, $root) !== 0) {
                throw new Exception("Something wrong: $file\n");
            }
            $path = str_replace('\\', '/', substr($file, strlen($root)));
63
            $map[$path] = "  'yii" . substr(str_replace('/', '\\', $path), 0, -4) . "' => YII2_PATH . '$path',";
64 65 66 67
        }
        ksort($map);
        $map = implode("\n", $map);
        $output = <<<EOD
Qiang Xue committed
68 69 70 71
<?php
/**
 * Yii core class map.
 *
72
 * This file is automatically generated by the "build classmap" command under the "build" folder.
Qiang Xue committed
73 74 75 76 77 78 79
 * Do not modify it directly.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

Alexander Makarov committed
80
return [
Qiang Xue committed
81
$map
Alexander Makarov committed
82
];
Qiang Xue committed
83 84

EOD;
85 86 87 88 89 90 91
        if (is_file($mapFile) && file_get_contents($mapFile) === $output) {
            echo "Nothing changed.\n";
        } else {
            file_put_contents($mapFile, $output);
            echo "Class map saved in $mapFile\n";
        }
    }
Qiang Xue committed
92
}