FEATURE #2350 @2h: add pipeline for displaying content
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace App\CMS\Contracts\Content\DisplayPipeline;
|
||||
|
||||
use App\Models\Content\Content;
|
||||
|
||||
interface Pipeline {
|
||||
/**
|
||||
* Add new process into pipeline
|
||||
*
|
||||
* @param Process $proces
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
*/
|
||||
function add($process);
|
||||
function run(Content $page);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace App\CMS\Contracts\Content\DisplayPipeline;
|
||||
|
||||
use App\Models\Content\Content;
|
||||
|
||||
interface Process {
|
||||
/**
|
||||
* Process and modify given page
|
||||
*
|
||||
* @param Content $page
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function handle(Content & $page);
|
||||
}
|
||||
@@ -30,7 +30,7 @@ abstract class Handler extends BaseHandler implements AppHandler
|
||||
*/
|
||||
public function render(Content $content, $parameters)
|
||||
{
|
||||
return Controller::VisitorsView($content->template, ['page' => $content, 'extra' => $parameters]);
|
||||
|
||||
}
|
||||
|
||||
public static function getExtendedFormSections()
|
||||
|
||||
@@ -29,11 +29,8 @@ class PageRenderer
|
||||
/**
|
||||
* @var $handler AppHandler
|
||||
*/
|
||||
// get handler
|
||||
$handler = new $page->moduleHandler;
|
||||
|
||||
$page->content = Shortcode::compile($page->content);
|
||||
|
||||
// render page
|
||||
return $handler->render($page, $parameters);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
namespace App\CMS\Pipeline;
|
||||
|
||||
use App\CMS\Contracts\Content\DisplayPipeline\Pipeline;
|
||||
use App\CMS\Contracts\Content\DisplayPipeline\Process;
|
||||
use App\CMS\Pipeline\Exceptions\InvalidProcessType;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Content\Content;
|
||||
|
||||
class DisplayPipeline implements Pipeline
|
||||
{
|
||||
protected $processes = [];
|
||||
protected $beforeStart = null;
|
||||
protected $beforeRender = null;
|
||||
|
||||
/**
|
||||
* Add new process into pipeline
|
||||
*
|
||||
* @param $process
|
||||
*
|
||||
* @return void
|
||||
* @throws InvalidProcessType
|
||||
*/
|
||||
function add($process)
|
||||
{
|
||||
$this->checkValidProcess($process);
|
||||
|
||||
$this->processes[] = $process;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if given classname implements Process interface
|
||||
*
|
||||
* @param $classname
|
||||
*
|
||||
* @throws InvalidProcessType
|
||||
*/
|
||||
protected function checkValidProcess($classname)
|
||||
{
|
||||
if (!in_array(Process::class, class_implements($classname))) {
|
||||
throw new InvalidProcessType($classname);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $beforeStart
|
||||
*/
|
||||
public function setBeforeStart($beforeStart)
|
||||
{
|
||||
$this->checkValidProcess($beforeStart);
|
||||
|
||||
if (!is_null($this->beforeStart)) {
|
||||
array_unshift($this->processes, $this->beforeStart);
|
||||
}
|
||||
|
||||
$this->beforeStart = $beforeStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $beforeRender
|
||||
*/
|
||||
public function setBeforeRender($beforeRender)
|
||||
{
|
||||
$this->checkValidProcess($beforeRender);
|
||||
|
||||
if (!is_null($this->beforeRender)) {
|
||||
array_push($this->processes, $this->beforeRender);
|
||||
}
|
||||
|
||||
$this->beforeRender = $beforeRender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run pipeline
|
||||
*
|
||||
* @param Content $page
|
||||
*
|
||||
* @return Content
|
||||
*/
|
||||
function run(Content $page)
|
||||
{
|
||||
$this->runBeforeStart($page);
|
||||
|
||||
foreach ($this->processes as $process) {
|
||||
/**
|
||||
* @var $run Process
|
||||
*/
|
||||
$run = new $process;
|
||||
$run->handle($page);
|
||||
}
|
||||
|
||||
$this->runBeforeRender($page);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
private function runBeforeStart($page)
|
||||
{
|
||||
if (!is_null($this->beforeStart)) {
|
||||
$process = new $this->beforeStart;
|
||||
$process->handle($page);
|
||||
}
|
||||
}
|
||||
|
||||
private function runBeforeRender($page)
|
||||
{
|
||||
if (!is_null($this->beforeRender)) {
|
||||
$process = new $this->beforeRender;
|
||||
$process->handle($page);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace App\CMS\Pipeline\Exceptions;
|
||||
|
||||
class InvalidProcessType extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace App\CMS\Pipeline\Process;
|
||||
|
||||
use App\CMS\Contracts\Content\DisplayPipeline\Process;
|
||||
use App\Models\Content\Content;
|
||||
|
||||
class AddViewedToPage implements Process {
|
||||
/**
|
||||
* Process and modify given page
|
||||
*
|
||||
* @param Content $page
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function handle(Content & $page)
|
||||
{
|
||||
$page->view();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace App\CMS\Pipeline\Process;
|
||||
|
||||
use App\CMS\Contracts\Content\DisplayPipeline\Process;
|
||||
use App\Models\Content\Content;
|
||||
|
||||
class CompileShortcodes implements Process {
|
||||
/**
|
||||
* Process and modify given page
|
||||
*
|
||||
* @param Content $page
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function handle(Content & $page)
|
||||
{
|
||||
// TODO: Implement handle() method.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user