1 <?php
2
3 namespace Scaffolder\Compilers\Layout;
4
5 use Illuminate\Support\Facades\File;
6 use Scaffolder\Compilers\AbstractViewCompiler;
7 use Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface;
8 use Scaffolder\Support\FileToCompile;
9 use Scaffolder\Support\PathParser;
10 use stdClass;
11
12 class PageLayoutCompiler extends AbstractViewCompiler
13 {
14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 public function compile($stub, $modelName, $modelData, stdClass $scaffolderConfig, $hash, ScaffolderThemeExtensionInterface $themeExtension, array $extensions, $extra = null)
29 {
30 $this->stub = $stub;
31
32 return $this->setPageTitle($scaffolderConfig)
33 ->setAppName($scaffolderConfig)
34 ->setLinks($extra['links'], $scaffolderConfig)
35 ->replaceRoutePrefix($scaffolderConfig->routing->prefix)
36 ->store($modelName, $scaffolderConfig, $themeExtension->runAfterPageLayoutIsCompiled($this->stub, $scaffolderConfig), new FileToCompile(null, null));
37 }
38
39 40 41 42 43 44 45 46 47 48
49 protected function store($modelName, stdClass $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
50 {
51 $path = PathParser::parse($scaffolderConfig->paths->views) . 'layouts/page.blade.php';
52
53 File::put($path, $compiled);
54
55 return $path;
56 }
57
58 59 60 61 62 63 64
65 private function setPageTitle(stdClass $scaffolderConfig)
66 {
67 $this->stub = str_replace('{{page_title}}', $scaffolderConfig->userInterface->pageTitle, $this->stub);
68
69 return $this;
70 }
71
72 73 74 75 76 77 78
79 private function setAppName(stdClass $scaffolderConfig)
80 {
81 $this->stub = str_replace('{{app_name}}', $scaffolderConfig->name, $this->stub);
82
83 return $this;
84 }
85
86 87 88 89 90 91 92 93
94 private function setLinks($links, stdClass $scaffolderConfig)
95 {
96 $navLinks = '';
97
98 foreach ($links as $link)
99 {
100 $navLinks .= sprintf("
101 <li>
102 <a href='/%s' class='waves-effect'>
103 %s
104 </a>
105 </li>", $scaffolderConfig->routing->prefix . '/' . strtolower($link['modelName']), $link['modelLabel']);
106 }
107
108 $this->stub = str_replace('{{links}}', $navLinks, $this->stub);
109
110 return $this;
111 }
112
113 114 115 116 117 118 119
120 private function replaceRoutePrefix($prefix)
121 {
122 $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
123
124 return $this;
125 }
126 }
127