Commit 315fb9c8 authored by nirgendswo's avatar nirgendswo

change logic for middleware

change PageService for parsing
parent 5034a4dc
...@@ -131,7 +131,11 @@ class ImageHelper ...@@ -131,7 +131,11 @@ class ImageHelper
*/ */
protected function save($image, $filePathname) 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 ...@@ -178,7 +182,10 @@ class ImageHelper
$absoluteDestinationPath = base_path().'/public/'.config('app.filesPublicPath').'/'.$destinationPath; $absoluteDestinationPath = base_path().'/public/'.config('app.filesPublicPath').'/'.$destinationPath;
if (!file_exists($absoluteDestinationPath)) { if (!file_exists($absoluteDestinationPath)) {
$absoluteSourcePath = base_path().'/'.config('app.filesPath').'/'.$sourcePath; $absoluteSourcePath = base_path().'/'.config('app.filesPath').'/'.$sourcePath;
$image = new ImageResize($absoluteSourcePath); if (file_exists($absoluteSourcePath)) {
$image = new ImageResize($absoluteSourcePath);
}
} }
return $image; 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 @@ ...@@ -2,10 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Facades\PageRepositoryFacade as PageRepository; use App\Facades\PageRepositoryFacade as PageRepository;
use App\PageService; use App\PageService;
...@@ -17,41 +14,31 @@ use App\Models\PageModel as Page; ...@@ -17,41 +14,31 @@ use App\Models\PageModel as Page;
* *
* @author Björn Hase * @author Björn Hase
*/ */
class PageController extends Controller class PageController extends PageAbstractController
{ {
/** default view */ const RESULT = [];
const VIEW = 'page';
/** methods */
const METHOD_POST = 'Post';
const METHOD_PUT = 'Put';
/** pageService, handle slug, page number and tags */
protected $pageService;
/** /**
* 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();
}
/** if (!$page) {
* search by slug a page, if not found, redirect to page.404 return redirect()->route('page.show', [
* 'slug' => '404'
* @return PageModel ]);
*/ }
protected function getPage()
{
$slug = $this->pageService->getSlug();
// get current page with slug return app()->make('view')->make($page->view(), [
$page = PageRepository::findOne($slug); 'page' => $page,
return $page; 'result' => self::RESULT
} ]);
}
/** /**
* post single page with slug * post single page with slug
...@@ -62,7 +49,7 @@ class PageController extends Controller ...@@ -62,7 +49,7 @@ class PageController extends Controller
*/ */
public function postAction(Request $request) public function postAction(Request $request)
{ {
$page = $this->getPage(); $page = $this->pageService->getPage();
if (!$page) { if (!$page) {
return redirect()->route('page.show', [ return redirect()->route('page.show', [
...@@ -70,21 +57,12 @@ class PageController extends Controller ...@@ -70,21 +57,12 @@ class PageController extends Controller
]); ]);
} }
$view = $this->getView($page); $result = $this->processAction($page, $request);
$action = $this->getAction($page, self::METHOD_POST);
$action = $this->processAction($action, $page, $request);
if (!$action) { return app()->make('view')->make($page->view(), [
return redirect()->route('page.show', [
'slug' => $page->slug
]);
}
return app()->make('view')->make($view, [
'page' => $page, 'page' => $page,
'request' => $request, 'request' => $request,
'result' => $action->getResult() 'result' => $result
]); ]);
} }
...@@ -97,7 +75,7 @@ class PageController extends Controller ...@@ -97,7 +75,7 @@ class PageController extends Controller
*/ */
public function putAction(Request $request) public function putAction(Request $request)
{ {
$page = $this->getPage(); $page = $this->pageService->getPage();
if (!$page) { if (!$page) {
return redirect()->route('page.show', [ return redirect()->route('page.show', [
...@@ -105,62 +83,37 @@ class PageController extends Controller ...@@ -105,62 +83,37 @@ class PageController extends Controller
]); ]);
} }
$view = $this->getView($page); $result = $this->processAction($page, $request);
$action = $this->getAction($page, self::METHOD_PUT);
$action = $this->processAction($action, $page, $request);
if (!$action) { return app()->make('view')->make($page->view(), [
return redirect()->route('page.show', [
'slug' => $page->slug
]);
}
return app()->make('view')->make($view, [
'page' => $page, 'page' => $page,
'request' => $request, '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 Mixed $action
* @param PageModel $page * @param PageModel $page
* @param Request $request * @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 = new $action($page, $request);
$action->process(); $action->process();
}
return $action;
}
/** $result = $action->getResult();
* show single page with slug
*
*
* @return View
*/
public function showAction()
{
$page = $this->getPage();
if (!$page) {
return redirect()->route('page.show', [
'slug' => '404'
]);
} }
return app()->make('view')->make($this->getView($page), [ return $result;
'page' => $page, }
'result' => []
]);
}
/** /**
* show single page as json * show single page as json
...@@ -173,48 +126,4 @@ class PageController extends Controller ...@@ -173,48 +126,4 @@ class PageController extends Controller
$page = $this->getPage(); $page = $this->getPage();
return response()->json($page); 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; ...@@ -6,11 +6,11 @@ use Closure;
use App\PageService; use App\PageService;
/** /**
* Create PageService * middleware for PageService
* *
* @author Björn Hase * @author Björn Hase
*/ */
class Parameters class PageMiddleware
{ {
/** /**
* Handle an incoming request. * Handle an incoming request.
......
...@@ -12,6 +12,9 @@ use App\Helpers\MarkdownHelper; ...@@ -12,6 +12,9 @@ use App\Helpers\MarkdownHelper;
*/ */
class PageModel class PageModel
{ {
/** default view */
const VIEW = 'page';
/* slug */ /* slug */
public $slug; public $slug;
...@@ -42,7 +45,30 @@ class PageModel ...@@ -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 * @return $string
*/ */
...@@ -51,4 +77,14 @@ class PageModel ...@@ -51,4 +77,14 @@ class PageModel
$markdownHelper = new MarkdownHelper(); $markdownHelper = new MarkdownHelper();
return $markdownHelper->text($this->markdown); 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 ...@@ -31,7 +31,8 @@ class AppServiceProvider extends ServiceProvider
}); });
$this->app->bind('App\PageRepository', function() { $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 ...@@ -33,6 +33,9 @@ class PageRepository
/** regex to get current filepath */ /** regex to get current filepath */
const REGEX_FILEPATH = '/\..*+$/'; const REGEX_FILEPATH = '/\..*+$/';
/** request */
protected $request;
/** directory for md-files */ /** directory for md-files */
protected $repositoryPath; protected $repositoryPath;
...@@ -54,9 +57,9 @@ class PageRepository ...@@ -54,9 +57,9 @@ class PageRepository
* *
* @param array $config * @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'); $this->repositoryPath = base_path().'/'.config('app.repositoryPath');
} }
...@@ -117,7 +120,7 @@ class PageRepository ...@@ -117,7 +120,7 @@ class PageRepository
public function paginate(Array $parameters, Array $options) public function paginate(Array $parameters, Array $options)
{ {
$options = array_merge($this->options, $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; $options['depth'] = 0;
return $this->find($parameters, $options); return $this->find($parameters, $options);
......
...@@ -4,11 +4,13 @@ namespace App\Services; ...@@ -4,11 +4,13 @@ namespace App\Services;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Facades\PageRepositoryFacade as PageRepository;
/** /**
* PageService * PageService
* *
* the page service try to get /p/* /tags/* from path, * 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 * @author Björn Hase
*/ */
...@@ -16,23 +18,18 @@ use Illuminate\Http\Request; ...@@ -16,23 +18,18 @@ use Illuminate\Http\Request;
class PageService { class PageService {
/** getting /p/? from slug */ /** getting /p/? from slug */
const REGEX_PAGE = '/\/p\/[0-9]*$/'; const REGEX_NUMBER = '/\/p\/[0-9]/';
const REGEX_PAGE_STRING = '/\/p\//'; const REGEX_NUMBER_STRING = '/\/p\//';
/** getting /tags/? from slug */ /** getting /tags/? from slug */
const REGEX_TAGS = '/\/tags\/[a-zA-Z0-9,]*$/'; const REGEX_TAGS = '/\/tags\/[a-zA-Z0-9,]*$/';
const REGEX_TAGS_STRING = '/\/tags\//'; const REGEX_TAGS_STRING = '/\/tags\//';
/** default page */
private $page = 1;
/** default tags */
private $tags = NULL;
/** default slug */ /** default slug */
private $slug = NULL; private $slug = NULL;
/** /**
*
* *
* @param Request $request * @param Request $request
*/ */
...@@ -44,18 +41,16 @@ class PageService { ...@@ -44,18 +41,16 @@ class PageService {
// getting slug parameter from route // getting slug parameter from route
// if slug not set empty slug // if slug not set empty slug
if (isset($route[2]['slug'])) { if (isset($route[2]['slug'])) {
$this->slug = $route[2]['slug']; $request->merge(['slug' => $route[2]['slug']]);
} else { } else {
$this->slug = ''; $slug = '';
$request->merge(['slug' => $slug]);
} }
// adding to path a '/' if it is not empty $this->parseNumber($request);
//if ($this->slug !== '') { $this->parseTags($request);
// $this->slug = '/'.$this->slug; $this->page = $this->parsePage($request);
//} $this->parseAction($this->page, $request);
$this->handlePage($this->slug);
$this->handleTags($this->slug);
} }
/** /**
...@@ -63,18 +58,24 @@ class PageService { ...@@ -63,18 +58,24 @@ class PageService {
* *
* @param string $slug * @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) { if (is_array($number) && count($number) > 0) {
$page = array_pop($page); $number = array_pop($number);
$this->page = preg_replace(self::REGEX_PAGE_STRING, '', $page); $number = preg_replace(self::REGEX_NUMBER_STRING, '', $number);
$this->page = intval($this->page); $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 { ...@@ -82,48 +83,63 @@ class PageService {
* *
* @param string $slug * @param string $slug
*/ */
private function handleTags($slug) private function parseTags(Request $request)
{ {
$slug = $request->slug;
$tags = NULL; $tags = NULL;
preg_match(self::REGEX_TAGS, $slug, $this->tags); preg_match(self::REGEX_TAGS, $slug, $tags);
if ($tags) { if (is_array($tags) && count($tags)) {
$this->tags = preg_replace(self::REGEX_TAGS_STRING, '', $tags); $tags = preg_replace(self::REGEX_TAGS_STRING, '', $tags);
$this->tags = $this->tags[0]; $tags = $tags[0];
$this->tags = explode(',', $this->tags); $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) { ...@@ -66,7 +66,7 @@ $app->singleton('App\PageService', function($app) {
*/ */
$app->routeMiddleware([ $app->routeMiddleware([
'parameters' => App\Http\Middleware\Parameters::class, 'page' => App\Http\Middleware\PageMiddleware::class,