|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chadanuk\MiniCms; |
|
4
|
|
|
|
|
5
|
|
|
use Chadanuk\MiniCms\Page; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Routing\Route; |
|
8
|
|
|
use Illuminate\Routing\Router; |
|
9
|
|
|
|
|
10
|
|
|
class MiniCms |
|
11
|
|
|
{ |
|
12
|
|
|
public $pageBlocks = null; |
|
13
|
|
|
|
|
14
|
|
|
public function createPage(array $pageData = []): Page |
|
15
|
|
|
{ |
|
16
|
|
|
return Page::create($pageData); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function getPageBySlug(string $slug): Page |
|
20
|
|
|
{ |
|
21
|
|
|
return Page::where('slug', $slug)->first(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function renderAdmin() |
|
25
|
|
|
{ |
|
26
|
|
|
$request = request(); |
|
27
|
|
|
|
|
28
|
|
|
if (!$request->is(config('mini-cms.admin-path') . '/*')) { |
|
29
|
|
|
return response('', 404); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$adminRouteServiceProvider = new MiniCmsAdminRouteServiceProvider(app()); |
|
33
|
|
|
$adminRouteServiceProvider->boot(); |
|
34
|
|
|
$response = null; |
|
35
|
|
|
|
|
36
|
|
|
$routes = collect(app()->routes->getRoutes())->filter( |
|
37
|
|
|
function (Route $route) use ($request, $response) { |
|
38
|
|
|
|
|
39
|
|
|
return is_string($route->action['uses']) && stristr($route->action['uses'], 'Chadanuk\MiniCms') !== false && $route->matches($request) && $route->uri !== config('mini-cms.admin-path') . '/mini-cms/{miniCmsPath}'; |
|
40
|
|
|
} |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
if ($routes->count() === 0) { |
|
44
|
|
|
return ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$route = $routes->first(); |
|
48
|
|
|
|
|
49
|
|
|
return $route->bind($request)->run(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getBlockOutput(String $type, String $label, string $pageSlug = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$request = request(); |
|
55
|
|
|
$page = $pageSlug ? Page::findBySlug($pageSlug) : $request->route('page'); |
|
56
|
|
|
|
|
57
|
|
|
if (!$this->pageBlocks) { |
|
58
|
|
|
$this->pageBlocks = $page->pageBlocks(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($this->pageBlocks->count() === 0) { |
|
62
|
|
|
return ''; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->pageBlocks->where('label', $label)->where('type', $type)->first()->render(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function renderBlock(String $type, String $label, string $pageSlug = null) |
|
69
|
|
|
{ |
|
70
|
|
|
echo static::getBlockOutput($type, $label, $pageSlug); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|