-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BlockRenderer added, include and call model & controller classes
- Loading branch information
1 parent
ffcbb51
commit 6eeab1d
Showing
3 changed files
with
178 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters