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 must not hold any more. |
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
|
|
|
$uniquePages = $this->getUniquePageNames(); |
25
|
|
|
$templates = $this->getTemplateNames(); |
26
|
|
|
|
27
|
|
|
if ($uniquePages->intersect($templates)->isNotEmpty()) { |
28
|
|
|
throw new \Exception('Templates and unique pages must not have the same function names when same model class is used.'); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get all defined unique pages. |
34
|
|
|
* |
35
|
|
|
* @return Collection |
36
|
|
|
*/ |
37
|
|
|
public function getUniquePages() |
38
|
|
|
{ |
39
|
|
|
$pages_trait = new \ReflectionClass('App\UniquePages'); |
40
|
|
|
$pages = $pages_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
41
|
|
|
|
42
|
|
|
return collect($pages); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all defined unique page names. |
47
|
|
|
* |
48
|
|
|
* @return Collection |
49
|
|
|
*/ |
50
|
|
|
public function getUniquePageNames() |
51
|
|
|
{ |
52
|
|
|
return $this->getUniquePages()->pluck('name'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the page names keyed with slugs. |
57
|
|
|
* |
58
|
|
|
* @return Collection |
59
|
|
|
*/ |
60
|
|
|
public function getUniqueSlugs() |
61
|
|
|
{ |
62
|
|
|
return $this->getUniquePageNames()->mapWithKeys(function ($name) { |
63
|
|
|
return [str_slug($name) => $name]; |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get all defined templates. |
69
|
|
|
* |
70
|
|
|
* @return Collection |
71
|
|
|
*/ |
72
|
|
|
public function getTemplates($template_name = false) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$templates_array = []; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$templates_trait = new \ReflectionClass('App\PageTemplates'); |
77
|
|
|
$templates = $templates_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
78
|
|
|
|
79
|
|
|
if (! count($templates)) { |
80
|
|
|
abort(503, trans('backpack::pagemanager.template_not_found')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return collect($templates); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get all defined template names. |
88
|
|
|
* |
89
|
|
|
* @return Collection |
90
|
|
|
*/ |
91
|
|
|
public function getTemplateNames() |
92
|
|
|
{ |
93
|
|
|
return $this->getTemplates()->pluck('name'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.