Skip to content

Commit c48cb81

Browse files
authored
Merge pull request #368 from kenjis/autoload-helpers
feat: autoload helpers by Composer
2 parents 1e9bd45 + 5cb4047 commit c48cb81

File tree

19 files changed

+49
-29
lines changed

19 files changed

+49
-29
lines changed

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
},
3838
"exclude-from-classmap": [
3939
"**/Database/Migrations/**"
40+
],
41+
"files": [
42+
"src/Helpers/auth_helper.php",
43+
"src/Helpers/email_helper.php"
4044
]
4145
},
4246
"autoload-dev": {

docs/authentication.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ helper method provides the `auth()` command which returns a convenient interface
5454
used functionality within the auth libraries. This must be loaded before it can be used.
5555

5656
```php
57-
helper('auth');
58-
5957
// get the current user
6058
auth()->user();
6159

@@ -65,6 +63,10 @@ auth()->id();
6563
user_id();
6664
```
6765

66+
> **Note**
67+
> The `auth_helper` is autoloaded by Composer. If you want to *override* the functions,
68+
> you need to define them in `app/Common.php`.
69+
6870
## Authenticator Responses
6971

7072
Many of the authenticator methods will return a `CodeIgniter\Shield\Result` class. This provides a consistent

docs/install.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ class Auth extends ShieldAuth
117117
}
118118
```
119119

120-
2. **Helper Setup** The `auth` and `setting` helpers need to be included in almost every page. The simplest way to do this is to add them to the `BaseController::initController` method:
120+
2. **Helper Setup** The `setting` helper needs to be included in almost every page. The simplest way to do this is to add them to the `BaseController::initController` method:
121121

122122
```php
123123
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
124124
{
125-
$this->helpers = array_merge($this->helpers, ['auth', 'setting']);
125+
$this->helpers = array_merge($this->helpers, ['setting']);
126126

127127
// Do Not Edit This Line
128128
parent::initController($request, $response, $logger);

src/Authentication/Actions/Email2FA.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function handle(IncomingRequest $request)
7070
}
7171

7272
// Send the user an email with the code
73-
helper('email');
7473
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
7574
$email->setTo($user->email);
7675
$email->setSubject(lang('Auth.email2FASubject'));

src/Authentication/Actions/EmailActivator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function show(): string
4040
$code = $this->createIdentity($user);
4141

4242
// Send the email
43-
helper('email');
4443
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
4544
$email->setTo($userEmail);
4645
$email->setSubject(lang('Auth.emailActivateSubject'));

src/Commands/Setup.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,44 @@ protected function add(string $file, string $code, string $pattern, string $repl
195195
}
196196
}
197197

198+
/**
199+
* @param string $file Relative file path like 'Controllers/BaseController.php'.
200+
* @param array $replaces [search => replace]
201+
*/
202+
protected function replace(string $file, array $replaces): void
203+
{
204+
$path = $this->distPath . $file;
205+
$cleanPath = clean_path($path);
206+
207+
$content = file_get_contents($path);
208+
209+
$output = $this->replacer->replace($content, $replaces);
210+
211+
if ($output === $content) {
212+
CLI::error(" Skipped {$cleanPath}. It has already been updated.");
213+
214+
return;
215+
}
216+
217+
if (write_file($path, $output)) {
218+
CLI::write(CLI::color(' Updated: ', 'green') . $cleanPath);
219+
} else {
220+
CLI::error(" Error updating {$cleanPath}.");
221+
}
222+
}
223+
198224
private function setupHelper(): void
199225
{
200-
$file = 'Controllers/BaseController.php';
226+
$file = 'Controllers/BaseController.php';
227+
$check = '$this->helpers = array_merge($this->helpers, [\'setting\']);';
228+
229+
// Replace old helper setup
230+
$replaces = [
231+
'$this->helpers = array_merge($this->helpers, [\'auth\', \'setting\']);' => $check,
232+
];
233+
$this->replace($file, $replaces);
201234

202-
$check = '$this->helpers = array_merge($this->helpers, [\'auth\', \'setting\']);';
235+
// Add helper setup
203236
$pattern = '/(' . preg_quote('// Do Not Edit This Line', '/') . ')/u';
204237
$replace = $check . "\n\n " . '$1';
205238

src/Controllers/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class LoginController extends BaseController
1010
{
11-
protected $helpers = ['auth', 'setting'];
11+
protected $helpers = ['setting'];
1212

1313
/**
1414
* Displays the form the login to the site.

src/Controllers/MagicLinkController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function loginAction()
8989
]);
9090

9191
// Send the user an email with the code
92-
helper('email');
9392
$email = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '');
9493
$email->setTo($user->email);
9594
$email->setSubject(lang('Auth.magicLinkSubject'));

src/Entities/AccessToken.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class AccessToken extends Entity
3636
public function user(): ?User
3737
{
3838
if ($this->user === null) {
39-
helper('auth');
40-
4139
$users = auth()->getProvider();
4240
$this->user = $users->findById($this->user_id);
4341
}

src/Filters/ChainAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ChainAuth implements FilterInterface
3333
*/
3434
public function before(RequestInterface $request, $arguments = null)
3535
{
36-
helper(['auth', 'settings']);
36+
helper('settings');
3737

3838
$chain = config('Auth')->authenticationChain;
3939

0 commit comments

Comments
 (0)