Skip to content

Commit e780d65

Browse files
committed
✨ add non module model to
be created with console command Signed-off-by: otengkwame <[email protected]>
1 parent 86baf37 commit e780d65

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Core/controllers/commands/Create.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class Create extends ConsoleController
2727
*/
2828
private $fileExtention = '.php';
2929

30+
/**
31+
* Set Namespace variable
32+
*
33+
* @var string
34+
*/
35+
private $namespace = '';
36+
3037
/**
3138
* Config variable
3239
*
@@ -239,6 +246,10 @@ private function createFile($filePath, $fileType, $stubType = '')
239246

240247
$contents = ($stubType === $this->view) ? '' : "<?php \n\n";
241248

249+
if (!empty($this->namespace)) {
250+
$contents .= "namespace " . $this->namespace . "; \n\n";
251+
}
252+
242253
$fileContent = $this->fileContent($fileType, $className, $stubType);
243254
$contents .= $fileContent;
244255

@@ -901,6 +912,49 @@ public function createNonModuleController($controllerName = '', $addController =
901912
}
902913
}
903914

915+
/**
916+
* Create A Non Module Model
917+
*
918+
* @param string $modelName
919+
* @param string $modelType
920+
* @param string $removeModel
921+
* @param string $location
922+
* @return void
923+
*/
924+
public function createNonModuleModel($modelName = '', $modelType = '--easy', $removeModel = '', $location = 'App/Models')
925+
{
926+
927+
$created = '';
928+
$namespace = 'App\Models';
929+
$modelName = ucwords($modelName);
930+
931+
$this->model = 'Models';
932+
$this->namespace = $namespace;
933+
$modelDirectory = $this->createAppRootDirectory($this->model);
934+
935+
if ($removeModel == '--remove-model') {
936+
$modelName = $modelName;
937+
} else {
938+
$modelName = Inflector::singularize($modelName) . 'Model';
939+
}
940+
941+
if (file_exists($modelDirectory . DS . $modelName . $this->fileExtention)) {
942+
$this->failureOutput(ucfirst($modelName) . " exists already in the " . $location . " directory");
943+
return;
944+
}
945+
946+
if ($modelDirectory && is_dir($modelDirectory)) {
947+
$filePath = $modelDirectory . DS . $modelName;
948+
$modelType = str_replace('-', '', $modelType);
949+
$created = $this->createFile($filePath, strtolower($modelType . '_') . 'model', $this->model);
950+
}
951+
952+
if ($created) {
953+
$this->successOutput(ucfirst($modelName) . " " .ucfirst($modelType) . " Model created successfully ");
954+
return;
955+
}
956+
}
957+
904958
/**
905959
* Create Model
906960
*

Core/core/Console/Console.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ protected static function createModel(...$args)
388388
$module = $args[0];
389389
$modelName = '';
390390

391+
$nonModuleModel = str_contains($args[0], '--name');
392+
393+
if ($nonModuleModel) {
394+
$args[3] = $args[2];
395+
$args[2] = $args[1];
396+
$args[1] = $args[0];
397+
}
398+
391399
$model = str_replace('=', ':', $args[1]);
392400
$model = explode(':', $model);
393401

@@ -420,6 +428,12 @@ protected static function createModel(...$args)
420428
exit;
421429
}
422430

431+
if ($nonModuleModel) {
432+
$command = Console::phpCommand() . 'create/createnonmodulemodel/' . $modelName . '/' . $modelType. '/' . $removeModel;
433+
static::runSystemCommand($command);
434+
return;
435+
}
436+
423437
$module = str_replace('=', ':', $module);
424438
$command = Console::phpCommand() . 'create/createmodel/' . $module . '/' . $modelName . '/' . $modelType. '/' . $removeModel;
425439
static::runSystemCommand($command);

0 commit comments

Comments
 (0)