Skip to content

Commit 7774592

Browse files
authored
Merge pull request #893 from kenjis/remove-autoload-auth_helper
refactor: remove Composer autoloading of auth helper
2 parents 8102411 + dbd6f5c commit 7774592

File tree

21 files changed

+97
-76
lines changed

21 files changed

+97
-76
lines changed

UPGRADING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
# Upgrade Guide
22

3+
## Version 1.0.0-beta.7 to 1.0.0-beta.8
4+
5+
### Mandatory Config Changes
6+
7+
#### Helper Autoloading
8+
9+
Helper autoloading has been changed to be done by CodeIgniter's autoloader
10+
instead of Composer.
11+
12+
So you need to update the settings. Run `php spark shield:setup` again, and the
13+
following steps will be done.
14+
15+
1. Add `auth` and `setting` to the `$helpers` array in **app/Config/Autoload.php**:
16+
17+
```php
18+
public $helpers = ['auth', 'setting'];
19+
```
20+
21+
2. Remove the following code in the `initController()` method in
22+
`**app/Controllers/BaseController.php**:
23+
24+
```php
25+
$this->helpers = array_merge($this->helpers, ['setting']);
26+
```
27+
328
## Version 1.0.0-beta.6 to 1.0.0-beta.7
429

530
### The minimum CodeIgniter version

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252
"psr-4": {
5353
"CodeIgniter\\Shield\\": "src"
5454
},
55-
"files": [
56-
"src/Helpers/auth_helper.php"
57-
],
5855
"exclude-from-classmap": [
5956
"**/Database/Migrations/**"
6057
]

docs/getting_started/install.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ your project.
7272

7373
### Manual Setup
7474

75-
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.
75+
1. **Config Setup:**
76+
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.
7677

7778
```php
7879
// new file - app/Config/Auth.php
@@ -91,29 +92,26 @@ your project.
9192
}
9293
```
9394

94-
2. **Helper Setup** The `setting` helper needs to be included in almost every page. The simplest way to do this is to add it to the `BaseController::initController()` method:
95+
2. **Helper Setup:**
96+
The `auth` and `setting` helpers need to be included in almost every page.
97+
The simplest way to do this is to add it to the **app/Config/Autoload.php** file:
9598

9699
```php
97-
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
98-
{
99-
$this->helpers = array_merge($this->helpers, ['setting']);
100-
101-
// Do Not Edit This Line
102-
parent::initController($request, $response, $logger);
103-
}
100+
public $helpers = ['auth', 'setting'];
104101
```
105102

106-
This requires that all of your controllers extend the `BaseController`, but that's a good practice anyway.
107-
108-
3. **Routes Setup** The default auth routes can be setup with a single call in **app/Config/Routes.php**:
103+
3. **Routes Setup:**
104+
The default auth routes can be setup with a single call in **app/Config/Routes.php**:
109105

110106
```php
111107
service('auth')->routes($routes);
112108
```
113109

114-
4. **Security Setup** Set `Config\Security::$csrfProtection` to `'session'` for security reasons, if you use Session Authenticator.
110+
4. **Security Setup:**
111+
Set `Config\Security::$csrfProtection` to `'session'` for security reasons, if you use Session Authenticator.
115112

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).
113+
5. **Email Setup:**
114+
Configure **app/Config/Email.php** to allow Shield to send emails.
117115

118116
```php
119117
<?php
@@ -130,7 +128,8 @@ your project.
130128
}
131129
```
132130

133-
6. **Migration** Run the migrations.
131+
6. **Migration:**
132+
Run the migrations.
134133

135134
!!! note
136135

docs/references/authentication/authentication.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ auth()->getProvider();
4848

4949
!!! note
5050

51-
The `auth_helper` is autoloaded by Composer. If you want to *override* the functions,
52-
you need to define them in **app/Common.php**.
51+
The `auth_helper` is autoloaded by CodeIgniter's autoloader if you follow the
52+
installation instruction. If you want to *override* the functions, create
53+
**app/Helpers/auth_helper.php**.
5354

5455
## Authenticator Responses
5556

src/Authentication/Authenticators/Session.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ class Session implements AuthenticatorInterface
7373

7474
public function __construct(UserModel $provider)
7575
{
76-
helper('setting');
77-
7876
$this->provider = $provider;
7977

8078
$this->loginModel = model(LoginModel::class);

src/Authorization/Groups.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
*/
1414
class Groups
1515
{
16-
public function __construct()
17-
{
18-
if (! function_exists('setting')) {
19-
helper('setting');
20-
}
21-
}
22-
2316
/**
2417
* Grabs a group info from settings.
2518
*/

src/Authorization/Traits/Authorizable.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ public function can(string ...$permissions): bool
259259
return false;
260260
}
261261

262-
$matrix = function_exists('setting')
263-
? setting('AuthGroups.matrix')
264-
: config('AuthGroups')->matrix;
262+
$matrix = setting('AuthGroups.matrix');
265263

266264
foreach ($this->groupCache as $group) {
267265
// Check exact match
@@ -396,18 +394,14 @@ private function saveGroupsOrPermissions(string $type, $model, array $cache): vo
396394
*/
397395
private function getConfigGroups(): array
398396
{
399-
return function_exists('setting')
400-
? array_keys(setting('AuthGroups.groups'))
401-
: array_keys(config('AuthGroups')->groups);
397+
return array_keys(setting('AuthGroups.groups'));
402398
}
403399

404400
/**
405401
* @return string[]
406402
*/
407403
private function getConfigPermissions(): array
408404
{
409-
return function_exists('setting')
410-
? array_keys(setting('AuthGroups.permissions'))
411-
: array_keys(config('AuthGroups')->permissions);
405+
return array_keys(setting('AuthGroups.permissions'));
412406
}
413407
}

src/Commands/Setup.php

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeIgniter\Commands\Database\Migrate;
99
use CodeIgniter\Shield\Commands\Setup\ContentReplacer;
1010
use CodeIgniter\Test\Filters\CITestStreamFilter;
11+
use Config\Autoload as AutoloadConfig;
1112
use Config\Email as EmailConfig;
1213
use Config\Services;
1314

@@ -78,7 +79,7 @@ private function publishConfig(): void
7879
$this->publishConfigAuthGroups();
7980
$this->publishConfigAuthToken();
8081

81-
$this->setupHelper();
82+
$this->setAutoloadHelpers();
8283
$this->setupRoutes();
8384

8485
$this->setSecurityCSRF();
@@ -236,24 +237,55 @@ private function replace(string $file, array $replaces): bool
236237
return false;
237238
}
238239

239-
private function setupHelper(): void
240+
private function setAutoloadHelpers(): void
240241
{
241-
$file = 'Controllers/BaseController.php';
242-
$check = '$this->helpers = array_merge($this->helpers, [\'setting\']);';
242+
$file = 'Config/Autoload.php';
243+
244+
$path = $this->distPath . $file;
245+
$cleanPath = clean_path($path);
246+
247+
$config = new AutoloadConfig();
248+
$helpers = $config->helpers;
249+
$newHelpers = array_unique(array_merge($helpers, ['auth', 'setting']));
250+
251+
$pattern = '/^ public \$helpers = \[.*\];/mu';
252+
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';
253+
$content = file_get_contents($path);
254+
$output = preg_replace($pattern, $replace, $content);
255+
256+
// check if the content is updated
257+
if ($output === $content) {
258+
$this->write(CLI::color(' Autoload Setup: ', 'green') . 'Everything is fine.');
259+
260+
return;
261+
}
262+
263+
if (write_file($path, $output)) {
264+
$this->write(CLI::color(' Updated: ', 'green') . $cleanPath);
265+
266+
$this->removeHelperLoadingInBaseController();
267+
} else {
268+
$this->error(" Error updating file '{$cleanPath}'.");
269+
}
270+
}
271+
272+
private function removeHelperLoadingInBaseController(): void
273+
{
274+
$file = 'Controllers/BaseController.php';
275+
276+
$check = ' $this->helpers = array_merge($this->helpers, [\'setting\']);';
243277

244278
// Replace old helper setup
245279
$replaces = [
246280
'$this->helpers = array_merge($this->helpers, [\'auth\', \'setting\']);' => $check,
247281
];
248-
if ($this->replace($file, $replaces)) {
249-
return;
250-
}
251-
252-
// Add helper setup
253-
$pattern = '/(' . preg_quote('// Do Not Edit This Line', '/') . ')/u';
254-
$replace = $check . "\n\n " . '$1';
282+
$this->replace($file, $replaces);
255283

256-
$this->add($file, $check, $pattern, $replace);
284+
// Remove helper setup
285+
$replaces = [
286+
"\n" . $check . "\n" => '',
287+
];
288+
$this->replace($file, $replaces);
257289
}
258290

259291
private function setupRoutes(): void

src/Controllers/ActionController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
class ActionController extends BaseController
1919
{
2020
protected ?ActionInterface $action = null;
21-
protected $helpers = ['setting'];
2221

2322
/**
2423
* Perform an initial check if we have a valid action or not.

src/Controllers/LoginController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class LoginController extends BaseController
1414
{
1515
use Viewable;
1616

17-
protected $helpers = ['setting'];
18-
1917
/**
2018
* Displays the form the login to the site.
2119
*

0 commit comments

Comments
 (0)