Skip to content

Commit 451cc0f

Browse files
committed
2 parents bc609e4 + f32612d commit 451cc0f

File tree

10 files changed

+91
-9
lines changed

10 files changed

+91
-9
lines changed

src/commands/PrototypeController.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class PrototypeController extends Controller
3434
{
3535

36-
public $escapeFileNames = false;
36+
public $escapeFileNames = true;
3737
public $exportPath = '@runtime/export';
3838

3939
/**
@@ -57,17 +57,17 @@ public function actions()
5757
$actions['export-html'] = [
5858
'class' => ExportAction::class,
5959
'modelClass' => Html::class,
60-
'extention' => 'html',
60+
'extension' => 'html',
6161
];
6262
$actions['export-less'] = [
6363
'class' => ExportAction::class,
6464
'modelClass' => Less::class,
65-
'extention' => 'less',
65+
'extension' => 'less',
6666
];
6767
$actions['export-twig'] = [
6868
'class' => ExportAction::class,
6969
'modelClass' => Twig::class,
70-
'extention' => 'twig',
70+
'extension' => 'twig',
7171
];
7272
return $actions;
7373
}
@@ -77,7 +77,6 @@ public function actions()
7777
*
7878
* Returns path alias if alias defined otherwise return plain path
7979
* Removes slash from end
80-
* Add sub dir with name of extention
8180
*/
8281
public function getExportPath()
8382
{

src/commands/actions/ExportAction.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
* @author Elias Luhr <[email protected]>
3232
*
3333
* @property ActiveRecord modelClass
34-
* @property string extention
34+
* @property string extension
3535
* @property PrototypeController controller
3636
*/
3737
class ExportAction extends Action
3838
{
3939
public $modelClass;
40-
public $extention;
40+
public $extension;
4141

4242
private static $availableModelTypes = [
4343
Html::class,
@@ -53,7 +53,7 @@ class ExportAction extends Action
5353
protected function run()
5454
{
5555

56-
$this->controller->stdout("Exporting {$this->extention} files" . PHP_EOL, Console::FG_BLUE);
56+
$this->controller->stdout("Exporting {$this->extension} files" . PHP_EOL, Console::FG_BLUE);
5757
if (!class_exists($this->modelClass)) {
5858
$this->controller->stderr("Model class '{$this->modelClass}' does not exist", Console::FG_RED);
5959
return ExitCode::IOERR;
@@ -83,7 +83,15 @@ protected function run()
8383
$fileName = Inflector::slug(str_replace('/','-',$entry->key));
8484
}
8585
try {
86-
if (file_put_contents($exportPath . DIRECTORY_SEPARATOR . $fileName . '.' . $this->extention, $entry->value) === false) {
86+
// check if filename "looks" like a path, if yes, we must create subdirs
87+
if ($fileName !== basename($fileName)) {
88+
$subDir = $exportPath . DIRECTORY_SEPARATOR . trim(dirname($fileName), DIRECTORY_SEPARATOR);
89+
if (!FileHelper::createDirectory($subDir)) {
90+
throw new ErrorException("Error while creating sub directory '{$subDir}' for file '{$fileName}'");
91+
}
92+
}
93+
94+
if (file_put_contents($exportPath . DIRECTORY_SEPARATOR . $fileName . '.' . $this->extension, $entry->value) === false) {
8795
throw new ErrorException("Error while writing file for key '{$entry->key}'");
8896
}
8997
$this->controller->stdout('.');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
class m211116_101347_add_timestamp_cols extends Migration
6+
{
7+
public function up()
8+
{
9+
$this->addColumn('{{%twig}}', 'created_at', $this->dateTime()->null());
10+
$this->addColumn('{{%twig}}', 'updated_at', $this->dateTime()->null());
11+
$this->addColumn('{{%less}}', 'created_at', $this->dateTime()->null());
12+
$this->addColumn('{{%less}}', 'updated_at', $this->dateTime()->null());
13+
$this->addColumn('{{%html}}', 'created_at', $this->dateTime()->null());
14+
$this->addColumn('{{%html}}', 'updated_at', $this->dateTime()->null());
15+
}
16+
17+
public function down()
18+
{
19+
echo "m211116_101347_add_timestamp_cols cannot be reverted.\n";
20+
return false;
21+
}
22+
}

src/models/BaseModel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,51 @@
1010
namespace dmstr\modules\prototype\models;
1111

1212

13+
use bedezign\yii2\audit\AuditTrailBehavior;
1314
use Yii;
15+
use yii\behaviors\TimestampBehavior;
1416
use yii\db\ActiveQuery;
1517
use yii\db\ActiveRecord;
18+
use yii\db\Expression;
1619

1720
/**
1821
* @package dmstr\modules\prototype\models
1922
* @author Elias Luhr <[email protected]>
2023
*/
2124
class BaseModel extends ActiveRecord
2225
{
26+
27+
/**
28+
* Column attribute 'created_at'
29+
*/
30+
const ATTR_CREATED_AT = 'created_at';
31+
32+
/**
33+
* Column attribute 'updated_at'
34+
*/
35+
const ATTR_UPDATED_AT = 'updated_at';
36+
37+
/**
38+
* @inheritdoc
39+
*
40+
* Use yii\behaviors\TimestampBehavior for created_at and updated_at attribute
41+
*
42+
* @return array
43+
*/
44+
public function behaviors()
45+
{
46+
47+
$behaviors = parent::behaviors();
48+
49+
$behaviors['timestamp'] = [
50+
'class' => TimestampBehavior::class,
51+
'createdAtAttribute' => static::ATTR_CREATED_AT,
52+
'updatedAtAttribute' => static::ATTR_UPDATED_AT,
53+
'value' => new Expression('NOW()'),
54+
];
55+
return $behaviors;
56+
}
57+
2358
/**
2459
* @inheritdoc
2560
*/

src/views/html/index.php

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

3+
use dmstr\modules\prototype\models\BaseModel;
34
use rmrevin\yii\fontawesome\FA;
45
use yii\bootstrap\ButtonDropdown;
56
use yii\grid\GridView;
@@ -103,6 +104,8 @@
103104
'contentOptions' => ['nowrap' => 'nowrap'],
104105
],
105106
'key',
107+
BaseModel::ATTR_UPDATED_AT,
108+
BaseModel::ATTR_CREATED_AT,
106109
],
107110
]
108111
); ?>

src/views/html/view.php

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

33
use dmstr\bootstrap\Tabs;
4+
use dmstr\modules\prototype\models\BaseModel;
45
use rmrevin\yii\fontawesome\FA;
56
use yii\helpers\Html;
67
use yii\widgets\DetailView;
@@ -79,6 +80,8 @@
7980
'id',
8081
'key',
8182
'value:html',
83+
BaseModel::ATTR_UPDATED_AT,
84+
BaseModel::ATTR_CREATED_AT,
8285
],
8386
]
8487
); ?>

src/views/less/index.php

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

3+
use dmstr\modules\prototype\models\BaseModel;
34
use rmrevin\yii\fontawesome\FA;
45
use yii\bootstrap\ButtonDropdown;
56
use yii\grid\GridView;
@@ -103,6 +104,8 @@
103104
'contentOptions' => ['nowrap' => 'nowrap'],
104105
],
105106
'key',
107+
BaseModel::ATTR_UPDATED_AT,
108+
BaseModel::ATTR_CREATED_AT,
106109
],
107110
]
108111
); ?>

src/views/less/view.php

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

33
use dmstr\bootstrap\Tabs;
4+
use dmstr\modules\prototype\models\BaseModel;
45
use rmrevin\yii\fontawesome\FA;
56
use yii\helpers\Html;
67
use yii\widgets\DetailView;
@@ -83,6 +84,8 @@
8384
'format' => 'html',
8485
'value' => '<pre>' . Html::encode($model->value) . '</pre>'
8586
],
87+
BaseModel::ATTR_UPDATED_AT,
88+
BaseModel::ATTR_CREATED_AT,
8689
],
8790
]
8891
); ?>

src/views/twig/index.php

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

3+
use dmstr\modules\prototype\models\BaseModel;
34
use rmrevin\yii\fontawesome\FA;
45
use yii\grid\GridView;
56
use yii\helpers\Html;
@@ -72,6 +73,8 @@
7273
'contentOptions' => ['nowrap' => 'nowrap'],
7374
],
7475
'key',
76+
BaseModel::ATTR_UPDATED_AT,
77+
BaseModel::ATTR_CREATED_AT,
7578
],
7679
]); ?>
7780
</div>

src/views/twig/view.php

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

33
use dmstr\bootstrap\Tabs;
4+
use dmstr\modules\prototype\models\BaseModel;
45
use rmrevin\yii\fontawesome\FA;
56
use yii\helpers\Html;
67
use yii\widgets\DetailView;
@@ -75,6 +76,8 @@
7576
'format' => 'raw',
7677
'value' => "<pre>" . htmlspecialchars($model->value) . "</pre>",
7778
],
79+
BaseModel::ATTR_UPDATED_AT,
80+
BaseModel::ATTR_CREATED_AT,
7881
],
7982
]); ?>
8083

0 commit comments

Comments
 (0)