-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewCompiler.php
More file actions
126 lines (112 loc) · 2.89 KB
/
ViewCompiler.php
File metadata and controls
126 lines (112 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Cradle;
use Twig\Environment;
use Cradle\View;
use Twig\Loader\FilesystemLoader;
use Cradle\Exceptions\CompileException;
/**
* A compiler for the views that will eventually be sent to the client.
*/
class ViewCompiler
{
/** @var array $views Stores the view objects in the order that they will be compiled. */
protected $views = [];
/** @var array $defaultParameters These parameters are passed into every compiled view file. */
protected $defaultParameters = [];
/**
* Adds a new view object to the array of view objects to be compiled later.
*
* @param View $view The view to be compiled
* @return null
*/
public function addView(View $view): void
{
array_push($this->views, $view);
}
/**
* Compiles a view object for output.
*
* @param View $view The view to be compiled
* @return string|null The output of the compiled view
*/
protected function compileView(View $view): ?string
{
// Check if the file to be compiled exists
$filePath = $view->getFullFilePath();
if (!file_exists($filePath)) {
throw new CompileException($filePath);
}
// Compile the view file
$filePath = $view->getRelativeFilePath();
$parameters = array_merge($this->defaultParameters, $view->getParameters());
$loader = new FilesystemLoader(RESOURCES_DIR . '/views');
$twig = new Environment($loader, []);
$output = $twig->render($filePath, $parameters);
// Return the parsed view
return $output;
}
/**
* Compiles all the view objects in the order in which they were added.
* It returns the concatenated output.
*
* @return string The concatenated output of all the indivdual views
*/
public function compileViews(): string
{
$output = '';
foreach ($this->views as $view) {
$output .= $this->compileView($view);
}
return $output;
}
/**
* Clears the views in the view compiler.
*
* @return void
*/
public function clearViews(): void
{
$this->views = [];
}
/**
* Set a default parameter to be passed into every compiled view.
*
* @param string $name
* @param string $value
* @return boolean
*/
public function setDefaultParameter(string $name, string $value): void
{
$this->defaultParameters[$name] = $value;
}
/**
* Get a default parameter.
*
* @param string $name
* @return string
*/
public function getDefaultParameter(string $name): string
{
return $this->defaultParameters[$name];
}
/**
* Unset a default parameter.
*
* @param string $name
* @return void
*/
public function removeDefaultParameter(string $name): void
{
unset($this->defaultParameters[$name]);
}
/**
* Checks if a default parameter has been set.
*
* @param string $name
* @return boolean
*/
public function hasDefaultParameter(string $name): bool
{
return isset($this->defaultParameters[$name]);
}
}