1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PageManager\app; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
trait TraitReflections |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Check for equal named templates and unique pages |
12
|
|
|
* |
13
|
|
|
* As the method name of a unique page will also be the template name in the database, |
14
|
|
|
* we must ensure that there are not any equal names defined. |
15
|
|
|
* If different Models (and so different tables in the database) are used, this condition must not hold any more. |
16
|
|
|
* |
17
|
|
|
* @throws \Exception |
18
|
|
|
*/ |
19
|
|
|
public function checkForTemplatesAndUniquePagesNotDistinct() |
20
|
|
|
{ |
21
|
|
|
if (config('backpack.pagemanager.page_model_class') !== config('backpack.pagemanager.unique_page_model_class')) { |
|
|
|
|
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$uniquePages = $this->getUniquePageNames(); |
26
|
|
|
$templates = $this->getTemplateNames(); |
27
|
|
|
|
28
|
|
|
if ($uniquePages->intersect($templates)->isNotEmpty()) { |
29
|
|
|
throw new \Exception('Templates and unique pages must not have the same function names when same model class is used.'); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get all defined unique pages. |
35
|
|
|
* |
36
|
|
|
* @return Collection |
37
|
|
|
*/ |
38
|
|
|
public function getUniquePages() |
39
|
|
|
{ |
40
|
|
|
$pages_trait = new \ReflectionClass('App\UniquePages'); |
41
|
|
|
$pages = $pages_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
42
|
|
|
|
43
|
|
|
return collect($pages); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all defined unique page names. |
48
|
|
|
* |
49
|
|
|
* @return Collection |
50
|
|
|
*/ |
51
|
|
|
public function getUniquePageNames() |
52
|
|
|
{ |
53
|
|
|
return $this->getUniquePages()->pluck('name'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the page names keyed with slugs. |
58
|
|
|
* |
59
|
|
|
* @return Collection |
60
|
|
|
*/ |
61
|
|
|
public function getUniqueSlugs() |
62
|
|
|
{ |
63
|
|
|
return $this->getUniquePageNames()->mapWithKeys(function ($name) { |
64
|
|
|
return [str_slug($name) => $name]; |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get all defined templates. |
70
|
|
|
* |
71
|
|
|
* @return Collection |
72
|
|
|
*/ |
73
|
|
|
public function getTemplates($template_name = false) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$templates_array = []; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$templates_trait = new \ReflectionClass('App\PageTemplates'); |
78
|
|
|
$templates = $templates_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
79
|
|
|
|
80
|
|
|
if (! count($templates)) { |
81
|
|
|
abort(503, trans('backpack::pagemanager.template_not_found')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return collect($templates); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get all defined template names |
89
|
|
|
* |
90
|
|
|
* @return Collection |
91
|
|
|
*/ |
92
|
|
|
public function getTemplateNames() |
93
|
|
|
{ |
94
|
|
|
return $this->getTemplates()->pluck('name'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.