FEATURE #2350 @2h: add pipeline for displaying content

This commit is contained in:
Michal Kváček
2017-03-08 23:38:37 +01:00
parent 44fb6b1be5
commit 450000fadd
10 changed files with 222 additions and 11 deletions
@@ -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);
}
+1 -1
View File
@@ -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()
-3
View File
@@ -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);
}
+112
View File
@@ -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.
}
}
+16 -7
View File
@@ -3,7 +3,6 @@
namespace App\Http\Controllers\Visitor;
use App\CMS\Contracts\Repositories\Content;
use App\CMS\Helpers\PageRenderer;
use App\Http\Controllers\Controller;
use App\Models\Content\Content as ContentModel;
use Illuminate\Support\Facades\Auth;
@@ -24,11 +23,20 @@ class ContentPage extends Controller
$this->content = $content;
}
/**
* Load page by URL
*
* This method also caches content
*
* @param $url
*
* @return mixed
*/
public function getPage($url)
{
$contentModel = $this->content;
$page = Cache::remember('content-' . $url, setting('contentCacheLifetime', 5), function () use ($contentModel, $url) {
return $contentModel->getByURL($url);
$contentRepository = $this->content;
$page = Cache::remember('content-' . $url, setting('contentCacheLifetime', 5), function () use ($contentRepository, $url) {
return $contentRepository->getByURL($url);
});
// no page available
@@ -54,9 +62,10 @@ class ContentPage extends Controller
abort(404);
}
// add one new "view" for page
$page->view();
$displayPipeline = app('displayPipeline');
$page = $displayPipeline->run($page);
return Controller::VisitorsView($page->template, ['page' => $page]);
return PageRenderer::render($page);
}
}
+17
View File
@@ -13,6 +13,9 @@ use App\CMS\Handlers\Search\Content;
use App\CMS\Handlers\Search\Files;
use App\CMS\Handlers\Search\Presenters\AdminContent as AdminContentPresenter;
use App\CMS\Handlers\Search\Presenters\AdminFiles;
use App\CMS\Pipeline\DisplayPipeline;
use App\CMS\Pipeline\Process\AddViewedToPage;
use App\CMS\Pipeline\Process\CompileShortcodes;
use App\CMS\Repositories\Content\Content as ContentRepository;
use App\CMS\Repositories\Module as ModuleRepository;
use App\CMS\Repositories\Templates\Template as TemplateRepository;
@@ -41,6 +44,7 @@ class AppServiceProvider extends ServiceProvider
$this->registerWidgets();
$this->registerSettings();
$this->registerSearch();
$this->registerDisplayPipeline();
}
}
@@ -64,6 +68,11 @@ class AppServiceProvider extends ServiceProvider
return new WidgetRegistrator($app);
});
// create singleton for display pipeline - pipeline used for displaying visitor pages
$this->app->singleton('displayPipeline', function ($app) {
return new DisplayPipeline();
});
// create registrator for template directories
$this->app->singleton('registerTemplateDir', function ($app) {
return new TemplateRegistrator($app);
@@ -115,4 +124,12 @@ class AppServiceProvider extends ServiceProvider
$registrator->register(new Content(), new AdminContentPresenter(), SearchEngineRegistrator::ADMIN);
$registrator->register(new Files(), new AdminFiles(), SearchEngineRegistrator::ADMIN);
}
private function registerDisplayPipeline()
{
$registrator = $this->app->make('displayPipeline');
$registrator->add(AddViewedToPage::class);
$registrator->add(CompileShortcodes::class);
}
}