Commit 315fb9c8 authored by nirgendswo's avatar nirgendswo

change logic for middleware

change PageService for parsing
parent 5034a4dc
......@@ -131,7 +131,11 @@ class ImageHelper
*/
protected function save($image, $filePathname)
{
$image->save(base_path().'/public/'.config('app.filesPublicPath').'/'.$filePathname);
$path = base_path().'/public/'.config('app.filesPublicPath').'/'.$filePathname;
if (!file_exists($path)) {
$image->save($path);
}
}
/**
......@@ -178,7 +182,10 @@ class ImageHelper
$absoluteDestinationPath = base_path().'/public/'.config('app.filesPublicPath').'/'.$destinationPath;
if (!file_exists($absoluteDestinationPath)) {
$absoluteSourcePath = base_path().'/'.config('app.filesPath').'/'.$sourcePath;
$image = new ImageResize($absoluteSourcePath);
if (file_exists($absoluteSourcePath)) {
$image = new ImageResize($absoluteSourcePath);
}
}
return $image;
......
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller;
use App\PageService;
/**
* H
*
*
* @author Björn Hase
*/
abstract class PageAbstractController extends Controller
{
/** pageService, parse slug, index, page, action and tags */
protected $pageService;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->pageService = app(PageService::class);
}
}
......@@ -2,10 +2,7 @@
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Facades\PageRepositoryFacade as PageRepository;
use App\PageService;
......@@ -17,41 +14,31 @@ use App\Models\PageModel as Page;
*
* @author Björn Hase
*/
class PageController extends Controller
class PageController extends PageAbstractController
{
/** default view */
const VIEW = 'page';
/** methods */
const METHOD_POST = 'Post';
const METHOD_PUT = 'Put';
/** pageService, handle slug, page number and tags */
protected $pageService;
const RESULT = [];
/**
* Create a new controller instance.
* show single page with slug
*
*
* @return void
* @return View
*/
public function __construct()
public function showAction(Request $request)
{
$this->pageService = app(PageService::class);
}
$page = $this->pageService->getPage();
/**
* search by slug a page, if not found, redirect to page.404
*
* @return PageModel
*/
protected function getPage()
{
$slug = $this->pageService->getSlug();
if (!$page) {
return redirect()->route('page.show', [
'slug' => '404'
]);
}
// get current page with slug
$page = PageRepository::findOne($slug);
return $page;
}
return app()->make('view')->make($page->view(), [
'page' => $page,
'result' => self::RESULT
]);
}
/**
* post single page with slug
......@@ -62,7 +49,7 @@ class PageController extends Controller
*/
public function postAction(Request $request)
{
$page = $this->getPage();
$page = $this->pageService->getPage();
if (!$page) {
return redirect()->route('page.show', [
......@@ -70,21 +57,12 @@ class PageController extends Controller
]);
}
$view = $this->getView($page);
$action = $this->getAction($page, self::METHOD_POST);
$action = $this->processAction($action, $page, $request);
$result = $this->processAction($page, $request);
if (!$action) {
return redirect()->route('page.show', [
'slug' => $page->slug
]);
}
return app()->make('view')->make($view, [
return app()->make('view')->make($page->view(), [
'page' => $page,
'request' => $request,
'result' => $action->getResult()
'result' => $result
]);
}
......@@ -97,7 +75,7 @@ class PageController extends Controller
*/
public function putAction(Request $request)
{
$page = $this->getPage();
$page = $this->pageService->getPage();
if (!$page) {
return redirect()->route('page.show', [
......@@ -105,62 +83,37 @@ class PageController extends Controller
]);
}
$view = $this->getView($page);
$action = $this->getAction($page, self::METHOD_PUT);
$action = $this->processAction($action, $page, $request);
$result = $this->processAction($page, $request);
if (!$action) {
return redirect()->route('page.show', [
'slug' => $page->slug
]);
}
return app()->make('view')->make($view, [
return app()->make('view')->make($page->view(), [
'page' => $page,
'request' => $request,
'result' => $action->getResult()
'result' => $result
]);
}
/**
* process action if method was put or post and a action
* was defined in the theme for a slug
*
* @param Mixed $action
* @param PageModel $page
* @param Request $request
* @return Mixed
* @return array
*/
protected function processAction($action, $page, $request)
protected function processAction($page, $request)
{
if ($action) {
$result = self::RESULT;
if (isset($request->action)) {
$action = new $action($page, $request);
$action->process();
}
return $action;
}
/**
* show single page with slug
*
*
* @return View
*/
public function showAction()
{
$page = $this->getPage();
if (!$page) {
return redirect()->route('page.show', [
'slug' => '404'
]);
$result = $action->getResult();
}
return app()->make('view')->make($this->getView($page), [
'page' => $page,
'result' => []
]);
}
return $result;
}
/**
* show single page as json
......@@ -173,48 +126,4 @@ class PageController extends Controller
$page = $this->getPage();
return response()->json($page);
}
/**
* get view of current page,
* if view is not set, use default const
*
* @param string $page
* @return string
*/
private function getView(Page $page)
{
$theme = env('APP_THEME');
if (isset($page->data['view'])) {
$view = $page->data['view'];
} else {
$view = self::VIEW;
}
$view = $theme.'.views.'.$view;
return $view;
}
/**
* get view of current page,
* if view is not set, use default const
*
* @param string $page
* @return string
*/
private function getAction(Page $page, $method)
{
$action = NULL;
$theme = env('APP_THEME');
$className = ucfirst(camel_case(str_replace('/', '', $page->slug)));
$classPath = '\\Themes\\'.$theme.'\\Actions\\'.$className.$method.'Action';
if (class_exists($classPath)) {
$action = $classPath;
}
return $action;
}
}
......@@ -6,11 +6,11 @@ use Closure;
use App\PageService;
/**
* Create PageService
* middleware for PageService
*
* @author Björn Hase
*/
class Parameters
class PageMiddleware
{
/**
* Handle an incoming request.
......
......@@ -12,6 +12,9 @@ use App\Helpers\MarkdownHelper;
*/
class PageModel
{
/** default view */
const VIEW = 'page';
/* slug */
public $slug;
......@@ -42,7 +45,30 @@ class PageModel
}
/**
* getting html
* getting current view of page and theme
*
* @return string view
*/
public function view()
{
// getting current theme
$theme = env('APP_THEME');
// check if view is set, if not use default
if (isset($this->data['view'])) {
$view = $this->data['view'];
} else {
$view = self::VIEW;
}
// create path to view
$view = $theme.'.views.'.$view;
return $view;
}
/**
* getting markdown as html
*
* @return $string
*/
......@@ -51,4 +77,14 @@ class PageModel
$markdownHelper = new MarkdownHelper();
return $markdownHelper->text($this->markdown);
}
/**
* rendering current page with view
*
* @return $string
*/
public function render()
{
return View::make($this->view(), ['page' => $this]);
}
}
......@@ -31,7 +31,8 @@ class AppServiceProvider extends ServiceProvider
});
$this->app->bind('App\PageRepository', function() {
return new \App\Repositories\PageRepository();
$request = app(\Illuminate\Http\Request::class);
return new \App\Repositories\PageRepository($request);
});
}
}
......@@ -33,6 +33,9 @@ class PageRepository
/** regex to get current filepath */
const REGEX_FILEPATH = '/\..*+$/';
/** request */
protected $request;
/** directory for md-files */
protected $repositoryPath;
......@@ -54,9 +57,9 @@ class PageRepository
*
* @param array $config
*/
public function __construct()
public function __construct($request)
{
$this->pageService = app(\App\PageService::class);
$this->request = $request;
$this->repositoryPath = base_path().'/'.config('app.repositoryPath');
}
......@@ -117,7 +120,7 @@ class PageRepository
public function paginate(Array $parameters, Array $options)
{
$options = array_merge($this->options, $options);
$options['offset'] = ($this->pageService->getPage() - 1) * $options['limit'];
$options['offset'] = ($this->request->number - 1) * $options['limit'];
$options['depth'] = 0;
return $this->find($parameters, $options);
......
......@@ -4,11 +4,13 @@ namespace App\Services;
use Illuminate\Http\Request;
use App\Facades\PageRepositoryFacade as PageRepository;
/**
* PageService
*
* the page service try to get /p/* /tags/* from path,
* and also modifies path
* and also modifies slug and tries to get page with slug
*
* @author Björn Hase
*/
......@@ -16,23 +18,18 @@ use Illuminate\Http\Request;
class PageService {
/** getting /p/? from slug */
const REGEX_PAGE = '/\/p\/[0-9]*$/';
const REGEX_PAGE_STRING = '/\/p\//';
const REGEX_NUMBER = '/\/p\/[0-9]/';
const REGEX_NUMBER_STRING = '/\/p\//';
/** getting /tags/? from slug */
const REGEX_TAGS = '/\/tags\/[a-zA-Z0-9,]*$/';
const REGEX_TAGS_STRING = '/\/tags\//';
/** default page */
private $page = 1;
/** default tags */
private $tags = NULL;
/** default slug */
private $slug = NULL;
/**
*
*
* @param Request $request
*/
......@@ -44,18 +41,16 @@ class PageService {
// getting slug parameter from route
// if slug not set empty slug
if (isset($route[2]['slug'])) {
$this->slug = $route[2]['slug'];
$request->merge(['slug' => $route[2]['slug']]);
} else {
$this->slug = '';
$slug = '';
$request->merge(['slug' => $slug]);
}
// adding to path a '/' if it is not empty
//if ($this->slug !== '') {
// $this->slug = '/'.$this->slug;
//}
$this->handlePage($this->slug);
$this->handleTags($this->slug);
$this->parseNumber($request);
$this->parseTags($request);
$this->page = $this->parsePage($request);
$this->parseAction($this->page, $request);
}
/**
......@@ -63,18 +58,24 @@ class PageService {
*
* @param string $slug
*/
private function handlePage($slug)
private function parseNumber(Request $request)
{
preg_match(self::REGEX_PAGE, $slug, $page);
$slug = $request->slug;
preg_match(self::REGEX_NUMBER, $slug, $number);
if ($page) {
$page = array_pop($page);
if (is_array($number) && count($number) > 0) {
$number = array_pop($number);
$this->page = preg_replace(self::REGEX_PAGE_STRING, '', $page);
$this->page = intval($this->page);
$number = preg_replace(self::REGEX_NUMBER_STRING, '', $number);
$number = intval($number);
$this->slug = preg_replace(self::REGEX_PAGE, '', $this->slug);
$slug = preg_replace(self::REGEX_NUMBER, '', $slug);
$request->merge(['slug' => $slug]);
} else {
$number = 1;
}
$request->merge(['number' => $number]);
}
/**
......@@ -82,48 +83,63 @@ class PageService {
*
* @param string $slug
*/
private function handleTags($slug)
private function parseTags(Request $request)
{
$slug = $request->slug;
$tags = NULL;
preg_match(self::REGEX_TAGS, $slug, $this->tags);
preg_match(self::REGEX_TAGS, $slug, $tags);
if ($tags) {
$this->tags = preg_replace(self::REGEX_TAGS_STRING, '', $tags);
if (is_array($tags) && count($tags)) {
$tags = preg_replace(self::REGEX_TAGS_STRING, '', $tags);
$this->tags = $this->tags[0];
$this->tags = explode(',', $this->tags);
$tags = $tags[0];
$tags = explode(',', $tags);
$request->merge(['tags' => $tags]);
$this->slug = preg_replace(self::REGEX_TAGS, '', $this->slug);
$slug = preg_replace(self::REGEX_TAGS, '', $slug);
$request->merge(['slug' => $slug]);
}
}
/**
* get current page
* get view of current page,
* if view is not set, use default const
*
* @return integer
* @param string $page
* @return string
*/
public function getPage()
private function parseAction($page, Request $request)
{
return $this->page;
if ($page && $request->getMethod()) {
$theme = env('APP_THEME');
$className = ucfirst(camel_case(str_replace('/', '', $page->slug)));
$classPath = '\\Themes\\'.$theme.'\\Actions\\'.$className.$request->getMethod().'Action';
if (class_exists($classPath)) {
$request->merge(['action' => $classPath]);
}
}
}
/**
* getting current tags
* find one page with slug
*
* @return array
* @param Request $request
* @return mixed
*/
public function getTags()
private function parsePage(Request $request)
{
return $this->tags;
return PageRepository::findOne($request->slug);
}
/**
* getting current slug
* returning Page
*
* @return string
* @return mixed
*/
public function getSlug()
public function getPage()
{
return $this->slug;
return $this->page;
}
}
......@@ -66,7 +66,7 @@ $app->singleton('App\PageService', function($app) {
*/
$app->routeMiddleware([
'parameters' => App\Http\Middleware\Parameters::class,
'page' => App\Http\Middleware\PageMiddleware::class,
]);
/*
......
<svg width="200" height="200" fill="#000000" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 100 100" enable-background="new 0 0 100 100" xml:space="preserve"><path d="M95.71,50c0-1.218-6.899-0.794-7.502-1.92c-0.575-1.112,5.031-4.672,4.723-5.801c-0.314-1.136-6.961-1.369-7.016-2.611 c-0.048-1.256,6.304-4.104,5.777-5.178c-0.521-1.078-6.771,2.04-7.701,1.191c-0.931-0.853,3.313-5.887,2.731-6.903 s-7.084,0.168-7.345-1.064c-0.273-1.239,5.538-5.106,4.867-6.089c-0.685-0.989-6.305,3.087-7.365,2.42 c-1.062-0.664,2.073-6.523,1.253-7.365c-0.815-0.849-6.729,2.201-7.393,1.146c-0.686-1.074,3.155-6.821,2.107-7.427 c-1.047-0.605-4.113,5.586-5.387,5.541c-1.239-0.048-1.561-6.694-2.703-6.985c-1.137-0.28-4.635,5.363-5.743,4.774 c-1.109-0.585-0.377-7.492-1.581-7.584c-1.171-0.089-1.637,6.883-2.841,7.263c-1.198,0.38-3.414-5.843-4.607-5.843 c-1.184,0-3.407,6.223-4.593,5.843c-1.211-0.38-1.661-7.352-2.839-7.263c-1.199,0.092-0.486,6.999-1.591,7.584 c-1.114,0.589-4.605-5.055-5.738-4.771c-1.155,0.291-1.472,6.937-2.705,6.985c-1.284,0.044-4.342-6.147-5.391-5.541 c-1.041,0.605,2.793,6.355,2.106,7.43c-0.664,1.054-6.571-1.999-7.379-1.153c-0.823,0.842,2.311,6.701,1.253,7.365 c-1.06,0.667-6.685-3.409-7.36-2.42c-0.683,0.982,5.127,4.85,4.861,6.089c-0.269,1.232-6.759,0.044-7.348,1.064 c-0.585,1.013,3.66,6.054,2.721,6.906c-0.933,0.846-7.175-2.266-7.695-1.19c-0.521,1.078,5.827,3.915,5.777,5.174 c-0.041,1.243-6.688,1.468-7.004,2.608c-0.316,1.13,5.292,4.696,4.714,5.808C11.187,49.203,4.291,48.782,4.291,50 c0,1.222,6.903,0.794,7.485,1.916c0.577,1.113-5.031,4.676-4.714,5.805c0.317,1.141,6.957,1.376,7.004,2.605 c0.042,1.27-6.303,4.109-5.781,5.178c0.524,1.082,6.763-2.033,7.696-1.188c0.939,0.855-3.307,5.89-2.723,6.906 c0.59,1.023,7.077-0.168,7.35,1.068c0.269,1.238-5.541,5.103-4.862,6.085c0.676,0.996,6.299-3.08,7.36-2.417 c1.064,0.664-2.075,6.521-1.252,7.362c0.827,0.853,6.725-2.204,7.389-1.157c0.688,1.075-3.147,6.832-2.102,7.431 c1.049,0.605,4.114-5.596,5.389-5.541c1.239,0.048,1.559,6.701,2.711,6.985c1.133,0.294,4.627-5.35,5.736-4.761 c1.102,0.575,0.381,7.488,1.586,7.577c1.169,0.086,1.631-6.875,2.842-7.259c1.193-0.38,3.413,5.846,4.6,5.846 c1.186,0,3.403-6.232,4.602-5.846c1.204,0.384,1.656,7.341,2.841,7.259c1.204-0.096,0.485-7.006,1.588-7.584 c1.108-0.592,4.606,5.048,5.736,4.764c1.149-0.294,1.465-6.934,2.703-6.981c1.287-0.041,4.347,6.146,5.394,5.541 c1.041-0.593-2.792-6.355-2.114-7.431c0.67-1.047,6.57,2.01,7.398,1.157c0.814-0.839-2.313-6.698-1.259-7.362 c1.061-0.663,6.681,3.413,7.365,2.424c0.671-0.982-5.134-4.854-4.867-6.092c0.274-1.236,6.77-0.052,7.358-1.071 c0.575-1.017-3.669-6.048-2.725-6.903c0.924-0.839,7.16,2.283,7.681,1.194c0.526-1.078-5.825-3.915-5.771-5.185 c0.041-1.232,6.694-1.465,7.016-2.605c0.309-1.129-5.298-4.682-4.723-5.801C88.811,50.805,95.71,51.222,95.71,50z M36.273,51.479 c-5.137,0-9.299-4.165-9.299-9.299c0-5.137,4.162-9.302,9.299-9.302c5.138,0,9.301,4.165,9.301,9.302 C45.574,47.313,41.411,51.479,36.273,51.479z M63.326,51.479c-5.133,0-9.295-4.165-9.295-9.299c0-5.137,4.162-9.302,9.295-9.302 c5.141,0,9.303,4.165,9.303,9.302C72.629,47.313,68.467,51.479,63.326,51.479z"/><path d="M36.273,38.675c-1.932,0-3.501,1.571-3.501,3.505c0,1.93,1.569,3.501,3.501,3.501c1.934,0,3.503-1.571,3.503-3.501 C39.776,40.246,38.207,38.675,36.273,38.675z"/><path d="M63.326,38.675c-1.93,0-3.497,1.571-3.497,3.505c0,1.93,1.567,3.501,3.497,3.501c1.938,0,3.505-1.571,3.505-3.501 C66.831,40.246,65.264,38.675,63.326,38.675z"/></svg>
\ No newline at end of file
......@@ -18,9 +18,9 @@
</h1>
<hr>
<div class="grid grid--justify-center">
@foreach($pageRepository->find(
@foreach($pageRepository->paginate(
['type' => 'post'],
['parent' => '/blog', 'orderBy' => ['createdAt' => 'asc']]) as $post)
['parent' => '/blog', 'limit' => 10, 'orderBy' => ['createdAt' => 'asc']]) as $post)
<div class="grid__col-xs-12 grid__col-md-10">
<div class="box box--post">
<article class="post">
......
......@@ -20,7 +20,7 @@ $app->get('rss/{type}', [
]);
// get the hole url as slug
$app->group(['middleware' => 'parameters'], function() use ($app) {
$app->group(['middleware' => 'page'], function() use ($app) {
$app->get('json/{slug:[0-9\/a-z,-]+}', [
'as' => 'page.json', 'uses' => 'PageController@jsonAction'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment