Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit a482d2f

Browse files
authored
Merge pull request #4 from justcoded/develop
Settings component IDE support, updated configs, replaced params usage to settings
2 parents 447e3c1 + b63b26f commit a482d2f

File tree

16 files changed

+211
-134
lines changed

16 files changed

+211
-134
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ CHANGELOG for Yii2 STARTER PROJECT TEMPLATE
33

44
*should be replaced with real project changelog later*
55

6+
v0.6
7+
---------------------
8+
* Added app components settings class to support IDE for modelsMap
9+
* Overwrite all params usage to settings usage
10+
* Updated AppSettingsForm sender* properties to system* properties.
11+
612
v0.5
713
---------------------
814
* Added settings extension with settings component

README.md

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -238,80 +238,3 @@ vendor/bin/codecept run functional,unit -- --coverage-html --coverage-xml
238238
```
239239
240240
You can see code coverage output under the `tests/_output` directory.
241-
242-
### Settings extension
243-
244-
For now this extension allows to store settings only in database. To run migration for creating
245-
settings table you need to configure the migrationPath in app-console.php
246-
247-
---------
248-
```php
249-
'migrate' => [
250-
'migrationPath' => [
251-
'@app/extensions/settings/migrations'
252-
],
253-
],
254-
```
255-
and run the migrate command or just run the next command
256-
```
257-
yii migrate --migrationPath=@app/extensions/settings/migrations
258-
```
259-
260-
**Component Setup**
261-
262-
To use the Setting Component, you need to configure the components array in your application configuration:
263-
264-
```php
265-
'components' => [
266-
'settings' => [
267-
'class' => 'justcoded\yii2\settings\components\DbSettings',
268-
],
269-
],
270-
```
271-
272-
and add component name to bootstrap array
273-
274-
```php
275-
'bootstrap' => ['settings'],
276-
```
277-
278-
**Usage**
279-
280-
```php
281-
// To set value
282-
Yii::$app->settings->set('section_name', 'key', 'value');
283-
284-
// To get value
285-
$value = Yii::$app->settings->get('section_name', 'key');
286-
```
287-
288-
There is a possibility to use models as some setting group object. To do this you have to
289-
add modelsMap param to component config:
290-
291-
```php
292-
'settings' => [
293-
'class' => 'justcoded\yii2\settings\components\DbSettings',
294-
'modelsMap' => [
295-
'custom_model_name' => 'path\to\CustomModel',
296-
],
297-
],
298-
```
299-
300-
Add action to controller to get settings form with keys according to the model's properties
301-
302-
```php
303-
public function actions()
304-
{
305-
return [
306-
'action_name' => [
307-
'class' => 'justcoded\yii2\settings\actions\SettingsAction',
308-
'modelClass' => 'path\to\CustomModel',
309-
],
310-
];
311-
}
312-
```
313-
and create view with form displaying. Now you can get settings this way:
314-
315-
```php
316-
$value = Yii::$app->settings->custom_model_name->property_name;
317-
```

app/components/Settings.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace app\components;
4+
5+
use justcoded\yii2\settings\components\DbSettings;
6+
use justcoded\yii2\settings\forms\AppSettingsForm;
7+
8+
/**
9+
* Class Settings
10+
*
11+
* @property AppSettingsForm $app
12+
*
13+
* @package app\components
14+
*/
15+
class Settings extends DbSettings
16+
{
17+
18+
}

app/console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Custom App class to allow custom components IDE
88
*
99
* @property \app\i18n\Formatter $formatter The main formatter for app
10-
* @property \justcoded\yii2\settings\components\Settings $settings Configuration params
10+
* @property \app\components\Settings $settings Configuration params
1111
*/
1212
class Application extends \yii\console\Application
1313
{

app/extensions/README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<p align="center">
2+
<a href="https://github.com/yiisoft" target="_blank">
3+
<img src="https://avatars0.githubusercontent.com/u/993323" height="100px">
4+
</a>
5+
<h1 align="center">Yii2 Settings Extension</h1>
6+
<br>
7+
</p>
8+
9+
10+
Replacement for Yii app params. Easy to use component to store application settings.
11+
Supports only DB storage for now.
12+
Have ready to use base Settings form model and controller Action.
13+
14+
### Installation
15+
16+
The preferred way to install this extension is through composer.
17+
18+
Either run
19+
20+
```bash
21+
php composer.phar require --prefer-dist yii2mod/yii2-settings "*"
22+
```
23+
24+
or add
25+
26+
```
27+
"yii2mod/yii2-settings": "*"
28+
```
29+
30+
to the require section of your composer.json.
31+
32+
### Configuration
33+
34+
#### Database migration
35+
36+
Before usage this extension, we'll also need to prepare the database.
37+
38+
You can add migrations path to your console config and then run `migrate` command:
39+
40+
```php
41+
'migrate' => [
42+
'migrationPath' => [
43+
'@vendor/justcoded/yii2-settings/migrations'
44+
],
45+
],
46+
```
47+
48+
or you can run the command below:
49+
50+
```
51+
php yii migrate --migrationPath=@vendor/justcoded/yii2-settings/migrations
52+
```
53+
54+
### Component Setup
55+
56+
To use the Setting Component, you need to configure the components array in your application configuration:
57+
58+
```php
59+
'components' => [
60+
'settings' => [
61+
'class' => 'justcoded\yii2\settings\components\DbSettings',
62+
],
63+
],
64+
```
65+
66+
and add component name to bootstrap array
67+
68+
```php
69+
'bootstrap' => ['log', 'settings'],
70+
```
71+
72+
### Usage
73+
74+
```php
75+
// set value
76+
Yii::$app->settings->set('section_name', 'key', 'value');
77+
78+
// get value
79+
$value = Yii::$app->settings->get('section_name', 'key');
80+
```
81+
82+
There is a possibility to use models as some setting group object. To do this you have to
83+
add modelsMap array to component's configuration:
84+
85+
```php
86+
'settings' => [
87+
'class' => 'justcoded\yii2\settings\components\DbSettings',
88+
'modelsMap' => [
89+
'section1' => 'app\models\MySettingsForm1',
90+
'section2' => 'app\models\MySettingsForm2',
91+
],
92+
],
93+
```
94+
95+
Add action to controller to get settings form with keys according to the model's properties
96+
97+
```php
98+
public function actions()
99+
{
100+
return [
101+
'actionName' => [
102+
'class' => 'justcoded\yii2\settings\actions\SettingsAction',
103+
'modelClass' => 'app\models\MySettingsForm1',
104+
],
105+
];
106+
}
107+
```
108+
and create view with some active form. (You can copy a template from extension "views" folder)
109+
110+
Now you can get settings in better way:
111+
112+
```php
113+
$value = Yii::$app->settings->section1->myPropertyName;
114+
```
115+
116+
This is very useful, if you overwrite Yii/Application classes and specify correct PHPDoc comments.
117+
In this way IDE will highlight all sections and properties.
118+
119+
### Example
120+
121+
You can check the example on our [Yii2 starter kit](https://github.com/justcoded/yii2-starter).

app/extensions/rbac/commands/RbacController.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,28 @@ protected function scanControllerRoutes(array $controllers)
229229
}
230230

231231
foreach ($methods as $method) {
232-
if (! preg_match('/^action([A-Z]([a-zA-Z0-9]+))$/', $method->getName(), $actionMatch)) {
232+
if (! preg_match('/^action([A-Z]([a-zA-Z0-9]+))$/', $method->getName(), $actionMatch)
233+
&& !('actions' === $method->getName() && $reflection->getName() === $method->class)
234+
) {
233235
continue;
234236
}
235237
$controllerId = Inflector::slug(Inflector::camel2words($classMatch[2]));
236-
$actionId = Inflector::slug(Inflector::camel2words($actionMatch[1]));
237238

238-
$actions[] = $moduleId . $controllerId . '/' . $actionId;
239+
if ('actions' === $method->getName()) {
240+
try {
241+
$controllerObj = Yii::createObject($method->class, [$controllerId, Yii::$app]);
242+
$customActions = $controllerObj->actions();
243+
foreach ($customActions as $actionId => $params) {
244+
$actions[] = $moduleId . $controllerId . '/' . $actionId;
245+
}
246+
} catch (\Exception $e) {
247+
$this->error("\t\tcan't scan custom actions from {$method->class}::actions(). You will need to add them manually.");
248+
}
249+
250+
} else {
251+
$actionId = Inflector::slug(Inflector::camel2words($actionMatch[1]));
252+
$actions[] = $moduleId . $controllerId . '/' . $actionId;
253+
}
239254
}
240255
}
241256

app/extensions/settings/forms/AppSettingsForm.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
/**
66
* Class AppSettingsForm
7+
*
8+
* @property array adminFriendlyEmail
9+
* @property array systemFriendlyEmail
10+
*
711
* @package justcoded\yii2\settings\forms
812
*/
913
class AppSettingsForm extends SettingsForm
@@ -27,14 +31,14 @@ class AppSettingsForm extends SettingsForm
2731
*
2832
* @var string
2933
*/
30-
public $senderEmail;
34+
public $systemEmail;
3135

3236
/**
3337
* Name, which will be set in 'from' mail column
3438
*
3539
* @var string
3640
*/
37-
public $senderName;
41+
public $systemName;
3842

3943
/**
4044
* In minuts
@@ -56,9 +60,9 @@ class AppSettingsForm extends SettingsForm
5660
public function rules()
5761
{
5862
return [
59-
[['adminEmail', 'adminName', 'senderName', 'senderEmail'], 'string'],
60-
[['adminEmail', 'senderEmail'], 'email'],
61-
[['passwordResetToken', 'rememberMeExpiration', 'senderEmail', 'adminEmail'], 'required'],
63+
[['adminEmail', 'adminName', 'systemEmail', 'systemName'], 'string'],
64+
[['adminEmail', 'systemEmail'], 'email'],
65+
[['passwordResetToken', 'rememberMeExpiration', 'systemEmail', 'adminEmail'], 'required'],
6266
[['passwordResetToken', 'rememberMeExpiration'], 'integer'],
6367
];
6468
}
@@ -71,8 +75,8 @@ public function attributeLabels()
7175
return [
7276
'adminEmail' => 'Admin email',
7377
'adminName' => 'Admin name',
74-
'senderEmail' => 'Sender email',
75-
'senderName' => 'Sender name',
78+
'systemEmail' => 'System email',
79+
'systemName' => 'System email name',
7680
'passwordResetToken' => 'Password reset token',
7781
'rememberMeExpiration' => 'Remember me expiration',
7882
];
@@ -99,8 +103,8 @@ public function getAdminFriendlyEmail()
99103
/**
100104
* @return array
101105
*/
102-
public function getSenderFriendlyEmail()
106+
public function getSystemFriendlyEmail()
103107
{
104-
return [$this->senderEmail => $this->senderName];
108+
return [$this->systemEmail => $this->systemName];
105109
}
106110
}

app/extensions/settings/views/app.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
<?= $form->field($model, 'adminEmail')->textInput() ?>
2525

26-
<?= $form->field($model, 'senderName')->textInput() ?>
26+
<?= $form->field($model, 'systemName')->textInput() ?>
2727

28-
<?= $form->field($model, 'senderEmail')->textInput() ?>
28+
<?= $form->field($model, 'systemEmail')->textInput() ?>
2929

3030
<?= $form->field($model, 'passwordResetToken')->input('number') ?>
3131

app/forms/ContactForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public function contact()
5151
{
5252
if ($this->validate()) {
5353
Yii::$app->mailer->compose()
54-
->setTo(Yii::$app->params['adminEmail'])
55-
->setFrom([$this->email => $this->name])
54+
->setTo(settings()->app->adminFriendlyEmail)
55+
->setFrom([settings()->app->systemEmail => $this->name])
5656
->setSubject($this->subject)
5757
->setTextBody($this->body)
5858
->send();

app/forms/LoginForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function validatePassword($attribute, $params)
6363
public function login()
6464
{
6565
if ($this->validate()) {
66-
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
66+
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? settings()->app->rememberMeExpiration : 0);
6767
}
6868

6969
return false;

0 commit comments

Comments
 (0)