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