Commit | Line | Data |
---|---|---|
5e434adf ARW |
1 | <?php |
2 | ||
b5c2afd0 EM |
3 | /** |
4 | * Class CRM_Core_CodeGen_Util_Template | |
5 | */ | |
5e434adf ARW |
6 | class CRM_Core_CodeGen_Util_Template { |
7 | protected $filetype; | |
8 | ||
9 | protected $smarty; | |
10 | protected $beautifier; | |
11 | ||
e61d1fa3 | 12 | /** |
8fe9ce40 | 13 | * @var bool |
e61d1fa3 | 14 | */ |
8fe9ce40 | 15 | protected $beautify; |
e61d1fa3 | 16 | |
5e434adf ARW |
17 | /** |
18 | * @param string $filetype | |
8fe9ce40 | 19 | * @param bool $beautify |
5e434adf | 20 | */ |
8fe9ce40 | 21 | public function __construct($filetype, $beautify = TRUE) { |
588af3f1 | 22 | $this->filetype = $filetype; |
23 | ||
b7a48b7e | 24 | $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->createSmarty(); |
5e434adf ARW |
25 | |
26 | $this->assign('generated', "DO NOT EDIT. Generated by CRM_Core_CodeGen"); | |
27 | ||
8fe9ce40 | 28 | if ($this->filetype === 'php' && $beautify) { |
5e434adf ARW |
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 | |
5e434adf ARW |
34 | $this->beautifier->setIndentChar(' '); |
35 | $this->beautifier->setIndentNumber(2); | |
36 | $this->beautifier->setNewLine("\n"); | |
37 | } | |
e61d1fa3 | 38 | |
8fe9ce40 | 39 | $this->beautify = $beautify; |
5e434adf ARW |
40 | } |
41 | ||
5e434adf | 42 | /** |
6a0b768e TO |
43 | * @param array $inputs |
44 | * Template filenames. | |
45 | * @param string $outpath | |
46 | * Full path to the desired output file. | |
5e434adf | 47 | */ |
00be9182 | 48 | public function runConcat($inputs, $outpath) { |
d9b759a7 TO |
49 | if (file_exists($outpath)) { |
50 | unlink($outpath); | |
51 | } | |
5e434adf ARW |
52 | foreach ($inputs as $infile) { |
53 | // FIXME: does not beautify. Document. | |
86bfa4f6 | 54 | file_put_contents($outpath, $this->smarty->fetch($infile) . "\n", FILE_APPEND); |
5e434adf ARW |
55 | } |
56 | } | |
57 | ||
58 | /** | |
8246bca4 | 59 | * Run template generator. |
60 | * | |
6a0b768e TO |
61 | * @param string $infile |
62 | * Filename of the template, without a path. | |
63 | * @param string $outpath | |
64 | * Full path to the desired output file. | |
5e434adf | 65 | */ |
00be9182 | 66 | public function run($infile, $outpath) { |
c3fc2621 | 67 | $contents = $this->smarty->fetch($infile); |
5e434adf | 68 | |
8fe9ce40 CW |
69 | if ($this->filetype === 'php' && $this->beautify) { |
70 | $this->beautifier->setInputString($contents); | |
71 | $this->beautifier->process(); | |
72 | $contents = $this->beautifier->get(); | |
e61d1fa3 | 73 | |
c3fc2621 | 74 | // The beautifier isn't as beautiful as one would hope. Here's some extra string fudging. |
8fe9ce40 CW |
75 | $replacements = [ |
76 | ') ,' => '),', | |
77 | "\n }\n}\n" => "\n }\n\n}\n", | |
78 | '=> true,' => '=> TRUE,', | |
79 | '=> false,' => '=> FALSE,', | |
1713a0ec | 80 | 'static ::' => 'static::', |
8fe9ce40 CW |
81 | ]; |
82 | $contents = str_replace(array_keys($replacements), array_values($replacements), $contents); | |
83 | $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents); | |
e61d1fa3 | 84 | |
c3fc2621 | 85 | // Convert old array syntax to new square brackets |
8fe9ce40 | 86 | $contents = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($contents); |
1713a0ec CW |
87 | // Remove numeric array keys (assuming its non-associative) |
88 | $contents = preg_replace("# '\\d+' => #", " ", $contents); | |
0db6c3e1 | 89 | } |
8fe9ce40 CW |
90 | // Ensure file ends with a newline |
91 | if (substr($contents, -1) !== "\n") { | |
92 | $contents .= "\n"; | |
5e434adf | 93 | } |
8fe9ce40 | 94 | file_put_contents($outpath, $contents); |
5e434adf ARW |
95 | } |
96 | ||
8246bca4 | 97 | /** |
98 | * Fetch via Smarty. | |
99 | * | |
100 | * @param string $infile | |
101 | * | |
102 | * @return string | |
103 | */ | |
90b9cb2c TO |
104 | public function fetch($infile) { |
105 | return $this->smarty->fetch($infile); | |
106 | } | |
107 | ||
2558c2b0 EM |
108 | /** |
109 | * @param $key | |
110 | * @param $value | |
111 | */ | |
00be9182 | 112 | public function assign($key, $value) { |
5e434adf ARW |
113 | $this->smarty->assign_by_ref($key, $value); |
114 | } | |
115 | ||
5e434adf | 116 | } |