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