Merge pull request #4851 from totten/master-createtest-bao
[civicrm-core.git] / CRM / Core / CodeGen / Util / Template.php
CommitLineData
5e434adf
ARW
1<?php
2
b5c2afd0
EM
3/**
4 * Class CRM_Core_CodeGen_Util_Template
5 */
5e434adf
ARW
6class 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 41 /**
6a0b768e
TO
42 * @param array $inputs
43 * Template filenames.
44 * @param string $outpath
45 * Full path to the desired output file.
5e434adf 46 */
00be9182 47 public function runConcat($inputs, $outpath) {
d9b759a7
TO
48 if (file_exists($outpath)) {
49 unlink($outpath);
50 }
5e434adf
ARW
51 foreach ($inputs as $infile) {
52 // FIXME: does not beautify. Document.
92fcb95f 53 file_put_contents($outpath, $this->smarty->fetch($infile) . "\n", FILE_APPEND);
5e434adf
ARW
54 }
55 }
56
57 /**
6a0b768e
TO
58 * @param string $infile
59 * Filename of the template, without a path.
60 * @param string $outpath
61 * Full path to the desired output file.
5e434adf 62 */
00be9182 63 public function run($infile, $outpath) {
5e434adf
ARW
64 $renderedContents = $this->smarty->fetch($infile);
65
66 if ($this->filetype === 'php') {
67 $this->beautifier->setInputString($renderedContents);
68 $this->beautifier->setOutputFile($outpath);
69 $this->beautifier->process();
70 $this->beautifier->save();
0db6c3e1
TO
71 }
72 else {
5e434adf
ARW
73 file_put_contents($outpath, $renderedContents);
74 }
75 }
76
2558c2b0
EM
77 /**
78 * @param $key
79 * @param $value
80 */
00be9182 81 public function assign($key, $value) {
5e434adf
ARW
82 $this->smarty->assign_by_ref($key, $value);
83 }
84
85 /**
86 * Clear the smarty cache and assign default values
87 * FIXME: unused cos we no longer do evil singleton magick
88 */
89 protected function reset() {
90 $this->smarty->clear_all_assign();
91 $this->smarty->clear_all_cache();
92 }
93}