Merge pull request #14587 from samuelsov/lab1058
[civicrm-core.git] / CRM / Core / CodeGen / Util / Smarty.php
1 <?php
2
3 /**
4 * Class CRM_Core_CodeGen_Util_Smarty
5 */
6 class CRM_Core_CodeGen_Util_Smarty {
7 /**
8 * @var CRM_Core_CodeGen_Util_Smarty
9 */
10 private static $singleton;
11
12 /**
13 * @return CRM_Core_CodeGen_Util_Smarty
14 */
15 public static function singleton() {
16 if (self::$singleton === NULL) {
17 self::$singleton = new CRM_Core_CodeGen_Util_Smarty();
18 }
19 return self::$singleton;
20 }
21
22 private $compileDir;
23
24 public function __destruct() {
25 if ($this->compileDir) {
26 CRM_Core_CodeGen_Util_File::cleanTempDir($this->compileDir);
27 }
28 }
29
30 /**
31 * Get templates_c directory.
32 *
33 * @return string
34 */
35 public function getCompileDir() {
36 if ($this->compileDir === NULL) {
37 $this->compileDir = CRM_Core_CodeGen_Util_File::createTempDir('templates_c_');
38 }
39 return $this->compileDir;
40 }
41
42 /**
43 * Create a Smarty instance.
44 *
45 * @return \Smarty
46 */
47 public function createSmarty() {
48 $base = dirname(dirname(dirname(dirname(__DIR__))));
49
50 require_once 'Smarty/Smarty.class.php';
51 $smarty = new Smarty();
52 $smarty->template_dir = "$base/xml/templates";
53 $smarty->plugins_dir = ["$base/packages/Smarty/plugins", "$base/CRM/Core/Smarty/plugins"];
54 $smarty->compile_dir = $this->getCompileDir();
55 $smarty->clear_all_cache();
56
57 // CRM-5308 / CRM-3507 - we need {localize} to work in the templates
58
59 require_once 'CRM/Core/Smarty/plugins/block.localize.php';
60 $smarty->register_block('localize', 'smarty_block_localize');
61
62 return $smarty;
63 }
64
65 }