From 3deba037e3dbc789e036871225021b170ae27047 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 29 Aug 2014 20:27:24 -0700 Subject: [PATCH] CRM_Core_CodeGen_Util_Smarty - Only generate one instance of Smarty (with one temp dir) --- CRM/Core/CodeGen/Main.php | 2 +- CRM/Core/CodeGen/Util/Smarty.php | 57 ++++++++++++++++++++++++++++++ CRM/Core/CodeGen/Util/Template.php | 17 +-------- 3 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 CRM/Core/CodeGen/Util/Smarty.php diff --git a/CRM/Core/CodeGen/Main.php b/CRM/Core/CodeGen/Main.php index 924713f733..3730730209 100644 --- a/CRM/Core/CodeGen/Main.php +++ b/CRM/Core/CodeGen/Main.php @@ -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 index 0000000000..587f1257c8 --- /dev/null +++ b/CRM/Core/CodeGen/Util/Smarty.php @@ -0,0 +1,57 @@ +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 diff --git a/CRM/Core/CodeGen/Util/Template.php b/CRM/Core/CodeGen/Util/Template.php index 3d14b8896f..05603d6bd5 100644 --- a/CRM/Core/CodeGen/Util/Template.php +++ b/CRM/Core/CodeGen/Util/Template.php @@ -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 -- 2.25.1