Skip to content

Commit fd9eac3

Browse files
committed
added Block Migration Generator
1 parent 7b1756c commit fd9eac3

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed

src/Bootstrap.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace nullref\cms;
44

5-
65
use yii\base\BootstrapInterface;
6+
use yii\base\Event;
77
use yii\i18n\PhpMessageSource;
8+
use yii\gii\Module as Gii;
89
use yii\web\Application as WebApplication;
910

1011
class Bootstrap implements BootstrapInterface
@@ -34,6 +35,16 @@ public function bootstrap($app)
3435
'basePath' => '@nullref/cms/messages',
3536
];
3637
}
38+
39+
if (YII_ENV_DEV) {
40+
Event::on(Gii::className(), Gii::EVENT_BEFORE_ACTION, function (Event $event) {
41+
/** @var Gii $gii */
42+
$gii = $event->sender;
43+
$gii->generators['relation-migration'] = [
44+
'class' => 'nullref\cms\generators\migration\Generator',
45+
];
46+
});
47+
}
3748
}
3849

3950
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace nullref\cms\generators\migration;
4+
5+
use Yii;
6+
use nullref\cms\models\Block;
7+
use yii\gii\CodeFile;
8+
use yii\gii\Generator as BaseGenerator;
9+
10+
11+
class Generator extends BaseGenerator
12+
{
13+
14+
public $block;
15+
16+
public static function getBlocks()
17+
{
18+
return Block::find()->all();
19+
}
20+
21+
22+
public function generate()
23+
{
24+
/** @var Block $selectedBlock */
25+
$selectedBlock = Block::find()->where(['id' => $this->block])->one();
26+
$time = new \DateTime();
27+
$time = $time->getTimestamp();
28+
$commands = "['id' => '$selectedBlock->id'," .
29+
"'class_name' => '$selectedBlock->class_name'," .
30+
"'config' => '{$selectedBlock->config}'," .
31+
"'createdAt' => $time," .
32+
"'updatedAt' => $time,]";
33+
34+
$files = [];
35+
$name = 'm' . gmdate('ymd_Hi') . '00_block_' . $selectedBlock->id;
36+
$code = $this->render('migration.php', [
37+
'name' => $name,
38+
'blockId' => $selectedBlock->id,
39+
'commands' => $commands,
40+
]);
41+
$files[] = new CodeFile(
42+
Yii::getAlias('@app/migrations') . '/' . $name . '.php',
43+
$code
44+
);
45+
return $files;
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function rules()
52+
{
53+
return [
54+
[['block'], 'required'],
55+
];
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function getName()
62+
{
63+
return 'Block Migration Generator';
64+
}
65+
66+
/**
67+
* @inheritdoc
68+
*/
69+
public function getDescription()
70+
{
71+
return 'This generator generates a migration for block';
72+
}
73+
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* @var $blockId string
5+
* @var $commands string
6+
*/
7+
8+
echo "<?php\n";
9+
?>
10+
11+
use yii\db\Migration;
12+
use yii\db\Schema;
13+
use nullref\cms\models\Block;
14+
15+
class <?= $name ?> extends Migration
16+
{
17+
18+
public function up()
19+
{
20+
/** @var Block $existBlock */
21+
$existBlock = Block::findOne(['id' => '<?= $blockId ?>']);
22+
if( $existBlock ) {
23+
$oldId = 'old_' . $existBlock->id;
24+
$existBlock->id = $oldId;
25+
$existBlock->save();
26+
}
27+
$this->insert(Block::tableName(), <?= $commands ?>);
28+
}
29+
30+
public function down()
31+
{
32+
$this->delete(Block::tableName(), ['id' => '<?= $blockId ?>']);
33+
/** @var Block $oldBlock */
34+
$oldBlock = Block::findOne(['id' => 'old_<?= $blockId ?>']);
35+
if( $oldBlock ) {
36+
$oldBlock->id = '<?= $blockId ?>';
37+
$oldBlock->save();
38+
}
39+
return true;
40+
}
41+
42+
}

src/generators/migration/form.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use nullref\cms\generators\migration\Generator;
4+
5+
/* @var $this yii\web\View */
6+
/* @var $form yii\widgets\ActiveForm */
7+
/* @var $generator yii\gii\generators\module\Generator */
8+
?>
9+
<div class="migration-form">
10+
<?php
11+
echo $form->field($generator, 'block')->dropDownList(\yii\helpers\ArrayHelper::map(Generator::getBlocks(), 'id', 'id'));
12+
?>
13+
</div>

0 commit comments

Comments
 (0)