Skip to content

Commit 40f4ce1

Browse files
committed
a bit of cleanup
1 parent 6088b27 commit 40f4ce1

File tree

11 files changed

+107
-81
lines changed

11 files changed

+107
-81
lines changed

src/Module.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
class Module extends \yii\base\Module
99
{
1010
use AccessBehaviorTrait;
11-
12-
public $controllerNamespace = 'dmstr\modules\prototype\controllers';
13-
11+
1412
private $_view = null;
1513

1614
public function beforeAction($action)
@@ -27,7 +25,7 @@ public function beforeAction($action)
2725
*/
2826
public function getView(){
2927
if ($this->_view === null) {
30-
$this->_view = clone(\Yii::$app->getComponents(false)['view']);
28+
$this->_view = clone \Yii::$app->getComponents(false)['view'];
3129
}
3230
return $this->_view;
3331
}

src/commands/PrototypeController.php

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

1010
namespace dmstr\modules\prototype\commands;
1111

12+
use dmstr\modules\prototype\commands\actions\ExportAction;
13+
use dmstr\modules\prototype\models\Html;
14+
use dmstr\modules\prototype\models\Less;
15+
use dmstr\modules\prototype\models\Twig;
1216
use yii\console\Controller;
1317
use yii\console\ExitCode;
1418
use yii\helpers\Console;
@@ -48,18 +52,18 @@ public function actions()
4852
{
4953
$actions = parent::actions();
5054
$actions['export-html'] = [
51-
'class' => 'dmstr\modules\prototype\commands\actions\ExportAction',
52-
'modelClass' => 'dmstr\modules\prototype\models\Html',
55+
'class' => ExportAction::class,
56+
'modelClass' => Html::class,
5357
'extention' => 'html',
5458
];
5559
$actions['export-less'] = [
56-
'class' => 'dmstr\modules\prototype\commands\actions\ExportAction',
57-
'modelClass' => 'dmstr\modules\prototype\models\Less',
60+
'class' => ExportAction::class,
61+
'modelClass' => Less::class,
5862
'extention' => 'less',
5963
];
6064
$actions['export-twig'] = [
61-
'class' => 'dmstr\modules\prototype\commands\actions\ExportAction',
62-
'modelClass' => 'dmstr\modules\prototype\models\Twig',
65+
'class' => ExportAction::class,
66+
'modelClass' => Twig::class,
6367
'extention' => 'twig',
6468
];
6569
return $actions;

src/commands/actions/ExportAction.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99

1010
namespace dmstr\modules\prototype\commands\actions;
1111

12+
use dmstr\modules\prototype\commands\PrototypeController;
13+
use dmstr\modules\prototype\models\Html;
14+
use dmstr\modules\prototype\models\Less;
15+
use dmstr\modules\prototype\models\Twig;
16+
use const PHP_EOL;
1217
use yii\base\Action;
18+
use yii\base\ErrorException;
1319
use yii\console\ExitCode;
1420
use yii\db\ActiveRecord;
1521
use yii\helpers\Console;
@@ -32,9 +38,9 @@ class ExportAction extends Action
3238
public $extention;
3339

3440
private static $availableModelTypes = [
35-
'dmstr\modules\prototype\models\Html',
36-
'dmstr\modules\prototype\models\Less',
37-
'dmstr\modules\prototype\models\Twig'
41+
Html::class,
42+
Less::class,
43+
Twig::class
3844
];
3945

4046
/**
@@ -45,7 +51,7 @@ class ExportAction extends Action
4551
protected function run()
4652
{
4753

48-
$this->controller->stdout("Exporting {$this->extention} files\n", Console::FG_BLUE);
54+
$this->controller->stdout("Exporting {$this->extention} files" . PHP_EOL, Console::FG_BLUE);
4955
if (!class_exists($this->modelClass)) {
5056
$this->controller->stderr("Model class '{$this->modelClass}' does not exist", Console::FG_RED);
5157
return ExitCode::IOERR;
@@ -60,14 +66,14 @@ protected function run()
6066

6167
try {
6268
if (!FileHelper::createDirectory($exportPath)) {
63-
throw new \Exception("Error while creating directory '{$exportPath}'");
69+
throw new ErrorException("Error while creating directory '{$exportPath}'");
6470
}
6571
} catch (\Exception $exception) {
66-
$this->controller->stderr($exception->getMessage() . "\n", Console::FG_RED);
72+
$this->controller->stderr($exception->getMessage() . PHP_EOL, Console::FG_RED);
6773
return ExitCode::IOERR;
6874
}
6975

70-
$entries = ($this->modelClass)::find()->all();
76+
$entries = $this->modelClass::find()->all();
7177

7278
foreach ($entries as $entry) {
7379
$fileName = $entry->key;
@@ -76,7 +82,7 @@ protected function run()
7682
}
7783
try {
7884
if (file_put_contents($exportPath . DIRECTORY_SEPARATOR . $fileName . '.' . $this->extention, $entry->value) === false) {
79-
throw new \Exception("Error while writing file for key '{$entry->key}'");
85+
throw new ErrorException("Error while writing file for key '{$entry->key}'");
8086
}
8187
$this->controller->stdout('.');
8288
} catch (\Exception $exception) {
@@ -85,8 +91,7 @@ protected function run()
8591
}
8692
}
8793

88-
89-
$this->controller->stdout("\nExported " . count($entries) . ' files to path ' . $exportPath . "\n", Console::FG_GREEN);
94+
$this->controller->stdout(PHP_EOL . 'Exported ' . count($entries) . ' files to path ' . $exportPath . PHP_EOL, Console::FG_GREEN);
9095
return ExitCode::OK;
9196
}
9297
}

src/controllers/DefaultController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace dmstr\modules\prototype\controllers;
44

5-
use dmstr\web\traits\AccessBehaviorTrait;
65
use yii\web\Controller;
76

87
class DefaultController extends Controller

src/models/Html.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace dmstr\modules\prototype\models;
44

5+
use bedezign\yii2\audit\AuditTrailBehavior;
56
use dmstr\modules\prototype\models\base\Html as BaseHtml;
6-
use yii\helpers\ArrayHelper;
77

88
/**
99
* This is the model class for table "app_html".
@@ -12,11 +12,8 @@ class Html extends BaseHtml
1212
{
1313
public function behaviors()
1414
{
15-
return ArrayHelper::merge(
16-
parent::behaviors(),
17-
[
18-
'bedezign\yii2\audit\AuditTrailBehavior',
19-
]
20-
);
15+
$behaviors = parent::behaviors();
16+
$behaviors['audit-trail'] = AuditTrailBehavior::class;
17+
return $behaviors;
2118
}
2219
}

src/models/Less.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace dmstr\modules\prototype\models;
44

5+
use bedezign\yii2\audit\AuditTrailBehavior;
56
use dmstr\modules\prototype\models\base\Less as BaseLess;
6-
use yii\helpers\ArrayHelper;
77

88
/**
99
* This is the model class for table "app_less".
@@ -12,12 +12,9 @@ class Less extends BaseLess
1212
{
1313
public function behaviors()
1414
{
15-
return ArrayHelper::merge(
16-
parent::behaviors(),
17-
[
18-
'bedezign\yii2\audit\AuditTrailBehavior',
19-
]
20-
);
15+
$behaviors = parent::behaviors();
16+
$behaviors['audit-trail'] = AuditTrailBehavior::class;
17+
return $behaviors;
2118
}
2219

2320
public function afterSave($insert, $changedAttributes)

src/models/Twig.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace dmstr\modules\prototype\models;
44

5+
use bedezign\yii2\audit\AuditTrailBehavior;
56
use dmstr\modules\prototype\models\base\Twig as BaseTwig;
6-
use yii\helpers\ArrayHelper;
77

88
/**
99
* This is the model class for table "app_twig".
@@ -12,11 +12,8 @@ class Twig extends BaseTwig
1212
{
1313
public function behaviors()
1414
{
15-
return ArrayHelper::merge(
16-
parent::behaviors(),
17-
[
18-
'bedezign\yii2\audit\AuditTrailBehavior',
19-
]
20-
);
15+
$behaviors = parent::behaviors();
16+
$behaviors['audit-trail'] = AuditTrailBehavior::class;
17+
return $behaviors;
2118
}
2219
}

src/views/default/index.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313

1414
<div class="row">
1515
<div class="col-sm-4">
16-
<?= \insolita\wgadminlte\SmallBox::widget(
16+
<?= SmallBox::widget(
1717
[
1818
'head' => 'Less',
1919
'type' => SmallBox::TYPE_LBLUE,
20-
'icon' => 'fa fa-'.FA::_PENCIL_SQUARE,
20+
'icon' => 'fa fa-' . FA::_PENCIL_SQUARE,
2121
'footer' => 'Themes',
2222
'footer_link' => ['/prototype/less'],
2323
]) ?>
2424
</div>
2525
<div class="col-sm-4">
26-
<?= \insolita\wgadminlte\SmallBox::widget(
26+
<?= SmallBox::widget(
2727
[
2828
'head' => 'Twig',
2929
'type' => SmallBox::TYPE_ORANGE,
30-
'icon' => 'fa fa-'.FA::_STICKY_NOTE,
30+
'icon' => 'fa fa-' . FA::_STICKY_NOTE,
3131
'footer' => 'Layouts',
3232
'footer_link' => ['/prototype/twig'],
3333
]) ?>
3434
</div>
3535
<div class="col-sm-4">
36-
<?= \insolita\wgadminlte\SmallBox::widget(
36+
<?= SmallBox::widget(
3737
[
3838
'head' => 'HTML',
3939
'type' => SmallBox::TYPE_MAR,
40-
'icon' => 'fa fa-'.FA::_STICKY_NOTE_O,
40+
'icon' => 'fa fa-' . FA::_STICKY_NOTE_O,
4141
'footer' => 'Snippets',
4242
'footer_link' => ['/prototype/html'],
4343
]) ?>

src/views/default/test.php

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

3+
use dmstr\modules\prototype\assets\DbAsset;
4+
use dmstr\modules\prototype\widgets\TwigWidget;
5+
36
?>
47

58
<h2>Test</h2>
69

7-
<?php \dmstr\modules\prototype\assets\DbAsset::register($this) ?>
10+
<?php DbAsset::register($this) ?>
811

912

1013

11-
<?= \dmstr\modules\prototype\widgets\TwigWidget::widget([
14+
<?= TwigWidget::widget([
1215
'id' => 'test',
1316
'enableFlash' => true,
1417
]) ?>

src/widgets/HtmlWidget.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,26 @@
1313
use yii\base\Event;
1414
use yii\base\Widget;
1515
use yii\helpers\Html;
16+
use dmstr\modules\prototype\models\Html as HtmlModel;
1617

18+
/**
19+
*
20+
* @property array $menuItems
21+
* @property string|null $key
22+
* @property string $moduleId
23+
* @property bool $enableFlash
24+
* @property bool $registerMenuItems
25+
* @property bool $renderEmpty
26+
* @property Html $_model
27+
*/
1728
class HtmlWidget extends Widget
1829
{
1930
const SETTINGS_SECTION = 'app.html';
2031
const ACCESS_ROLE = 'Editor';
2132

2233
public $key = null;
2334
public $enableFlash = false;
35+
public $moduleId = 'prototype';
2436
public $registerMenuItems = true;
2537
public $renderEmpty = true;
2638

@@ -29,22 +41,22 @@ class HtmlWidget extends Widget
2941
public function init()
3042
{
3143
parent::init();
32-
$this->_model = \dmstr\modules\prototype\models\Twig::findOne(['key' => $this->generateKey()]);
44+
$this->_model = HtmlModel::findOne(['key' => $this->generateKey()]);
3345
if ($this->registerMenuItems) {
3446
\Yii::$app->trigger('registerMenuItems', new Event(['sender' => $this]));
3547
}
3648
}
3749

3850
public function run()
3951
{
40-
$this->_model = $model = \dmstr\modules\prototype\models\Html::findOne(['key' => $this->generateKey()]);
52+
$this->_model = $model = HtmlModel::findOne(['key' => $this->generateKey()]);
4153
$html = '';
4254

4355
if (\Yii::$app->user->can(self::ACCESS_ROLE)) {
44-
$link = ($model) ? $this->generateEditLink($model->id) : $this->generateCreateLink();
56+
$link = $model ? $this->generateEditLink($model->id) : $this->generateCreateLink();
4557
if ($this->enableFlash) {
4658
\Yii::$app->session->addFlash(
47-
($model) ? 'success' : 'info',
59+
$model ? 'success' : 'info',
4860
"Edit contents in {$link}, key: <code>{$this->generateKey()}</code>"
4961
);
5062
}
@@ -67,7 +79,7 @@ public function getMenuItems()
6779
[
6880
'label' => ($this->_model ? FA::icon(FA::_EDIT) :
6981
FA::icon(FA::_PLUS_SQUARE)).' <b>'.$this->generateKey().'</b> <span class="label label-danger">HTML</span>',
70-
'url' => ($this->_model) ? $this->generateEditRoute($this->_model->id) : $this->generateCreateRoute(),
82+
'url' => $this->_model ? $this->generateEditRoute($this->_model->id) : $this->generateCreateRoute(),
7183
],
7284
];
7385
}
@@ -76,32 +88,32 @@ private function generateKey()
7688
{
7789
if ($this->key) {
7890
return $this->key;
79-
} else {
80-
$key = \Yii::$app->request->getQueryParam('id');
8191
}
92+
93+
$key = \Yii::$app->request->getQueryParam('id');
8294
return \Yii::$app->language.'/'.\Yii::$app->controller->route.($key ? '/'.$key : '');
8395
}
8496

8597
private function generateCreateLink()
8698
{
8799

88100
return Html::a(FA::icon(FA::_PLUS_SQUARE) . ' HTML',
89-
['/prototype/html/create', 'Html' => ['key' => $this->generateKey()]]);
101+
['/' . $this->moduleId . '/html/create', 'Html' => ['key' => $this->generateKey()]]);
90102
}
91103

92104
private function generateEditLink($id)
93105
{
94-
return Html::a('prototype module', ['/prototype/html/update', 'id' => $id]);
106+
return Html::a($this->moduleId . ' module', ['/' . $this->moduleId . '/html/update', 'id' => $id]);
95107
}
96108

97109
private function generateCreateRoute()
98110
{
99-
return ['/prototype/html/create', 'Html' => ['key' => $this->generateKey()]];
111+
return ['/' . $this->moduleId . '/html/create', 'Html' => ['key' => $this->generateKey()]];
100112
}
101113

102114
private function generateEditRoute($id)
103115
{
104-
return ['/prototype/html/update', 'id' => $id];
116+
return ['/' . $this->moduleId . '/html/update', 'id' => $id];
105117
}
106118

107119
private function renderEmpty()

0 commit comments

Comments
 (0)