1 | <?php |
||
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 |