Skip to content

Commit

Permalink
Block Generator also generates the view file of the PHPBlock in the
Browse files Browse the repository at this point in the history
depending view folder. closes #1059
  • Loading branch information
nadar committed Nov 1, 2016
1 parent 9958889 commit 214d202
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The changelog contains informations about bug fixes, new features or bc breaking
- [#1057](https://github.com/luyadev/luya/issues/1057) PHP Block CMS View class provides more and better helper methods in order to retrieve config contents.
- [#1060](https://github.com/luyadev/luya/issues/1060) Filemanager Drag&Drop and Copy&Paste of files enabled (Chrome, Firefox and HTML5 Browsers only).
- [#1063](https://github.com/luyadev/luya/issues/1063) Cleanup the block interface in order to make concret block implementations.
- [#1059](https://github.com/luyadev/luya/issues/1059) Block Generator also generates the view file of the PHPBlock in the depending view folder.
- [#999](https://github.com/luyadev/luya/issues/999) Rewritten the CRUD generator and added ability to disable i18n fiel generation.

### Fixed
Expand Down
26 changes: 25 additions & 1 deletion core/console/commands/BlockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function getBlockName()
*/
public $phpdoc = [];

public $viewFileDoc = [];

/**
* Get an array with all modules where you can generate blocks for.
*
Expand Down Expand Up @@ -229,6 +231,7 @@ private function varCreator($prefix, $typeCast)

if ($extra) {
$this->phpdoc[] = '{{extras.'.$v['var'].'}}';
$this->viewFileDoc[] = '$this->extraValue(\''.$v['var'].'\');';
$this->extras[] = $extra;
}

Expand All @@ -255,6 +258,15 @@ protected function getFileBasePath()
return Yii::$app->getModule($this->moduleName)->getBasePath();
}

public function generateViewFile($blockClassName)
{
sort($this->viewFileDoc);
return $this->view->render('@luya/console/commands/views/block/create_block_view.php', [
'blockClassName' => $blockClassName,
'phpdoc' => $this->viewFileDoc,
]);
}

/**
* Wizzard to create a new CMS block.
*
Expand Down Expand Up @@ -303,6 +315,7 @@ public function actionCreate()
while ($doVars) {
$item = $this->varCreator('Variabel (vars) #'.$i, 'var');
$this->phpdoc[] = '{{vars.'.$item['var'].'}}';
$this->viewFileDoc[] = '$this->varValue(\''.$item['var'].'\');';
$this->config['vars'][] = $item;
$doVars = $this->confirm('Add one more?', false);
++$i;
Expand All @@ -312,6 +325,7 @@ public function actionCreate()
while ($doCfgs) {
$item = $this->varCreator('Configration (cfgs) #'.$i, 'cfg');
$this->phpdoc[] = '{{cfgs.'.$item['var'].'}}';
$this->viewFileDoc[] = '$this->cfgValue(\''.$item['var'].'\');';
$this->config['cfgs'][] = $item;
$doCfgs = $this->confirm('Add one more?', false);
++$i;
Expand All @@ -321,6 +335,7 @@ public function actionCreate()
while ($doPlaceholders) {
$item = $this->placeholderCreator('Placeholder (placeholders) #'.$i);
$this->phpdoc[] = '{{placeholders.'.$item['var'].'}}';
$this->viewFileDoc[] = '$this->placeholderValue(\''.$item['var'].'\');';
$this->config['placeholders'][] = $item;
$doPlaceholders = $this->confirm('Add one more?', false);
++$i;
Expand Down Expand Up @@ -351,7 +366,16 @@ public function actionCreate()
}

if (FileHelper::createDirectory($folder) && FileHelper::writeFile($filePath, $content)) {
return $this->outputSuccess("Block '$filePath' has been created.");

// generate view file based on block object view context
$object = Yii::createObject(['class' => $this->getFileNamespace() . '\\' . $this->blockName]);
$viewsFolder = Yii::getAlias($object->getViewPath());
$viewFilePath = $viewsFolder . DIRECTORY_SEPARATOR . $object->getViewFileName('php');
if (FileHelper::createDirectory($viewsFolder) && FileHelper::writeFile($viewFilePath, $this->generateViewFile($this->blockName))) {
$this->outputInfo('View file for the block has been created: ' . $viewFilePath);
}

return $this->outputSuccess("Block {$this->blockName} has been created: " . $filePath);
}

return $this->outputError("Error while creating block '$filePath'");
Expand Down
17 changes: 17 additions & 0 deletions core/console/commands/views/block/create_block_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
echo "<?php\n";
?>

use Yii;

/**
* View file for block: <?= $blockClassName; ?>
*
<?php foreach ($phpdoc as $doc): ?>
* @param <?= $doc; ?>

<?php endforeach; ?>
*
* @var $this \luya\cms\base\PhpBlockView
*/
?>
27 changes: 27 additions & 0 deletions tests/core/console/controllers/BlockControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,31 @@ public function admin()
$ctrl->dryRun = true;
$this->assertEquals($this->getHtml($tpl), $this->getHtml($ctrl->actionCreate()));
}

public function testBlockViewFileContent()
{
$ctrl = new BlockController('id', Yii::$app);
$ctrl->viewFileDoc = [
'$this->varValue(\'foo\');', '$this->varValue(\'foo\');', '$this->extraValue(\'foo\');', '$this->cfgValue(\'foo\');',
];

$view = <<<'EOT'
<?php
use Yii;
/**
* View file for block: MySuperBlock
*
* @param $this->cfgValue('foo');
* @param $this->extraValue('foo');
* @param $this->varValue('foo');
* @param $this->varValue('foo');
*
* @var $this \luya\cms\base\PhpBlockView
*/
?>
EOT;
$this->assertSame($view, $ctrl->generateViewFile('MySuperBlock'));
}
}

0 comments on commit 214d202

Please sign in to comment.