forked from codeigniter4/shield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserModelGenerator.php
More file actions
92 lines (72 loc) · 2.28 KB
/
UserModelGenerator.php
File metadata and controls
92 lines (72 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace CodeIgniter\Shield\Commands\Generators;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\GeneratorTrait;
/**
* Generates a custom user model file.
*/
class UserModelGenerator extends BaseCommand
{
use GeneratorTrait;
/**
* @var string
*/
protected $group = 'Shield';
/**
* @var string
*/
protected $name = 'shield:model';
/**
* @var string
*/
protected $description = 'Generate a new UserModel file.';
/**
* @var string
*/
protected $usage = 'shield:model [<name>] [options]';
/**
* @var array<string, string>
*/
protected $arguments = [
'name' => 'The model class name. If not provided, this will default to `UserModel`.',
];
/**
* @var array<string, string>
*/
protected $options = [
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
'--suffix' => 'Append the component title to the class name (e.g. User => UserModel).',
'--force' => 'Force overwrite existing file.',
];
/**
* Actually execute the command.
*/
public function run(array $params): void
{
$this->component = 'Model';
$this->directory = 'Models';
$this->template = 'usermodel.tpl.php';
$this->classNameLang = 'CLI.generator.className.model';
$this->setHasClassName(false);
$class = $params[0] ?? CLI::getSegment(2) ?? 'UserModel';
if (! $this->verifyChosenModelClassName($class, $params)) {
CLI::error('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', 'light_gray', 'red');
return; // @TODO when CI4 is at v4.3+, change this to `return 1;` to signify failing exit
}
$params[0] = $class;
$this->execute($params);
}
/**
* The chosen class name should not conflict with the alias of the parent class.
*/
private function verifyChosenModelClassName(string $class, array $params): bool
{
helper('inflector');
if (array_key_exists('suffix', $params) && ! strripos($class, 'Model')) {
$class .= 'Model';
}
return strtolower(pascalize($class)) !== 'shieldusermodel';
}
}