Skip to content

Commit 2c9387f

Browse files
committed
#2 added Block Generator
1 parent fd9eac3 commit 2c9387f

File tree

7 files changed

+230
-4
lines changed

7 files changed

+230
-4
lines changed

src/Bootstrap.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ public function bootstrap($app)
4040
Event::on(Gii::className(), Gii::EVENT_BEFORE_ACTION, function (Event $event) {
4141
/** @var Gii $gii */
4242
$gii = $event->sender;
43-
$gii->generators['relation-migration'] = [
43+
$gii->generators['block-migration-generator'] = [
4444
'class' => 'nullref\cms\generators\migration\Generator',
4545
];
46+
$gii->generators['block-generator'] = [
47+
'class' => 'nullref\cms\generators\block\Generator',
48+
];
4649
});
4750
}
4851
}

src/components/BlockManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ class BlockManager extends Component
1313
const CLASS_WIDGET = '\Widget';
1414
const CLASS_BLOCK = '\Block';
1515

16-
protected $blocks = [];
16+
public $blocks = [];
17+
18+
protected $_blocks = [];
1719

1820
public function register($id, $namespace)
1921
{
2022
if (class_exists($namespace . self::CLASS_BLOCK) && class_exists($namespace . self::CLASS_WIDGET)) {
21-
$this->blocks[$id] = $namespace;
23+
$this->_blocks[$id] = $namespace;
2224
} else {
2325
throw new InvalidConfigException("Classes Widget and Block must be present in namespace '$namespace'");
2426
}
2527
}
2628

2729
public function getList()
2830
{
29-
return array_merge($this->blocks,[
31+
return array_merge($this->blocks, $this->_blocks, [
3032
'text' => 'nullref\cms\blocks\text',
3133
'html' => 'nullref\cms\blocks\html',
3234
'image' => 'nullref\cms\blocks\image',

src/generators/block/Generator.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace nullref\cms\generators\block;
4+
5+
use Yii;
6+
use yii\gii\CodeFile;
7+
use yii\gii\Generator as BaseGenerator;
8+
9+
10+
class Generator extends BaseGenerator
11+
{
12+
const FORM_NAME = '_form';
13+
const BLOCK_NAME = 'Block';
14+
const WIDGET_NAME = 'Widget';
15+
16+
public $blockName = 'NewBlock';
17+
public $blockModel = 'nullref\cms\components\Block';
18+
public $widgetModel = 'nullref\cms\components\Widget';
19+
public $destinationPath = 'app\components\blocks\newBlock';
20+
21+
22+
public function generate()
23+
{
24+
//Init _form.php
25+
$files[] = new CodeFile(
26+
Yii::getAlias('@' . str_replace('\\', '/', $this->destinationPath)) . '/' . $this::FORM_NAME . '.php',
27+
$this->render('_form.php', [])
28+
);
29+
//Init block.php
30+
$files[] = new CodeFile(
31+
Yii::getAlias('@' . str_replace('\\', '/', $this->destinationPath)) . '/' . $this::BLOCK_NAME . '.php',
32+
$this->render('block.php', [
33+
'blockName' => $this->blockName,
34+
'destination' => $this->destinationPath,
35+
'blockModel' => $this->blockModel,
36+
])
37+
);
38+
//Init widget.php
39+
$files[] = new CodeFile(
40+
Yii::getAlias('@' . str_replace('\\', '/', $this->destinationPath)) . '/' . $this::WIDGET_NAME . '.php',
41+
$this->render('widget.php', [
42+
'blockName' => $this->blockName,
43+
'destination' => $this->destinationPath,
44+
'widgetModel' => $this->widgetModel,
45+
])
46+
);
47+
48+
return $files;
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
public function successMessage()
55+
{
56+
$output = <<<EOD
57+
<p>The block has been generated successfully.</p>
58+
<p>To use the block, you need to add this to your application configuration in modules.php:</p>
59+
EOD;
60+
$code = <<<EOD
61+
......
62+
'cms' => [
63+
'class' => 'nullref\cms\Module',
64+
'blockManagerClass' =>[
65+
'class'=> 'nullref\cms\components\BlockManager',
66+
'blocks'=>[
67+
'$this->blockName'=> '$this->destinationPath',
68+
]
69+
],
70+
],
71+
......
72+
EOD;
73+
return $output . '<pre>' . highlight_string($code, true) . '</pre>';
74+
}
75+
76+
77+
/**
78+
* @return array
79+
*/
80+
public function rules()
81+
{
82+
return [
83+
[['blockName', 'blockModel', 'widgetModel'], 'required'],
84+
//['blockName', 'unique', 'message' => Yii::t('cms','Block with this name already exist')],
85+
['destinationPath', 'safe'],
86+
['destinationPath', 'default', 'value' => '@nullref/cms/blocks/newBlock'],
87+
];
88+
}
89+
90+
/**
91+
* @inheritdoc
92+
*/
93+
public function attributeLabels()
94+
{
95+
return array_merge(parent::attributeLabels(), [
96+
'blockName' => 'Block Name',
97+
'blockModel' => 'Block Model Class',
98+
'widgetModel' => 'Widget Model Class',
99+
'destinationPath' => 'Destination Path',
100+
]);
101+
}
102+
103+
/**
104+
* @inheritdoc
105+
*/
106+
public function hints()
107+
{
108+
return array_merge(parent::hints(), [
109+
'blockName' => 'You should set name for your block',
110+
'blockModel' => 'You should provide a fully qualified class name, e.g., <code>nullref\cms\components\Block</code>.',
111+
'widgetModel' => 'You should provide a fully qualified class name, e.g., <code>nullref\cms\components\Widget</code>.',
112+
'destinationPath' => 'Specify the directory for storing the files for your new block. You may use path alias here, e.g.,
113+
<code>app\components\blocks\newBlock</code>'
114+
]);
115+
}
116+
117+
118+
/**
119+
* @inheritdoc
120+
*/
121+
public function getName()
122+
{
123+
return 'Block Generator';
124+
}
125+
126+
/**
127+
* @inheritdoc
128+
*/
129+
public function getDescription()
130+
{
131+
return 'This generator generates a block for cms';
132+
}
133+
134+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/**
4+
* @var $form \yii\widgets\ActiveForm
5+
* @var $block \nullref\cms\blocks\text\Block
6+
*/
7+
8+
echo "<?php\n";
9+
?>
10+
11+
/* Add nessesary fields like content below */
12+
echo $form->field($block, 'content')->textarea();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* @var $blockName string
5+
* @var $destination string
6+
* @var $blockModel string
7+
*/
8+
9+
echo "<?php\n";
10+
?>
11+
12+
namespace <?= $destination ?>;
13+
14+
use <?= $blockModel ?> as BaseBlock;
15+
/**
16+
* Class Block
17+
*/
18+
class Block extends BaseBlock
19+
{
20+
public $content;
21+
22+
public function getName()
23+
{
24+
return '<?= $blockName ?> Block';
25+
}
26+
27+
public function rules()
28+
{
29+
return [
30+
[['content'],'required'],
31+
];
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* @var $blockName string
5+
* @var $destination string
6+
* @var $widgetModel string
7+
*/
8+
9+
echo "<?php\n";
10+
?>
11+
12+
namespace <?= $destination ?>;
13+
14+
use <?= $widgetModel ?> as BaseWidget;
15+
use yii\helpers\Html;
16+
17+
18+
class Widget extends BaseWidget
19+
{
20+
public $content;
21+
22+
public function run()
23+
{
24+
return $this->content;
25+
}
26+
}

src/generators/block/form.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use nullref\cms\generators\block\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, 'blockName')->textInput();
12+
echo $form->field($generator, 'blockModel')->textInput();
13+
echo $form->field($generator, 'widgetModel')->textInput();
14+
echo $form->field($generator, 'destinationPath')->textInput();
15+
?>
16+
</div>

0 commit comments

Comments
 (0)