Skip to content

Commit

Permalink
BlockRenderer added, include and call model & controller classes
Browse files Browse the repository at this point in the history
  • Loading branch information
HansSchouten committed Dec 16, 2019
1 parent ffcbb51 commit 6eeab1d
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 34 deletions.
102 changes: 102 additions & 0 deletions src/Modules/GrapesJS/Block/BlockRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace PHPageBuilder\Modules\GrapesJS\Block;

use PHPageBuilder\Contracts\PageContract;
use PHPageBuilder\ThemeBlock;

class BlockRenderer
{
/**
* @var PageContract $page
*/
protected $page;

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

/**
* BlockAdapter constructor.
*
* @param PageContract $page
* @param bool $forPageBuilder
*/
public function __construct(PageContract $page, $forPageBuilder)
{
$this->page = $page;
$this->forPageBuilder = $forPageBuilder;
}

/**
* Render the given theme block with the given stored block data.
*
* @param ThemeBlock $themeBlock
* @param $blockData
* @return string
*/
public function render(ThemeBlock $themeBlock, $blockData)
{
if ($themeBlock->isHtmlBlock()) {
return $this->renderHtmlBlock($themeBlock, $blockData);
} else {
return $this->renderDynamicBlock($themeBlock, $blockData);
}
}

/**
* Render the given html theme block with the given stored block data.
*
* @param ThemeBlock $themeBlock
* @param $blockData
* @return string
*/
protected function renderHtmlBlock(ThemeBlock $themeBlock, $blockData)
{
if (! empty($blockData)) {
$html = $blockData;
} else {
$html = file_get_contents($themeBlock->getViewFile());
}
return $html;
}

/**
* Render the given dynamic theme block with the given stored block data.
*
* @param ThemeBlock $themeBlock
* @param $blockData
* @return string
*/
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();

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

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

ob_start();
require $themeBlock->getViewFile();
$html = ob_get_contents();
ob_end_clean();

return $html;
}
}
44 changes: 15 additions & 29 deletions src/Modules/GrapesJS/PageRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use PHPageBuilder\Contracts\PageContract;
use PHPageBuilder\Contracts\ThemeContract;
use PHPageBuilder\Modules\GrapesJS\Block\BaseModel;
use PHPageBuilder\Modules\GrapesJS\Block\BlockViewFunctions;
use PHPageBuilder\Modules\GrapesJS\Block\BlockRenderer;
use PHPageBuilder\ThemeBlock;
use Exception;

Expand Down Expand Up @@ -132,44 +131,30 @@ public function renderBody()

/**
* Include a rendered theme block with the given slug, data instance id and data context.
* Note: this method is called from php blocks, layout files or after parsing shortcodes.
* This method is called on parsing shortcodes.
*
* @param $slug
* @param null $id the id with which data for this block is stored
* @param null $parentBlockId
* @return false|string
* @return string
*/
public function renderBlock($slug, $id = null, $parentBlockId = null)
{
$html = '';
$themeBlock = new ThemeBlock($this->theme, $slug);
$blockData = $this->pageBlocksData;

if ($themeBlock->isHtmlBlock()) {
// if for this block id in the parent block's context is html data stored, use that html for this block
if (! is_null($parentBlockId) && isset($blockData[$parentBlockId]) && isset($blockData[$parentBlockId][$id])) {
$html = $blockData[$parentBlockId][$id];
} else {
$html = file_get_contents($themeBlock->getViewFile());
}
} else {
$modelPath = $themeBlock->getModelFile();
require_once $modelPath;

$block = new BaseModel;
$data = $blockData[$id] ?? [];
$block->init($themeBlock, $data, $this->forPageBuilder);

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

ob_start();
require $themeBlock->getViewFile();
$html = ob_get_contents();
ob_end_clean();
$pageBlocksData = $this->pageBlocksData;
$blockData = null;
// get data for this block stored in the context of the parent block
if (! is_null($parentBlockId) && isset($pageBlocksData[$parentBlockId]) && isset($pageBlocksData[$parentBlockId][$id])) {
$blockData = $pageBlocksData[$parentBlockId][$id];
} elseif (isset($pageBlocksData[$id])) {
// if no data is stored in context of the parent block, get data stored for this block's id
$blockData = $pageBlocksData[$id];
}

$blockRenderer = new BlockRenderer($this->page, $this->forPageBuilder);
$html = $blockRenderer->render($themeBlock, $blockData);

if ($this->forPageBuilder) {
$id = $id ?? $slug;
$html = '<phpb-block block-slug="' . e($slug) . '" block-id="' . e($id) . '" is-html="' . ($themeBlock->isHtmlBlock() ? 'true' : 'false') . '">'
Expand All @@ -192,6 +177,7 @@ public function parseShortcode(string $shortcode, $data = [])
{
$originalData = $this->pageBlocksData;

// parse the shortcode with the data array passed as an argument to this method
$this->pageBlocksData = $data;
$html = $this->shortcodeParser->doShortcodes($shortcode);

Expand Down
66 changes: 61 additions & 5 deletions src/ThemeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPageBuilder;

use PHPageBuilder\Contracts\ThemeContract;
use PHPageBuilder\Modules\GrapesJS\Block\BaseController;
use PHPageBuilder\Modules\GrapesJS\Block\BaseModel;

class ThemeBlock
{
Expand Down Expand Up @@ -34,7 +36,7 @@ public function __construct(ThemeContract $theme, string $blockSlug)

$this->config = [];
if (file_exists($this->getFolder() . '/config.php')) {
$this->config = include $this->getFolder() . '/config.php';
$this->config = require $this->getFolder() . '/config.php';
}
}

Expand All @@ -49,29 +51,83 @@ public function getFolder()
}

/**
* Return the controller file of this theme block.
* Return the namespace to the folder of this theme block.
*
* @return string
*/
protected function getNamespace()
{
$themesPath = phpb_config('theme.folder');
$themesFolderName = basename($themesPath);
$blockFolder = $this->getFolder();
$namespacePath = $themesFolderName . str_replace($themesPath, '', $blockFolder);

// convert each character after a - to uppercase
$namespace = implode('-', array_map('ucfirst', explode('-', $namespacePath)));
// convert each character after a _ to uppercase
$namespace = implode('_', array_map('ucfirst', explode('-', $namespace)));
// convert each character after a / to uppercase
$namespace = implode('/', array_map('ucfirst', explode('/', $namespace)));
// remove all dashes
$namespace = str_replace('-', '', $namespace);
// remove all underscores
$namespace = str_replace('_', '', $namespace);
// replace / by \
$namespace = str_replace('/', '\\', $namespace);

return $namespace;
}

/**
* Return the controller class of this theme block.
*
* @return string
*/
public function getControllerClass()
{
if (file_exists($this->getFolder() . '/controller.php')) {
return $this->getNamespace() . '\\Controller';
}
return BaseController::class;
}

/**
* Return the controller file of this theme block.
*
* @return string|null
*/
public function getControllerFile()
{
if (file_exists($this->getFolder() . '/controller.php')) {
return $this->getFolder() . '/controller.php';
}
return __DIR__ . '/Modules/GrapesJS/Block/BaseController.php';
return null;
}

/**
* Return the model file of this theme block.
* Return the model class of this theme block.
*
* @return string
*/
public function getModelClass()
{
if (file_exists($this->getFolder() . '/model.php')) {
return $this->getNamespace() . '\\Model';
}
return BaseModel::class;
}

/**
* Return the model file of this theme block.
*
* @return string|null
*/
public function getModelFile()
{
if (file_exists($this->getFolder() . '/model.php')) {
return $this->getFolder() . '/model.php';
}
return __DIR__ . '/Modules/GrapesJS/Block/BaseModel.php';
return null;
}

/**
Expand Down

0 comments on commit 6eeab1d

Please sign in to comment.