Skip to content

Commit 112ba2a

Browse files
authored
Merge pull request #876 from kenjis/feat-setup-command-email-setup
feat: `shield:setup` does Email setup
2 parents 0133ff5 + f6c1a4d commit 112ba2a

File tree

8 files changed

+315
-172
lines changed

8 files changed

+315
-172
lines changed

docs/getting_started/install.md

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ Require it with an explicit version constraint allowing its desired stability.
5454

5555
## Initial Setup
5656

57+
There are a few setup items to do before you can start using Shield in
58+
your project.
59+
5760
### Command Setup
5861

59-
1. Run the following command. This command handles steps 1-5 of *Manual Setup* and runs the migrations.
62+
1. Run the following command. This command handles steps 1-6 of *Manual Setup*.
6063

6164
```console
6265
php spark shield:setup
@@ -67,36 +70,8 @@ Require it with an explicit version constraint allowing its desired stability.
6770
If you want to customize table names, you must change the table names before running database migrations.
6871
See [Customizing Table Names](../customization/table_names.md).
6972

70-
2. Configure **app/Config/Email.php** to allow Shield to send emails with the [Email Class](https://codeigniter.com/user_guide/libraries/email.html).
71-
72-
```php
73-
<?php
74-
75-
namespace Config;
76-
77-
use CodeIgniter\Config\BaseConfig;
78-
79-
class Email extends BaseConfig
80-
{
81-
/**
82-
* @var string
83-
*/
84-
public $fromEmail = '[email protected]';
85-
86-
/**
87-
* @var string
88-
*/
89-
public $fromName = 'your name';
90-
91-
// ...
92-
}
93-
```
94-
9573
### Manual Setup
9674

97-
There are a few setup items to do before you can start using Shield in
98-
your project.
99-
10075
1. Copy the **Auth.php**, **AuthGroups.php**, and **AuthToken.php** from **vendor/codeigniter4/shield/src/Config/** into your project's config folder and update the namespace to `Config`. You will also need to have these classes extend the original classes. See the example below. These files contain all the settings, group, and permission information for your application and will need to be modified to meet the needs of your site.
10176

10277
```php
@@ -138,7 +113,24 @@ your project.
138113

139114
4. **Security Setup** Set `Config\Security::$csrfProtection` to `'session'` for security reasons, if you use Session Authenticator.
140115

141-
5. **Migration** Run the migrations.
116+
5. Configure **app/Config/Email.php** to allow Shield to send emails with the [Email Class](https://codeigniter.com/user_guide/libraries/email.html).
117+
118+
```php
119+
<?php
120+
121+
namespace Config;
122+
123+
use CodeIgniter\Config\BaseConfig;
124+
125+
class Email extends BaseConfig
126+
{
127+
public string $fromEmail = '[email protected]';
128+
public string $fromName = 'your name';
129+
// ...
130+
}
131+
```
132+
133+
6. **Migration** Run the migrations.
142134

143135
!!! note
144136

@@ -155,28 +147,3 @@ your project.
155147

156148
1. Remove sample migration files in **tests/_support/Database/Migrations/**
157149
2. Or install `sqlite3` php extension
158-
159-
6. Configure **app/Config/Email.php** to allow Shield to send emails.
160-
161-
```php
162-
<?php
163-
164-
namespace Config;
165-
166-
use CodeIgniter\Config\BaseConfig;
167-
168-
class Email extends BaseConfig
169-
{
170-
/**
171-
* @var string
172-
*/
173-
public $fromEmail = '[email protected]';
174-
175-
/**
176-
* @var string
177-
*/
178-
public $fromName = 'your name';
179-
180-
// ...
181-
}
182-
```

rector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
2121
use Rector\Config\RectorConfig;
2222
use Rector\Core\ValueObject\PhpVersion;
23+
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
2324
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
2425
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
2526
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
@@ -109,6 +110,11 @@
109110
__DIR__ . '/tests/Commands/UserModelGeneratorTest.php',
110111
__DIR__ . '/tests/Controllers/LoginTest.php',
111112
],
113+
114+
RecastingRemovalRector::class => [
115+
// To check old Email Config file
116+
__DIR__ . '/src/Commands/Setup.php',
117+
],
112118
]);
113119

114120
// auto import fully qualified class names

src/Commands/BaseCommand.php

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

0 commit comments

Comments
 (0)