Merge pull request #19082 from compucorp/dev-core-1790-contact-card-delay
[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 * @var bool
14 */
15 protected $beautify;
16
17 /**
18 * @param string $filetype
19 * @param bool $beautify
20 */
21 public function __construct($filetype, $beautify = TRUE) {
22 $this->filetype = $filetype;
23
24 $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->createSmarty();
25
26 $this->assign('generated', "DO NOT EDIT. Generated by CRM_Core_CodeGen");
27
28 if ($this->filetype === 'php' && $beautify) {
29 require_once 'PHP/Beautifier.php';
30 // create an instance
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");
37 }
38
39 $this->beautify = $beautify;
40 }
41
42 /**
43 * @param array $inputs
44 * Template filenames.
45 * @param string $outpath
46 * Full path to the desired output file.
47 */
48 public function runConcat($inputs, $outpath) {
49 if (file_exists($outpath)) {
50 unlink($outpath);
51 }
52 foreach ($inputs as $infile) {
53 // FIXME: does not beautify. Document.
54 file_put_contents($outpath, $this->smarty->fetch($infile) . "\n", FILE_APPEND);
55 }
56 }
57
58 /**
59 * Run template generator.
60 *
61 * @param string $infile
62 * Filename of the template, without a path.
63 * @param string $outpath
64 * Full path to the desired output file.
65 */
66 public function run($infile, $outpath) {
67 $contents = $this->smarty->fetch($infile);
68
69 if ($this->filetype === 'php' && $this->beautify) {
70 $this->beautifier->setInputString($contents);
71 $this->beautifier->process();
72 $contents = $this->beautifier->get();
73
74 // The beautifier isn't as beautiful as one would hope. Here's some extra string fudging.
75 $replacements = [
76 ') ,' => '),',
77 "\n }\n}\n" => "\n }\n\n}\n",
78 '=> true,' => '=> TRUE,',
79 '=> false,' => '=> FALSE,',
80 'static ::' => 'static::',
81 'use\\' => 'use ',
82 ];
83 $contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
84 $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents);
85
86 // Convert old array syntax to new square brackets
87 $contents = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($contents);
88 // Remove numeric array keys (assuming its non-associative)
89 $contents = preg_replace("# '\\d+' => #", " ", $contents);
90 }
91 // Ensure file ends with a newline
92 if (substr($contents, -1) !== "\n") {
93 $contents .= "\n";
94 }
95 file_put_contents($outpath, $contents);
96 }
97
98 /**
99 * Fetch via Smarty.
100 *
101 * @param string $infile
102 *
103 * @return string
104 */
105 public function fetch($infile) {
106 return $this->smarty->fetch($infile);
107 }
108
109 /**
110 * Fetch multiple templates - and concatenate them.
111 *
112 * @see runConcat
113 * @param array $inputs
114 * Template filenames.
115 * @return string
116 */
117 public function fetchConcat($inputs) {
118 $buf = '';
119 foreach ($inputs as $infile) {
120 $buf .= $this->smarty->fetch($infile);
121 $buf .= "\n";
122 }
123 return $buf;
124 }
125
126 /**
127 * @param $key
128 * @param $value
129 */
130 public function assign($key, $value) {
131 $this->smarty->assign_by_ref($key, $value);
132 }
133
134 }