comparison vendor/chi-teck/drupal-code-generator/src/Asset.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace DrupalCodeGenerator;
4
5 use DrupalCodeGenerator\Helper\Renderer;
6
7 /**
8 * Simple data structure to represent an asset being generated.
9 */
10 class Asset {
11
12 /**
13 * Asset path.
14 *
15 * @var string
16 */
17 protected $path;
18
19 /**
20 * Asset content.
21 *
22 * @var string
23 */
24 protected $content;
25
26 /**
27 * Twig template to render header.
28 *
29 * @var string
30 */
31 protected $headerTemplate;
32
33 /**
34 * Twig template to render main content.
35 *
36 * @var string
37 */
38 protected $template;
39
40 /**
41 * Template variables.
42 *
43 * @var array
44 */
45 protected $vars = [];
46
47 /**
48 * Action.
49 *
50 * This defines an action to take if specified file already exists.
51 *
52 * @var string
53 */
54 protected $action = 'replace';
55
56 /**
57 * Header size.
58 *
59 * @var int
60 */
61 protected $headerSize = 0;
62
63 /**
64 * Asset mode.
65 *
66 * @var int
67 */
68 protected $mode;
69
70 /**
71 * Asset type (file or directory).
72 *
73 * @var string
74 */
75 protected $type = 'file';
76
77 /**
78 * Getter for asset path.
79 *
80 * @return string
81 * Asset path.
82 */
83 public function getPath() {
84 return Utils::tokenReplace($this->path, $this->getVars());
85 }
86
87 /**
88 * Getter for asset content.
89 *
90 * @return string
91 * Asset content.
92 */
93 public function getContent() {
94 return $this->content;
95 }
96
97 /**
98 * Getter for header template.
99 *
100 * @return string
101 * Asset header template.
102 */
103 public function getHeaderTemplate() {
104 return $this->headerTemplate;
105 }
106
107 /**
108 * Getter for template.
109 *
110 * @return string
111 * Asset template.
112 */
113 public function getTemplate() {
114 return $this->template;
115 }
116
117 /**
118 * Getter for asset vars.
119 *
120 * @return array
121 * Asset template variables.
122 */
123 public function getVars() {
124 return $this->vars;
125 }
126
127 /**
128 * Getter for asset action.
129 *
130 * @return string
131 * Asset action.
132 */
133 public function getAction() {
134 return $this->action;
135 }
136
137 /**
138 * Getter for asset header size.
139 *
140 * @return string
141 * Asset header size.
142 */
143 public function getHeaderSize() {
144 return $this->headerSize;
145 }
146
147 /**
148 * Getter for asset mode.
149 *
150 * @return string
151 * Asset file mode.
152 */
153 public function getMode() {
154 return $this->mode ?: ($this->isDirectory() ? 0755 : 0644);
155 }
156
157 /**
158 * Getter for asset type.
159 *
160 * @return string
161 * Asset type.
162 */
163 public function getType() {
164 return $this->type;
165 }
166
167 /**
168 * Setter for asset path.
169 *
170 * @param string $path
171 * Asset path.
172 *
173 * @return \DrupalCodeGenerator\Asset
174 * The asset.
175 */
176 public function path($path) {
177 $this->path = $path;
178 return $this;
179 }
180
181 /**
182 * Setter for asset content.
183 *
184 * @param string $content
185 * Asset content.
186 *
187 * @return \DrupalCodeGenerator\Asset
188 * The asset.
189 */
190 public function content($content) {
191 $this->content = $content;
192 return $this;
193 }
194
195 /**
196 * Setter for asset header template.
197 *
198 * @param string $header_template
199 * Asset template.
200 *
201 * @return \DrupalCodeGenerator\Asset
202 * The asset.
203 */
204 public function headerTemplate($header_template) {
205 $this->headerTemplate = $header_template;
206 return $this;
207 }
208
209 /**
210 * Setter for asset template.
211 *
212 * @param string $template
213 * Asset template.
214 *
215 * @return \DrupalCodeGenerator\Asset
216 * The asset.
217 */
218 public function template($template) {
219 $this->template = $template;
220 return $this;
221 }
222
223 /**
224 * Setter for asset vars.
225 *
226 * @param array $vars
227 * Asset template variables.
228 *
229 * @return \DrupalCodeGenerator\Asset
230 * The asset.
231 */
232 public function vars(array $vars) {
233 $this->vars = $vars;
234 return $this;
235 }
236
237 /**
238 * Setter for asset action.
239 *
240 * @param string $action
241 * Asset action.
242 *
243 * @return \DrupalCodeGenerator\Asset
244 * The asset.
245 */
246 public function action($action) {
247 $this->action = $action;
248 return $this;
249 }
250
251 /**
252 * Setter for asset header size.
253 *
254 * @param int $header_size
255 * Asset header size.
256 *
257 * @return \DrupalCodeGenerator\Asset
258 * The asset.
259 */
260 public function headerSize($header_size) {
261 $this->headerSize = $header_size;
262 return $this;
263 }
264
265 /**
266 * Setter for asset mode.
267 *
268 * @param string $mode
269 * Asset mode.
270 *
271 * @return \DrupalCodeGenerator\Asset
272 * The asset.
273 */
274 public function mode($mode) {
275 $this->mode = $mode;
276 return $this;
277 }
278
279 /**
280 * Setter for asset type.
281 *
282 * @param string $type
283 * Asset type.
284 *
285 * @return \DrupalCodeGenerator\Asset
286 * The asset.
287 */
288 public function type($type) {
289 $this->type = $type;
290 return $this;
291 }
292
293 /**
294 * Determines if the asset is a directory.
295 *
296 * @return bool
297 * True if the asset is a directory, false otherwise.
298 */
299 public function isDirectory() {
300 return $this->getType() == 'directory';
301 }
302
303 /**
304 * Renders the asset template.
305 *
306 * @param \DrupalCodeGenerator\Helper\Renderer $renderer
307 * Renderer helper.
308 */
309 public function render(Renderer $renderer) {
310 if (!$this->isDirectory() && is_null($this->getContent())) {
311 $content = '';
312 if ($header_template = $this->getHeaderTemplate()) {
313 $content .= $renderer->render($header_template, $this->getVars()) . "\n";
314 }
315 $content .= $renderer->render($this->getTemplate(), $this->getVars());
316 $this->content($content);
317 }
318 }
319
320 }