luya\console\commands\BlockController::actionCreate PHP Method

actionCreate() public method

Wizzard to create a new CMS block.
public actionCreate ( ) : number
return number
    public function actionCreate()
    {
        if (empty($this->type)) {
            Console::clearScreenBeforeCursor();
            $this->type = $this->select('Do you want to create an app or module Block?', [self::TYPE_APP => 'Creates a project block inside your @app Namespace (casual).', self::TYPE_MODULE => 'Creating a block inside a later specified Module.']);
        }
        if ($this->type == self::TYPE_MODULE && count($this->getModuleProposal()) === 0) {
            return $this->outputError('Your project does not have Project-Modules registered!');
        }
        if (empty($this->moduleName) && $this->type == self::TYPE_MODULE) {
            $this->moduleName = $this->select('Choose a module to create the block inside:', $this->getModuleProposal());
        }
        if (empty($this->blockName)) {
            $this->blockName = $this->prompt('Insert a name for your Block (e.g. HeadTeaser):', ['required' => true]);
        }
        if ($this->isContainer === null) {
            $this->isContainer = $this->confirm("Do you want to add placeholders to your block that serve as a container for nested blocks?", false);
        }
        if ($this->cacheEnabled === null) {
            $this->cacheEnabled = $this->confirm("Do you want to enable the caching for this block or not?", true);
        }
        if ($this->config === null) {
            $this->config = ['vars' => [], 'cfgs' => [], 'placeholders' => []];
            $doConfigure = $this->confirm('Would you like to configure this Block? (vars, cfgs, placeholders)', false);
            if ($doConfigure) {
                $doVars = $this->confirm('Add new Variable (vars)?', false);
                $i = 1;
                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;
                }
                $doCfgs = $this->confirm('Add new Configuration (cgfs)?', false);
                $i = 1;
                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;
                }
                $doPlaceholders = $this->confirm('Add new Placeholder (placeholders)?', false);
                $i = 1;
                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;
                }
            }
        }
        $folder = $this->getFileBasePath() . DIRECTORY_SEPARATOR . 'blocks';
        $filePath = $folder . DIRECTORY_SEPARATOR . $this->blockName . '.php';
        sort($this->phpdoc);
        $content = $this->view->render('@luya/console/commands/views/block/create_block.php', ['namespace' => $this->getFileNamespace(), 'className' => $this->blockName, 'name' => Inflector::camel2words($this->blockName), 'type' => $this->type, 'module' => $this->moduleName, 'isContainer' => $this->isContainer, 'cacheEnabled' => $this->cacheEnabled, 'config' => $this->config, 'phpdoc' => $this->phpdoc, 'extras' => $this->extras, 'luyaText' => $this->getGeneratorText('block/create')]);
        if ($this->dryRun) {
            return $content;
        }
        if (FileHelper::createDirectory($folder) && FileHelper::writeFile($filePath, $content)) {
            // 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}'");
    }

Usage Example

Example #1
0
    public function testModuleBlock()
    {
        $tpl = <<<'EOT'
<?php

namespace luyatests\data\modules\consolemodule\blocks;

use luya\cms\base\PhpBlock;
use luya\cms\frontend\blockgroups\ProjectGroup;
use luya\cms\helpers\BlockHelper;

/**
 * My Test Block.
 *
 * File has been created with `block/create` command on LUYA version 1.0.0-dev. 
 */
class MyTestBlock extends PhpBlock
{
    /**
     * @var string The module where this block belongs to in order to find the view files.
     */
    public $module = 'consolemodule';

    /**
     * @var boolean Choose whether block is a layout/container/segmnet/section block or not, Container elements will be optically displayed
     * in a different way for a better user experience. Container block will not display isDirty colorizing.
     */
    public $isContainer = true;

    /**
     * @var bool Choose whether a block can be cached trough the caching component. Be carefull with caching container blocks.
     */
    public $cacheEnabled = true;
    
    /**
     * @var int The cache lifetime for this block in seconds (3600 = 1 hour), only affects when cacheEnabled is true
     */
    public $cacheExpiration = 3600;

    /**
     * @inheritDoc
     */
    public function blockGroup()
    {
        return ProjectGroup::class;
    }

    /**
     * @inheritDoc
     */
    public function name()
    {
        return 'My Test Block';
    }
    
    /**
     * @inheritDoc
     */
    public function icon()
    {
        return 'extension'; // see the list of icons on: https://design.google.com/icons/
    }
 
    /**
     * @inheritDoc
     */
    public function config()
    {
        return [
            'vars' => [
                 ['var' => 'foo', 'label' => 'Foo', 'type' => 'zaa-text'],
                 ['var' => 'bar', 'label' => 'Bar', 'type' => 'zaa-image', 'options' => OPTIONS!],
            ],
            'cfgs' => [
                 ['var' => 'foo', 'label' => 'Foo', 'type' => 'zaa-text'],
                 ['var' => 'bar', 'label' => 'Bar', 'type' => 'zaa-image', 'options' => OPTIONS!],
            ],
            'placeholders' => [
                 ['var' => 'foo', 'label' => 'Foo'],
                 ['var' => 'bar', 'label' => 'Bar'],
            ],
        ];
    }
    
    /**
     * @inheritDoc
     */
    public function extraVars()
    {
        return [
            'foo'=>'bar'
        ];
    }

    /**
     * {@inheritDoc} 
     *
     * @param {{extras.foobar}}
     * @param {{vars.bar}}
     * @param {{vars.foo}}
    */
    public function admin()
    {
        return '<p>My Test Block Admin View</p>';
    }
}
EOT;
        $ctrl = new BlockController('id', Yii::$app);
        $ctrl->blockName = 'My Test';
        $ctrl->moduleName = 'consolemodule';
        $ctrl->type = BlockController::TYPE_MODULE;
        $ctrl->config = ['vars' => [['var' => 'foo', 'type' => 'zaa-text', 'label' => 'Foo'], ['var' => 'bar', 'type' => 'zaa-image', 'label' => 'Bar', 'options' => 'OPTIONS!']], 'cfgs' => [['var' => 'foo', 'type' => 'zaa-text', 'label' => 'Foo'], ['var' => 'bar', 'type' => 'zaa-image', 'label' => 'Bar', 'options' => 'OPTIONS!']], 'placeholders' => [['var' => 'foo', 'label' => 'Foo'], ['var' => 'bar', 'label' => 'Bar']]];
        $ctrl->isContainer = true;
        // make tests
        $ctrl->cacheEnabled = true;
        // make tests
        $ctrl->extras = ["'foo'=>'bar'"];
        $ctrl->phpdoc = ['{{extras.foobar}}', '{{vars.foo}}', '{{vars.bar}}'];
        $ctrl->dryRun = true;
        $this->assertEquals($this->getHtml($tpl), $this->getHtml($ctrl->actionCreate()));
    }