1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PageManager\app; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
trait TraitReflections |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Check for equal named templates and unique pages. |
11
|
|
|
* |
12
|
|
|
* As the method name of a unique page will also be the template name in the database, |
13
|
|
|
* we must ensure that there are not any equal names defined. |
14
|
|
|
* If different Models (and so different tables in the database) are used, this condition is obsolete. |
15
|
|
|
* |
16
|
|
|
* @throws \Exception |
17
|
|
|
*/ |
18
|
|
|
public function checkForTemplatesAndUniquePagesNotDistinct() |
19
|
|
|
{ |
20
|
|
|
if (config('backpack.pagemanager.page_model_class') !== config('backpack.pagemanager.unique_page_model_class')) { |
|
|
|
|
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// use the internal function directly to prevent an 503 abort if no pages are defined |
25
|
|
|
$uniquePages = $this->loadUniquePages()->pluck('name'); |
26
|
|
|
// use the internal function directly to prevent an 503 abort if no templates are defined |
27
|
|
|
$templates = $this->loadTemplates()->pluck('name'); |
28
|
|
|
|
29
|
|
|
if ($uniquePages->intersect($templates)->isNotEmpty()) { |
30
|
|
|
throw new \Exception('Templates and unique pages must not have the same function names when same model class is used.'); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/* |
35
|
|
|
|-------------------------------------------------------------------------- |
36
|
|
|
| UNIQUE PAGES |
37
|
|
|
|-------------------------------------------------------------------------- |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Load all defined unique pages. |
42
|
|
|
* |
43
|
|
|
* @return Collection |
44
|
|
|
*/ |
45
|
|
|
private function loadUniquePages() |
46
|
|
|
{ |
47
|
|
|
$pages_trait = new \ReflectionClass('App\UniquePages'); |
48
|
|
|
$pages = $pages_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
49
|
|
|
|
50
|
|
|
return collect($pages); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all defined unique pages. |
55
|
|
|
* |
56
|
|
|
* @return Collection |
57
|
|
|
*/ |
58
|
|
|
public function getUniquePages() |
59
|
|
|
{ |
60
|
|
|
$pages = $this->loadUniquePages(); |
61
|
|
|
|
62
|
|
|
if (! count($pages)) { |
63
|
|
|
abort(503, trans('backpack::pagemanager.template_not_found')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $pages; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get all defined unique page names. |
71
|
|
|
* |
72
|
|
|
* @return Collection |
73
|
|
|
*/ |
74
|
|
|
public function getUniquePageNames() |
75
|
|
|
{ |
76
|
|
|
return $this->getUniquePages()->pluck('name'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the page names keyed with slugs. |
81
|
|
|
* |
82
|
|
|
* @return Collection |
83
|
|
|
*/ |
84
|
|
|
public function getUniqueSlugs() |
85
|
|
|
{ |
86
|
|
|
return $this->getUniquePageNames()->mapWithKeys(function ($name) { |
87
|
|
|
return [str_slug($name) => $name]; |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
|-------------------------------------------------------------------------- |
94
|
|
|
| TEMPLATES |
95
|
|
|
|-------------------------------------------------------------------------- |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Load all defined templates. |
100
|
|
|
* |
101
|
|
|
* @return Collection |
102
|
|
|
*/ |
103
|
|
|
private function loadTemplates() |
104
|
|
|
{ |
105
|
|
|
$templates_trait = new \ReflectionClass('App\PageTemplates'); |
106
|
|
|
$templates = $templates_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
107
|
|
|
|
108
|
|
|
return collect($templates); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get all defined templates. |
113
|
|
|
* |
114
|
|
|
* @return Collection |
115
|
|
|
*/ |
116
|
|
|
public function getTemplates($template_name = false) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$templates = $this->loadTemplates(); |
119
|
|
|
|
120
|
|
|
if (! count($templates)) { |
121
|
|
|
abort(503, trans('backpack::pagemanager.template_not_found')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return collect($templates); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get all defined template names. |
129
|
|
|
* |
130
|
|
|
* @return Collection |
131
|
|
|
*/ |
132
|
|
|
public function getTemplateNames() |
133
|
|
|
{ |
134
|
|
|
return $this->getTemplates()->pluck('name'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.