1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of TwigView. |
5
|
|
|
* |
6
|
|
|
** (c) 2014 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WyriHaximus\TwigView\Shell\Task; |
13
|
|
|
|
14
|
|
|
use Bake\Shell\Task\TemplateTask; |
15
|
|
|
use Cake\Console\Shell; |
16
|
|
|
use Cake\Utility\Inflector; |
17
|
|
|
|
18
|
|
|
final class TwigTemplateTask extends TemplateTask |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return string Task name. |
22
|
|
|
*/ |
23
|
|
|
public function name(): string |
24
|
|
|
{ |
25
|
|
|
return 'twig_template'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Assembles and writes bakes the twig view file. |
30
|
|
|
* |
31
|
|
|
* @param string $action Action to bake. |
32
|
|
|
* @param string $content Content to write. |
33
|
|
|
* @param string $outputFile The destination action name. If null, will fallback to $template. |
34
|
|
|
* @return string Generated file content. |
35
|
|
|
*/ |
36
|
|
|
public function bake($action, $content = '', $outputFile = null): string |
37
|
|
|
{ |
38
|
|
|
if ($outputFile === null) { |
39
|
|
|
$outputFile = $action; |
40
|
|
|
} |
41
|
|
|
if ($content === true) { |
42
|
|
|
$content = $this->getContent($action); |
43
|
|
|
} |
44
|
|
|
if (empty($content)) { |
45
|
|
|
$this->err("<warning>No generated content for '{$action}.ctp', not generating template.</warning>"); |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
$this->out("\n" . sprintf('Baking `%s` view twig template file...', $outputFile), 1, Shell::QUIET); |
50
|
|
|
$path = $this->getPath(); |
51
|
|
|
$filename = $path . Inflector::underscore($outputFile) . '.twig'; |
52
|
|
|
$this->createFile($filename, $content); |
53
|
|
|
|
54
|
|
|
return $content; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|