Skip to content

Commit 57986ba

Browse files
committed
refactor: extract BaseCommand
1 parent 2196278 commit 57986ba

3 files changed

Lines changed: 81 additions & 117 deletions

File tree

src/Commands/BaseCommand.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\Shield\Commands;
6+
7+
use CodeIgniter\CLI\BaseCommand as FrameworkBaseCommand;
8+
use CodeIgniter\Shield\Commands\Utils\InputOutput;
9+
10+
abstract class BaseCommand extends FrameworkBaseCommand
11+
{
12+
protected static ?InputOutput $io = null;
13+
14+
/**
15+
* The group the command is lumped under
16+
* when listing commands.
17+
*
18+
* @var string
19+
*/
20+
protected $group = 'Shield';
21+
22+
/**
23+
* Asks the user for input.
24+
*
25+
* @param string $field Output "field" question
26+
* @param array|string $options String to a default value, array to a list of options (the first option will be the default value)
27+
* @param array|string $validation Validation rules
28+
*
29+
* @return string The user input
30+
*/
31+
protected function prompt(string $field, $options = null, $validation = null): string
32+
{
33+
return self::$io->prompt($field, $options, $validation);
34+
}
35+
36+
/**
37+
* Outputs a string to the cli on its own line.
38+
*/
39+
protected function write(
40+
string $text = '',
41+
?string $foreground = null,
42+
?string $background = null
43+
): void {
44+
self::$io->write($text, $foreground, $background);
45+
}
46+
47+
/**
48+
* Outputs an error to the CLI using STDERR instead of STDOUT
49+
*/
50+
protected function error(
51+
string $text,
52+
string $foreground = 'light_red',
53+
?string $background = null
54+
): void {
55+
self::$io->error($text, $foreground, $background);
56+
}
57+
58+
protected function ensureInputOutput(): void
59+
{
60+
if (self::$io === null) {
61+
self::$io = new InputOutput();
62+
}
63+
}
64+
65+
/**
66+
* @internal Testing purpose only
67+
*/
68+
public static function setInputOutput(InputOutput $io): void
69+
{
70+
self::$io = $io;
71+
}
72+
73+
/**
74+
* @internal Testing purpose only
75+
*/
76+
public static function resetInputOutput(): void
77+
{
78+
self::$io = null;
79+
}
80+
}

src/Commands/Setup.php

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,15 @@
44

55
namespace CodeIgniter\Shield\Commands;
66

7-
use CodeIgniter\CLI\BaseCommand;
87
use CodeIgniter\CLI\CLI;
9-
use CodeIgniter\CodeIgniter;
108
use CodeIgniter\Commands\Database\Migrate;
119
use CodeIgniter\Shield\Commands\Setup\ContentReplacer;
12-
use CodeIgniter\Shield\Commands\Utils\InputOutput;
1310
use CodeIgniter\Test\Filters\CITestStreamFilter;
1411
use Config\Email as EmailConfig;
1512
use Config\Services;
1613

1714
class Setup extends BaseCommand
1815
{
19-
private static ?InputOutput $io = null;
20-
21-
/**
22-
* The group the command is lumped under
23-
* when listing commands.
24-
*
25-
* @var string
26-
*/
27-
protected $group = 'Shield';
28-
2916
/**
3017
* The Command's name
3118
*
@@ -402,48 +389,4 @@ private function runMigrations(): void
402389

403390
CITestStreamFilter::$buffer = '';
404391
}
405-
406-
private function prompt(string $field, $options = null, $validation = null): string
407-
{
408-
return self::$io->prompt($field, $options, $validation);
409-
}
410-
411-
private function write(
412-
string $text = '',
413-
?string $foreground = null,
414-
?string $background = null
415-
): void {
416-
self::$io->write($text, $foreground, $background);
417-
}
418-
419-
private function error(
420-
string $text,
421-
string $foreground = 'light_red',
422-
?string $background = null
423-
): void {
424-
self::$io->error($text, $foreground, $background);
425-
}
426-
427-
private function ensureInputOutput(): void
428-
{
429-
if (self::$io === null) {
430-
self::$io = new InputOutput();
431-
}
432-
}
433-
434-
/**
435-
* @internal Testing purpose only
436-
*/
437-
public static function setInputOutput(InputOutput $io): void
438-
{
439-
self::$io = $io;
440-
}
441-
442-
/**
443-
* @internal Testing purpose only
444-
*/
445-
public static function resetInputOutput(): void
446-
{
447-
self::$io = null;
448-
}
449392
}

src/Commands/User.php

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
namespace CodeIgniter\Shield\Commands;
66

7-
use CodeIgniter\CLI\BaseCommand;
87
use CodeIgniter\Shield\Authentication\Authenticators\Session;
98
use CodeIgniter\Shield\Commands\Exceptions\BadInputException;
109
use CodeIgniter\Shield\Commands\Exceptions\CancelException;
11-
use CodeIgniter\Shield\Commands\Utils\InputOutput;
1210
use CodeIgniter\Shield\Config\Auth;
1311
use CodeIgniter\Shield\Entities\User as UserEntity;
1412
use CodeIgniter\Shield\Exceptions\UserNotFoundException;
@@ -18,20 +16,11 @@
1816

1917
class User extends BaseCommand
2018
{
21-
private static ?InputOutput $io = null;
22-
private array $validActions = [
19+
private array $validActions = [
2320
'create', 'activate', 'deactivate', 'changename', 'changeemail',
2421
'delete', 'password', 'list', 'addgroup', 'removegroup',
2522
];
2623

27-
/**
28-
* The group the command is lumped under
29-
* when listing commands.
30-
*
31-
* @var string
32-
*/
33-
protected $group = 'Shield';
34-
3524
/**
3625
* Command's name
3726
*
@@ -251,31 +240,6 @@ private function setValidationRules(): void
251240
];
252241
}
253242

254-
/**
255-
* Asks the user for input.
256-
*
257-
* @param string $field Output "field" question
258-
* @param array|string $options String to a default value, array to a list of options (the first option will be the default value)
259-
* @param array|string $validation Validation rules
260-
*
261-
* @return string The user input
262-
*/
263-
private function prompt(string $field, $options = null, $validation = null): string
264-
{
265-
return self::$io->prompt($field, $options, $validation);
266-
}
267-
268-
/**
269-
* Outputs a string to the cli on its own line.
270-
*/
271-
private function write(
272-
string $text = '',
273-
?string $foreground = null,
274-
?string $background = null
275-
): void {
276-
self::$io->write($text, $foreground, $background);
277-
}
278-
279243
/**
280244
* Create a new user
281245
*
@@ -692,27 +656,4 @@ private function findUser($question = '', $username = null, $email = null): User
692656

693657
return $userModel->findById($user['id']);
694658
}
695-
696-
private function ensureInputOutput(): void
697-
{
698-
if (self::$io === null) {
699-
self::$io = new InputOutput();
700-
}
701-
}
702-
703-
/**
704-
* @internal Testing purpose only
705-
*/
706-
public static function setInputOutput(InputOutput $io): void
707-
{
708-
self::$io = $io;
709-
}
710-
711-
/**
712-
* @internal Testing purpose only
713-
*/
714-
public static function resetInputOutput(): void
715-
{
716-
self::$io = null;
717-
}
718659
}

0 commit comments

Comments
 (0)