Skip to content

Commit

Permalink
Pass model to controller and call handleRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
HansSchouten committed Dec 16, 2019
1 parent 6eeab1d commit 40af56c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
26 changes: 25 additions & 1 deletion src/Modules/GrapesJS/Block/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@

class BaseController
{
/**
* @var BaseModel $model
*/
protected $model;

public function index()
/**
* @var bool $forPageBuilder
*/
protected $forPageBuilder;

/**
* Pass essential data to this Block model instance.
*
* @param BaseModel $model
* @param bool $forPageBuilder
*/
public function init(BaseModel $model, $forPageBuilder = false)
{
$this->model = $model;
$this->forPageBuilder = $forPageBuilder;
}

/**
* Handle the current request.
*/
public function handleRequest()
{
}

Expand Down
10 changes: 5 additions & 5 deletions src/Modules/GrapesJS/Block/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ class BaseModel
protected $data;

/**
* @var bool $editMode
* @var bool $forPageBuilder
*/
protected $editMode;
protected $forPageBuilder;

/**
* Pass essential data to this Block model instance.
*
* @param ThemeBlock $block
* @param array $data
* @param bool $editMode
* @param bool $forPageBuilder
*/
public function init(ThemeBlock $block, $data = [], $editMode = false)
public function init(ThemeBlock $block, $data = [], $forPageBuilder = false)
{
$this->block = $block;
$this->data = is_array($data) ? $data : [];
$this->editMode = $editMode;
$this->forPageBuilder = $forPageBuilder;
}

/**
Expand Down
24 changes: 13 additions & 11 deletions src/Modules/GrapesJS/Block/BlockRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,27 @@ protected function renderHtmlBlock(ThemeBlock $themeBlock, $blockData)
protected function renderDynamicBlock(ThemeBlock $themeBlock, $blockData)
{
$controller = new BaseController;
$block = new BaseModel;

if ($themeBlock->getControllerClass()) {
require_once $themeBlock->getControllerFile();
$controllerClass = $themeBlock->getControllerClass();
$controller = new $controllerClass;
}
$controller->index();
$model = new BaseModel;
$blockData = $blockData ?? [];

if ($themeBlock->getModelFile()) {
require_once $themeBlock->getModelFile();
$modelClass = $themeBlock->getModelClass();
$block = new $modelClass;
$model = new $modelClass;
}
$blockData = $blockData ?? [];
$block->init($themeBlock, $blockData, $this->forPageBuilder);
$model->init($themeBlock, $blockData, $this->forPageBuilder);

if ($themeBlock->getControllerFile()) {
require_once $themeBlock->getControllerFile();
$controllerClass = $themeBlock->getControllerClass();
$controller = new $controllerClass;
}
$controller->init($model, $this->forPageBuilder);
$controller->handleRequest();

// init additional variables that should be accessible in the view
$page = $this->page;
$block = $model;

ob_start();
require $themeBlock->getViewFile();
Expand Down

0 comments on commit 40af56c

Please sign in to comment.