4 * Class CRM_Core_CodeGen_Util_Template
6 class CRM_Core_CodeGen_Util_Template
{
10 protected $beautifier;
12 // FIXME: Set by Main...
13 static public $smartyPluginDirs = array();
16 * @param string $filetype
18 function __construct($filetype) {
19 $this->compileDir
= CRM_Core_CodeGen_Util_File
::createTempDir('templates_c_');
21 $this->filetype
= $filetype;
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();
31 $this->assign('generated', "DO NOT EDIT. Generated by CRM_Core_CodeGen");
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');
37 if ($this->filetype
=== 'php') {
38 require_once 'PHP/Beautifier.php';
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");
52 function __destruct() {
53 CRM_Core_CodeGen_Util_File
::cleanTempDir($this->compileDir
);
57 * @param array $inputs template filenames
58 * @param string $outpath full path to the desired output file
60 function runConcat($inputs, $outpath) {
61 if (file_exists($outpath)) {
64 foreach ($inputs as $infile) {
65 // FIXME: does not beautify. Document.
66 file_put_contents($outpath, $this->smarty
->fetch($infile) ."\n", FILE_APPEND
);
71 * @param string $infile filename of the template, without a path
72 * @param string $outpath full path to the desired output file
74 function run($infile, $outpath) {
75 $renderedContents = $this->smarty
->fetch($infile);
77 if ($this->filetype
=== 'php') {
78 $this->beautifier
->setInputString($renderedContents);
79 $this->beautifier
->setOutputFile($outpath);
80 $this->beautifier
->process();
81 $this->beautifier
->save();
83 file_put_contents($outpath, $renderedContents);
91 function assign($key, $value) {
92 $this->smarty
->assign_by_ref($key, $value);
96 * Clear the smarty cache and assign default values
97 * FIXME: unused cos we no longer do evil singleton magick
99 protected function reset() {
100 $this->smarty
->clear_all_assign();
101 $this->smarty
->clear_all_cache();