Skip to content

Commit 718dd4e

Browse files
committed
Make the class name input optional with default.
1 parent fbadd42 commit 718dd4e

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

docs/concepts.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ command in the terminal:
3636
php spark shield:model UserModel
3737
```
3838

39+
The class name is optional. If none is provided, the generated class name would be `UserModel`.
40+
3941
You should set `Config\Auth::$userProvider` as follows:
4042

4143
```php

src/Commands/Generators/UserModelGenerator.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CodeIgniter\Shield\Commands\Generators;
66

77
use CodeIgniter\CLI\BaseCommand;
8+
use CodeIgniter\CLI\CLI;
89
use CodeIgniter\CLI\GeneratorTrait;
910

1011
/**
@@ -32,15 +33,13 @@ class UserModelGenerator extends BaseCommand
3233
/**
3334
* @var string
3435
*/
35-
protected $usage = 'shield:model <name> [options]';
36+
protected $usage = 'shield:model [<name>] [options]';
3637

3738
/**
38-
* The Command's Arguments
39-
*
4039
* @var array<string, string>
4140
*/
4241
protected $arguments = [
43-
'name' => 'The model class name.',
42+
'name' => 'The model class name. If not provided, this will default to `UserModel`.',
4443
];
4544

4645
/**
@@ -62,6 +61,17 @@ public function run(array $params): void
6261
$this->template = 'usermodel.tpl.php';
6362

6463
$this->classNameLang = 'CLI.generator.className.model';
64+
$this->setHasClassName(false);
65+
66+
$class = $params[0] ?? CLI::getSegment(2) ?? 'UserModel';
67+
68+
if (in_array(strtolower($class), ['shielduser', 'shieldusermodel'], true)) {
69+
CLI::error('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', 'light_gray', 'red');
70+
71+
return; // @TODO when CI4 is at v4.3+, change this to `return 1;` to signify failing exit
72+
}
73+
74+
$params[0] = $class;
6575

6676
$this->execute($params);
6777
}

tests/Commands/UserModelGeneratorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,30 @@ public function testGenerateUserModelWithSuffix(): void
113113
$this->assertFileExists($filepath);
114114
$this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath));
115115
}
116+
117+
public function testGenerateUserModelWithoutClassNameInput(): void
118+
{
119+
command('shield:model');
120+
121+
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
122+
123+
$filepath = APPPATH . 'Models/UserModel.php';
124+
$this->assertFileExists($filepath);
125+
$this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath));
126+
}
127+
128+
public function testGenerateUserCannotAcceptShieldUserModelAsInput(): void
129+
{
130+
command('shield:model ShieldUserModel');
131+
132+
$this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer);
133+
$this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php');
134+
135+
CITestStreamFilter::$buffer = '';
136+
137+
command('shield:model ShieldUser --suffix');
138+
139+
$this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer);
140+
$this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php');
141+
}
116142
}

0 commit comments

Comments
 (0)