1
|
|
|
<?php namespace Modules\Page\Composers; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\View\View; |
4
|
|
|
use Illuminate\Filesystem\Filesystem; |
5
|
|
|
use Modules\Core\Foundation\Theme\ThemeManager; |
6
|
|
|
|
7
|
|
|
class TemplateViewComposer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ThemeManager |
11
|
|
|
*/ |
12
|
|
|
private $themeManager; |
13
|
|
|
/** |
14
|
|
|
* @var Filesystem |
15
|
|
|
*/ |
16
|
|
|
private $finder; |
17
|
|
|
|
18
|
|
|
public function __construct(ThemeManager $themeManager, Filesystem $finder) |
19
|
|
|
{ |
20
|
|
|
$this->themeManager = $themeManager; |
21
|
|
|
$this->finder = $finder; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function compose(View $view) |
25
|
|
|
{ |
26
|
|
|
$view->with('all_templates', $this->getTemplates()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function getTemplates() |
30
|
|
|
{ |
31
|
|
|
$path = $this->getCurrentThemeBasePath(); |
32
|
|
|
|
33
|
|
|
$templates = []; |
34
|
|
|
|
35
|
|
|
foreach ($this->finder->allFiles($path . '/views') as $template) { |
36
|
|
|
$relativePath = $template->getRelativePath(); |
37
|
|
|
|
38
|
|
|
if ($this->isLayoutOrPartial($relativePath)) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$templateName = $this->getTemplateName($template); |
43
|
|
|
$file = $this->removeExtensionsFromFilename($template); |
44
|
|
|
|
45
|
|
|
if ($this->hasSubdirectory($relativePath)) { |
46
|
|
|
$templates[str_replace('/', '.', $relativePath) . '.' . $file] = $templateName; |
47
|
|
|
} else { |
48
|
|
|
$templates[$file] = $templateName; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $templates; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the base path of the current theme. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
private function getCurrentThemeBasePath() |
61
|
|
|
{ |
62
|
|
|
return $this->themeManager->find(setting('core::template'))->getPath(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Read template name defined in comments. |
67
|
|
|
* |
68
|
|
|
* @param $template |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function getTemplateName($template) |
73
|
|
|
{ |
74
|
|
|
preg_match("/{{-- Template: (.*) --}}/", $template->getContents(), $templateName); |
75
|
|
|
|
76
|
|
|
if (count($templateName) > 1) { |
77
|
|
|
return $templateName[1]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->getDefaultTemplateName($template); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* If the template name is not defined in comments, build a default. |
85
|
|
|
* |
86
|
|
|
* @param $template |
87
|
|
|
* |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
private function getDefaultTemplateName($template) |
91
|
|
|
{ |
92
|
|
|
$relativePath = $template->getRelativePath(); |
93
|
|
|
$fileName = $this->removeExtensionsFromFilename($template); |
94
|
|
|
|
95
|
|
|
return $this->hasSubdirectory($relativePath) ? $relativePath . '/' . $fileName : $fileName; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check if the given path is a layout or a partial. |
100
|
|
|
* |
101
|
|
|
* @param string $relativePath |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
private function isLayoutOrPartial($relativePath) |
106
|
|
|
{ |
107
|
|
|
return preg_match("#(layouts|partials)#i", $relativePath) === 1; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Remove the extension from the filename. |
112
|
|
|
* |
113
|
|
|
* @param $template |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
private function removeExtensionsFromFilename($template) |
118
|
|
|
{ |
119
|
|
|
return str_replace('.blade.php', '', $template->getFilename()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Check if the relative path is not empty (meaning the template is in a directory). |
124
|
|
|
* |
125
|
|
|
* @param $relativePath |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
private function hasSubdirectory($relativePath) |
130
|
|
|
{ |
131
|
|
|
return ! empty($relativePath); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|