Skip to content

Commit 6d79233

Browse files
Modernization and DI mailer injection. (#580)
1 parent e193746 commit 6d79233

71 files changed

Lines changed: 414 additions & 237 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/config/main.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'csrfParam' => '_csrf-backend',
1919
],
2020
'user' => [
21-
'identityClass' => 'common\models\User',
21+
'identityClass' => \common\models\User::class,
2222
'enableAutoLogin' => true,
2323
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
2424
],

backend/controllers/SiteController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace backend\controllers;
46

57
use common\models\LoginForm;
68
use Yii;
7-
use yii\filters\VerbFilter;
89
use yii\filters\AccessControl;
10+
use yii\filters\VerbFilter;
911
use yii\web\Controller;
12+
use yii\web\ErrorAction;
1013
use yii\web\Response;
1114

1215
/**
@@ -17,7 +20,7 @@ class SiteController extends Controller
1720
/**
1821
* {@inheritdoc}
1922
*/
20-
public function behaviors()
23+
public function behaviors(): array
2124
{
2225
return [
2326
'access' => [
@@ -46,11 +49,11 @@ public function behaviors()
4649
/**
4750
* {@inheritdoc}
4851
*/
49-
public function actions()
52+
public function actions(): array
5053
{
5154
return [
5255
'error' => [
53-
'class' => \yii\web\ErrorAction::class,
56+
'class' => ErrorAction::class,
5457
],
5558
];
5659
}
@@ -60,7 +63,7 @@ public function actions()
6063
*
6164
* @return string
6265
*/
63-
public function actionIndex()
66+
public function actionIndex(): string
6467
{
6568
return $this->render('index');
6669
}
@@ -70,7 +73,7 @@ public function actionIndex()
7073
*
7174
* @return string|Response
7275
*/
73-
public function actionLogin()
76+
public function actionLogin(): string|Response
7477
{
7578
if (!Yii::$app->user->isGuest) {
7679
return $this->goHome();
@@ -95,7 +98,7 @@ public function actionLogin()
9598
*
9699
* @return Response
97100
*/
98-
public function actionLogout()
101+
public function actionLogout(): Response
99102
{
100103
Yii::$app->user->logout();
101104

backend/views/site/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/** @var yii\web\View $this */
46

57
use yii\helpers\Html;

backend/views/site/login.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/** @var yii\web\View $this */
46
/** @var yii\bootstrap5\ActiveForm $form */
57
/** @var \common\models\LoginForm $model */
68

79
use yii\bootstrap5\ActiveForm;
810
use yii\bootstrap5\Html;
911

10-
$this->title = 'Login';
12+
$this->title = 'Sign in to your account';
1113
$htmlIcon = <<<HTML
12-
<div class="input-group"><span class="input-group-text" aria-hidden="true">%s</span>{input}</div>{error}{hint}
14+
{label}<div class="input-group"><span class="input-group-text" aria-hidden="true">%s</span>{input}</div>{error}{hint}
1315
HTML;
16+
$labelOptions = ['class' => 'form-label fw-semibold small'];
1417
?>
1518
<div class="card border-0 overflow-hidden login-split-card">
1619
<div class="row g-0">
@@ -54,14 +57,13 @@
5457
],
5558
) ?>
5659
</div>
57-
<h1 class="h3 fw-bold mb-1">Sign in to your account</h1>
60+
<h1 class="h3 fw-bold mb-1"><?= Html::encode($this->title) ?></h1>
5861
<p class="text-body-secondary small">Enter your credentials to access the admin panel</p>
5962
</div>
6063

6164
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
6265

6366
<div class="mb-3">
64-
<label class="form-label fw-semibold small" for="loginform-username">Your Username</label>
6567
<?= $form->field($model, 'username', [
6668
'options' => ['class' => 'mb-0'],
6769
'template' => sprintf($htmlIcon, '&#128100;'),
@@ -70,19 +72,18 @@
7072
'placeholder' => 'username',
7173
'autofocus' => true,
7274
],
73-
])->textInput() ?>
75+
])->textInput()->label('Your Username', $labelOptions) ?>
7476
</div>
7577

7678
<div class="mb-3">
77-
<label class="form-label fw-semibold small" for="loginform-password">Your Password</label>
7879
<?= $form->field($model, 'password', [
7980
'options' => ['class' => 'mb-0'],
8081
'template' => sprintf($htmlIcon, '&#128274;'),
8182
'inputOptions' => [
8283
'class' => 'form-control',
8384
'placeholder' => 'Password',
8485
],
85-
])->passwordInput() ?>
86+
])->passwordInput()->label('Your Password', $labelOptions) ?>
8687
</div>
8788

8889
<div class="mb-4">

backend/web/css/site.css

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ a.desc:after {
7070
color: var(--bs-navbar-hover-color);
7171
}
7272

73-
.navbar-nav .nav-link.logout:focus {
74-
outline: none;
75-
}
73+
7674

7775
/* footer logo: light logo by default (light bg-body-tertiary), dark logo for dark theme */
7876
.footer-logo-dark { display: none; }
@@ -108,8 +106,8 @@ a.desc:after {
108106
}
109107

110108
.login-split-card .input-group:focus-within {
111-
border-color: #86b7fe;
112-
box-shadow: 0 0 0 .25rem rgba(13, 110, 253, .25);
109+
border-color: var(--bs-primary);
110+
box-shadow: 0 0 0 .25rem rgba(var(--bs-primary-rgb), .25);
113111
}
114112

115113
/* validation styles for browsers that support :has() */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace common\bootstrap;
6+
7+
use Yii;
8+
use yii\base\Application;
9+
use yii\base\BootstrapInterface;
10+
use yii\mail\MailerInterface;
11+
12+
/**
13+
* Registers the application mailer component as the DI container singleton for {@see MailerInterface}.
14+
*
15+
* Codeception replaces `components.mailer` with its own TestMailer to capture sent emails.
16+
*
17+
* Since the DI container is configured before the mailer mock, this bootstrap ensures the DI singleton resolves to the
18+
* same mailer instance used by the application.
19+
*/
20+
final class MailerBootstrap implements BootstrapInterface
21+
{
22+
/**
23+
* @param Application $app Application instance.
24+
*/
25+
public function bootstrap($app): void
26+
{
27+
Yii::$container->setSingleton(MailerInterface::class, $app->mailer);
28+
}
29+
}

common/config/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
Yii::setAlias('@common', dirname(__DIR__));
46
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
57
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');

common/config/main.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
6+
'bootstrap' => [
7+
\common\bootstrap\MailerBootstrap::class,
8+
],
49
'aliases' => [
510
'@bower' => '@vendor/bower-asset',
611
'@npm' => '@vendor/npm-asset',

common/config/params.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
46
'adminEmail' => 'admin@example.com',
57
'supportEmail' => 'support@example.com',

common/config/test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
46
'id' => 'app-common-tests',
57
'basePath' => dirname(__DIR__),
68
'components' => [
79
'user' => [
810
'class' => \yii\web\User::class,
9-
'identityClass' => 'common\models\User',
11+
'identityClass' => \common\models\User::class,
1012
],
1113
],
1214
];

0 commit comments

Comments
 (0)