Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[civicrm-core.git] / CRM / Core / CodeGen / Util / Template.php
1 <?php
2
3 /**
4 * Class CRM_Core_CodeGen_Util_Template
5 */
6 class CRM_Core_CodeGen_Util_Template {
7 protected $filetype;
8
9 protected $smarty;
10 protected $beautifier;
11
12 /**
13 * @param string $filetype
14 */
15 public function __construct($filetype) {
16 $this->filetype = $filetype;
17
18 $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->getSmarty();
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
41 /**
42 * @param array $inputs template filenames
43 * @param string $outpath full path to the desired output file
44 */
45 public function runConcat($inputs, $outpath) {
46 if (file_exists($outpath)) {
47 unlink($outpath);
48 }
49 foreach ($inputs as $infile) {
50 // FIXME: does not beautify. Document.
51 file_put_contents($outpath, $this->smarty->fetch($infile) ."\n", FILE_APPEND);
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 */
59 public function run($infile, $outpath) {
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
72 /**
73 * @param $key
74 * @param $value
75 */
76 public function assign($key, $value) {
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 }