4 * Class CRM_Core_CodeGen_Util_Template
6 class CRM_Core_CodeGen_Util_Template
{
10 protected $beautifier;
18 * @param string $filetype
19 * @param bool $beautify
21 public function __construct($filetype, $beautify = TRUE) {
22 $this->filetype
= $filetype;
24 $this->smarty
= CRM_Core_CodeGen_Util_Smarty
::singleton()->createSmarty();
26 $this->assign('generated', "DO NOT EDIT. Generated by CRM_Core_CodeGen");
28 if ($this->filetype
=== 'php' && $beautify) {
29 require_once 'PHP/Beautifier.php';
31 $this->beautifier
= new PHP_Beautifier();
32 $this->beautifier
->addFilter('ArrayNested');
33 // add one or more filters
34 $this->beautifier
->setIndentChar(' ');
35 $this->beautifier
->setIndentNumber(2);
36 $this->beautifier
->setNewLine("\n");
39 $this->beautify
= $beautify;
43 * @param array $inputs
45 * @param string $outpath
46 * Full path to the desired output file.
48 public function runConcat($inputs, $outpath) {
49 if (file_exists($outpath)) {
52 foreach ($inputs as $infile) {
53 // FIXME: does not beautify. Document.
54 file_put_contents($outpath, $this->smarty
->fetch($infile) . "\n", FILE_APPEND
);
59 * Run template generator.
61 * @param string $infile
62 * Filename of the template, without a path.
63 * @param string $outpath
64 * Full path to the desired output file.
66 public function run($infile, $outpath) {
67 $contents = $this->smarty
->fetch($infile);
69 if ($this->filetype
=== 'php' && $this->beautify
) {
70 $this->beautifier
->setInputString($contents);
71 $this->beautifier
->process();
72 $contents = $this->beautifier
->get();
74 // The beautifier isn't as beautiful as one would hope. Here's some extra string fudging.
77 "\n }\n}\n" => "\n }\n\n}\n",
78 '=> true,' => '=> TRUE,',
79 '=> false,' => '=> FALSE,',
80 'static ::' => 'static::'
82 $contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
83 $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents);
85 // Convert old array syntax to new square brackets
86 $contents = CRM_Core_CodeGen_Util_ArraySyntaxConverter
::convert($contents);
88 // Ensure file ends with a newline
89 if (substr($contents, -1) !== "\n") {
92 file_put_contents($outpath, $contents);
98 * @param string $infile
102 public function fetch($infile) {
103 return $this->smarty
->fetch($infile);
110 public function assign($key, $value) {
111 $this->smarty
->assign_by_ref($key, $value);