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