Commit | Line | Data |
---|---|---|
5e434adf ARW |
1 | <?php |
2 | ||
b5c2afd0 EM |
3 | /** |
4 | * Class CRM_Core_CodeGen_Util_Template | |
5 | */ | |
5e434adf ARW |
6 | class CRM_Core_CodeGen_Util_Template { |
7 | protected $filetype; | |
8 | ||
9 | protected $smarty; | |
10 | protected $beautifier; | |
11 | ||
5e434adf ARW |
12 | /** |
13 | * @param string $filetype | |
14 | */ | |
00be9182 | 15 | public function __construct($filetype) { |
588af3f1 | 16 | $this->filetype = $filetype; |
17 | ||
3deba037 | 18 | $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->getSmarty(); |
5e434adf ARW |
19 | |
20 | $this->assign('generated', "DO NOT EDIT. Generated by CRM_Core_CodeGen"); | |
21 | ||
22 | // CRM-5308 / CRM-3507 - we need {localize} to work in the templates | |
23 | require_once 'CRM/Core/Smarty/plugins/block.localize.php'; | |
24 | $this->smarty->register_block('localize', 'smarty_block_localize'); | |
25 | ||
26 | if ($this->filetype === 'php') { | |
27 | require_once 'PHP/Beautifier.php'; | |
28 | // create an instance | |
29 | $this->beautifier = new PHP_Beautifier(); | |
30 | $this->beautifier->addFilter('ArrayNested'); | |
31 | // add one or more filters | |
32 | $this->beautifier->addFilter('Pear'); | |
33 | // add one or more filters | |
34 | $this->beautifier->addFilter('NewLines', array('after' => 'class, public, require, comment')); | |
35 | $this->beautifier->setIndentChar(' '); | |
36 | $this->beautifier->setIndentNumber(2); | |
37 | $this->beautifier->setNewLine("\n"); | |
38 | } | |
39 | } | |
40 | ||
5e434adf ARW |
41 | /** |
42 | * @param array $inputs template filenames | |
43 | * @param string $outpath full path to the desired output file | |
44 | */ | |
00be9182 | 45 | public function runConcat($inputs, $outpath) { |
d9b759a7 TO |
46 | if (file_exists($outpath)) { |
47 | unlink($outpath); | |
48 | } | |
5e434adf ARW |
49 | foreach ($inputs as $infile) { |
50 | // FIXME: does not beautify. Document. | |
45581edd | 51 | file_put_contents($outpath, $this->smarty->fetch($infile) ."\n", FILE_APPEND); |
5e434adf ARW |
52 | } |
53 | } | |
54 | ||
55 | /** | |
56 | * @param string $infile filename of the template, without a path | |
57 | * @param string $outpath full path to the desired output file | |
58 | */ | |
00be9182 | 59 | public function run($infile, $outpath) { |
5e434adf ARW |
60 | $renderedContents = $this->smarty->fetch($infile); |
61 | ||
62 | if ($this->filetype === 'php') { | |
63 | $this->beautifier->setInputString($renderedContents); | |
64 | $this->beautifier->setOutputFile($outpath); | |
65 | $this->beautifier->process(); | |
66 | $this->beautifier->save(); | |
67 | } else { | |
68 | file_put_contents($outpath, $renderedContents); | |
69 | } | |
70 | } | |
71 | ||
2558c2b0 EM |
72 | /** |
73 | * @param $key | |
74 | * @param $value | |
75 | */ | |
00be9182 | 76 | public function assign($key, $value) { |
5e434adf ARW |
77 | $this->smarty->assign_by_ref($key, $value); |
78 | } | |
79 | ||
80 | /** | |
81 | * Clear the smarty cache and assign default values | |
82 | * FIXME: unused cos we no longer do evil singleton magick | |
83 | */ | |
84 | protected function reset() { | |
85 | $this->smarty->clear_all_assign(); | |
86 | $this->smarty->clear_all_cache(); | |
87 | } | |
88 | } |