Skip to content

Commit 7d8d825

Browse files
committed
Update ControllerCommand.php
1 parent 27d841a commit 7d8d825

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

framework/Console/ControllerCommand.php

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,59 @@ public static function make($argv)
2323

2424
// split the controller name by '/'
2525
$controllerNameArray = explode('/', $controllerNameArgv);
26-
$controllerName = end($controllerNameArray);
27-
$pattern = '/Controller$/';
28-
if (!preg_match($pattern, $controllerName)) {
29-
print_r('not match');
30-
$controllerName .= 'Controller';
31-
}
26+
$controllerNameArray = array_map(function ($item) {
27+
return ucfirst($item);
28+
}, $controllerNameArray);
29+
30+
$controllerNameSpace = "LaraCore\\App\\Http\\Controllers";
31+
$controllersBasePath = 'app/Http/Controllers';
3232

33-
// create file path
34-
$filePath = null;
35-
$replaceNamespace = null;
3633
if (count($controllerNameArray) > 1) {
37-
$newArray = array_slice($controllerNameArray, 0, -1);
38-
$replaceNamespace = '\\' . str_replace('/', '\\', $newArray);
39-
print_r(['$replaceNamespace' => $replaceNamespace]);
40-
$filePath = DS . implode(DS, $controllerNameArray);
34+
$controllerName = end($controllerNameArray);
35+
$pattern = '/Controller$/';
36+
if (!preg_match($pattern, $controllerName)) {
37+
$controllerName .= 'Controller';
38+
}
39+
$controllerNameSpace .= '\\' . str_replace('/', '\\', implode('/', array_slice($controllerNameArray, 0, -1)));
40+
$controllersBasePath .= '/' . implode('/', array_slice($controllerNameArray, 0, -1));
41+
} else {
42+
$controllerName = $controllerNameArray[0];
43+
$pattern = '/Controller$/';
44+
if (!preg_match($pattern, $controllerName)) {
45+
$controllerName .= 'Controller';
46+
}
4147
}
42-
43-
$controllerNameSpace = "LaraCore\\App\\Http\\Controllers{$replaceNamespace}";
48+
unset($controllerNameArray[end($controllerNameArray)]);
4449

4550
// Check if the controller class already exists
46-
if (class_exists($controllerNameSpace)) {
51+
$class = $controllerNameSpace . '\\' . $controllerName;
52+
if (class_exists($class)) {
4753
Log::error("Controller '$controllerName' already exists.");
4854
exit(1);
4955
}
50-
$path = ROOT . DS . 'app' . DS . 'Http' . DS . 'Controllers' . $filePath;
56+
// controller file path
57+
$controllerPath = check_or_make_dir($controllersBasePath);
58+
$controllerPath .= DS . $controllerName . '.php';
5159

52-
// Check if the Migrations directory exists, and create it if not
53-
if (!is_dir($path)) {
54-
mkdir($path);
55-
}
56-
57-
$path = $path . DS . $controllerName . '.php';
60+
// get controller stub
61+
$controllerStubPath = base_path('framework/Stub/Controller.stub');
62+
$controllerStub = file_get_contents($controllerStubPath);
5863

59-
$controllerStub = file_get_contents(ROOT . DS . 'framework' . DS . 'Stub' . DS . 'Controller.stub');
60-
print_r([
61-
$path,
62-
$replaceNamespace,
63-
]);
64-
exit;
65-
Log::info("Controller file created: $path");
64+
Log::info("Controller file created...: $controllerPath");
6665

66+
// replace stub
6767
$controllerContent = str_replace(
6868
['{{Namespace}}', '{{ControllerName}}'],
6969
[$controllerNameSpace, $controllerName],
7070
$controllerStub
7171
);
7272

73-
$createFile = file_put_contents($path, $controllerContent);
73+
$createFile = file_put_contents($controllerPath, $controllerContent);
7474
if (!$createFile) {
75-
Log::error("Controller $controllerName not created.");
75+
Log::error("Controller '$controllerName' not created.");
7676
exit(1);
7777
}
78-
Log::success("Controller $controllerName created successfully.");
79-
}
80-
81-
/**
82-
* Summary of createMigrationFile
83-
* @param mixed $migrationName
84-
* @return void
85-
*/
86-
private static function createMigrationFile($migrationName)
87-
{
88-
78+
Log::success("Controller '$controllerName' created successfully.");
8979
}
9080

9181
}

0 commit comments

Comments
 (0)