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+ }
0 commit comments