Files
core/app/CMS/Pipeline/DisplayPipeline.php
T

112 lines
2.5 KiB
PHP

<?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);
}
}
}