CRM_Core_CodeGen_Util_Smarty - Only generate one instance of Smarty (with one temp...
authorTim Otten <totten@civicrm.org>
Sat, 30 Aug 2014 03:27:24 +0000 (20:27 -0700)
committerTim Otten <totten@civicrm.org>
Sat, 30 Aug 2014 03:30:59 +0000 (20:30 -0700)
CRM/Core/CodeGen/Main.php
CRM/Core/CodeGen/Util/Smarty.php [new file with mode: 0644]
CRM/Core/CodeGen/Util/Template.php

index 924713f73338964235386c0568cd93bf04b33ade..373073020930b088e5c819aee81fbd99baea5a3d 100644 (file)
@@ -47,7 +47,7 @@ class CRM_Core_CodeGen_Main {
     // default cms is 'drupal', if not specified
     $this->cms = isset($argCms) ? strtolower($argCms) : 'drupal';
 
-    CRM_Core_CodeGen_Util_Template::$smartyPluginDirs = $smartyPluginDirs;
+    CRM_Core_CodeGen_Util_Smarty::singleton()->setPluginDirs($smartyPluginDirs);
 
     $versionFile        = "version.xml";
     $versionXML         = CRM_Core_CodeGen_Util_Xml::parse($versionFile);
diff --git a/CRM/Core/CodeGen/Util/Smarty.php b/CRM/Core/CodeGen/Util/Smarty.php
new file mode 100644 (file)
index 0000000..587f125
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+class CRM_Core_CodeGen_Util_Smarty {
+  /**
+   * @var CRM_Core_CodeGen_Util_Smarty
+   */
+  private static $singleton;
+
+  /**
+   * @return CRM_Core_CodeGen_Util_Smarty
+   */
+  public static function singleton() {
+    if (self::$singleton === NULL) {
+      self::$singleton = new CRM_Core_CodeGen_Util_Smarty();
+    }
+    return self::$singleton;
+  }
+
+  private $smartyPluginDirs = array();
+
+  /**
+   * @var Smarty
+   */
+  private $smarty;
+
+  private $compileDir;
+
+  function __destruct() {
+    if ($this->compileDir) {
+      CRM_Core_CodeGen_Util_File::cleanTempDir($this->compileDir);
+    }
+  }
+
+  function setPluginDirs($pluginDirs) {
+    $this->smartyPluginDirs = $pluginDirs;
+    $this->smarty = NULL;
+  }
+
+  function getCompileDir() {
+    if ($this->compileDir === NULL) {
+      $this->compileDir = CRM_Core_CodeGen_Util_File::createTempDir('templates_c_');
+    }
+    return $this->compileDir;
+  }
+
+  function getSmarty() {
+    if ($this->smarty === NULL) {
+      require_once 'Smarty/Smarty.class.php';
+      $this->smarty = new Smarty();
+      $this->smarty->template_dir = './templates';
+      $this->smarty->plugins_dir = $this->smartyPluginDirs;
+      $this->smarty->compile_dir = $this->getCompileDir();
+      $this->smarty->clear_all_cache();
+    }
+    return $this->smarty;
+  }
+}
\ No newline at end of file
index 3d14b8896f3c3b3eea0e41ca4e3367ed3ccddd45..05603d6bd5dd5870144bbf3c4b3d82532e69736a 100644 (file)
@@ -9,24 +9,13 @@ class CRM_Core_CodeGen_Util_Template {
   protected $smarty;
   protected $beautifier;
 
-  // FIXME: Set by Main...
-  static public $smartyPluginDirs = array();
-
   /**
    * @param string $filetype
    */
   function __construct($filetype) {
-    $this->compileDir = CRM_Core_CodeGen_Util_File::createTempDir('templates_c_');
-
     $this->filetype = $filetype;
 
-    // TODO use Core Smarty
-    require_once 'Smarty/Smarty.class.php';
-    $this->smarty = new Smarty();
-    $this->smarty->template_dir = './templates';
-    $this->smarty->plugins_dir = self::$smartyPluginDirs;
-    $this->smarty->compile_dir = $this->compileDir;
-    $this->smarty->clear_all_cache();
+    $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->getSmarty();
 
     $this->assign('generated', "DO NOT EDIT.  Generated by CRM_Core_CodeGen");
 
@@ -49,10 +38,6 @@ class CRM_Core_CodeGen_Util_Template {
     }
   }
 
-  function __destruct() {
-    CRM_Core_CodeGen_Util_File::cleanTempDir($this->compileDir);
-  }
-
   /**
    * @param array $inputs template filenames
    * @param string $outpath full path to the desired output file