Commit | Line | Data |
---|---|---|
4bcd4c62 TO |
1 | <?php |
2 | namespace Civi\Setup; | |
3 | ||
4 | class Template { | |
5 | ||
6 | protected $filetype; | |
7 | ||
8 | protected $smarty; | |
9 | protected $beautifier; | |
10 | ||
11 | public function __construct($srcPath, $fileType) { | |
12 | $this->filetype = $fileType; | |
13 | ||
14 | $this->smarty = \Civi\Setup\SmartyUtil::createSmarty($srcPath); | |
15 | ||
16 | $this->assign('generated', "DO NOT EDIT. Generated by Installer"); | |
17 | ||
18 | if ($this->filetype === 'php') { | |
19 | $packagePath = PackageUtil::getPath($srcPath); | |
20 | require_once implode(DIRECTORY_SEPARATOR, [$packagePath, 'PHP', 'Beautifier.php']); | |
21 | // create an instance | |
22 | $this->beautifier = new PHP_Beautifier(); | |
23 | $this->beautifier->addFilter('ArrayNested'); | |
24 | // add one or more filters | |
25 | $this->beautifier->setIndentChar(' '); | |
26 | $this->beautifier->setIndentNumber(2); | |
27 | $this->beautifier->setNewLine("\n"); | |
28 | } | |
29 | } | |
30 | ||
31 | public function assign($tpl_var, $value = NULL) { | |
32 | return $this->smarty->assign($tpl_var, $value); | |
33 | } | |
34 | ||
35 | /** | |
36 | * Run template generator. | |
37 | * | |
38 | * @param string $infile | |
39 | * Filename of the template, without a path. | |
40 | * @return string | |
41 | */ | |
42 | public function getContent($infile) { | |
43 | $contents = $this->smarty->fetch($infile); | |
44 | ||
45 | if ($this->filetype === 'php') { | |
46 | $this->beautifier->setInputString($contents); | |
47 | $this->beautifier->process(); | |
48 | $contents = $this->beautifier->get(); | |
49 | // The beautifier isn't as beautiful as one would hope. Here's some extra string fudging. | |
50 | $replacements = [ | |
51 | ') ,' => '),', | |
52 | "\n }\n}\n" => "\n }\n\n}\n", | |
53 | '=> true,' => '=> TRUE,', | |
54 | '=> false,' => '=> FALSE,', | |
55 | ]; | |
56 | $contents = str_replace(array_keys($replacements), array_values($replacements), $contents); | |
57 | $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents); | |
58 | // Convert old array syntax to new square brackets | |
59 | $contents = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($contents); | |
60 | } | |
61 | ||
62 | return $contents; | |
63 | } | |
64 | ||
65 | /** | |
66 | * @param array $inputs | |
67 | * Template filenames. | |
68 | */ | |
69 | public function getConcatContent($inputs) { | |
70 | $content = ''; | |
71 | foreach ($inputs as $infile) { | |
72 | // FIXME: does not beautify. Document. | |
73 | $content .= $this->smarty->fetch($infile) . "\n"; | |
74 | } | |
75 | ||
76 | return $content; | |
77 | } | |
78 | ||
79 | } |