1: <?php
2: /**
3: * This file contains the frontend class for smarty wrapper plugin.
4: *
5: * @package Plugin
6: * @subpackage SmartyWrapper
7: * @version SVN Revision $Rev:$
8: *
9: * @author Andreas Dieter
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Wrapper class for Integration of smarty.
19: *
20: * @package Plugin
21: * @subpackage SmartyWrapper
22: */
23: class cSmartyFrontend {
24:
25: /**
26: * The smarty Object
27: *
28: * @var Smarty
29: */
30: protected static $oSmarty;
31:
32: /**
33: * static flag to simulate singleton behaviour
34: *
35: * @var bool
36: */
37: public static $bSmartyInstanciated = false;
38:
39: /**
40: * static default paths
41: *
42: * @var array
43: */
44: protected static $aDefaultPaths = array();
45:
46: /**
47: * constructor
48: *
49: * @param array &$aCfg contenido cfg array
50: * @param array &$aClientCfg contenido client cfg array of the specific
51: * client
52: * @throws cException
53: * @throws cInvalidArgumentException if the given configurations are not an
54: * array
55: */
56: public function __construct(&$aCfg, &$aClientCfg, $bSanityCheck = false) {
57: // check if already instanciated
58: if (isset(self::$bSmartyInstanciated) && self::$bSmartyInstanciated) {
59: throw new cException("cSmartyFrontend class is intended to be used as singleton. Do not instanciate multiple times.");
60: }
61:
62: if (!is_array($aCfg)) {
63: throw new cInvalidArgumentException(__CLASS__ . " " . __FUNCTION__ . " Parameter 1 invalid.");
64: }
65:
66: if (!is_array($aClientCfg)) {
67: throw new cInvalidArgumentException(__CLASS__ . " " . __FUNCTION__ . " Parameter 2 invalid.");
68: }
69:
70: self::$oSmarty = new cSmartyWrapper();
71: self::$aDefaultPaths = array(
72: 'template_dir' => $aClientCfg['module']['path'],
73: 'cache_dir' => $aClientCfg['cache']['path'] . 'templates_c',
74: 'compile_dir' => $aClientCfg['cache']['path'] . 'templates_c'
75: );
76:
77: // check the template directory and create new one if it not exists
78: if (!is_dir(self::$aDefaultPaths['compile_dir'])) {
79: mkdir(self::$aDefaultPaths['compile_dir'], 0777);
80: }
81:
82: // check if folders exist and rights ok if needed
83: if ($bSanityCheck) {
84: foreach (self::$aDefaultPaths as $key => $value) {
85: if (!file_exists($value)) {
86: throw new cException(sprintf("Class %s Error: Folder %s does not exist. Please create.", __CLASS__, $value));
87: }
88: if ($key == 'cache' || $key == 'compile_dir') {
89: if (!is_writable($value)) {
90: throw new cException(sprintf("Class %s Error: Folder %s is not writable. Please check for sufficient rights.", __CLASS__, $value));
91: }
92: }
93: }
94: }
95:
96: self::resetPaths();
97: self::$bSmartyInstanciated = true;
98: }
99:
100: /**
101: * prevent users from cloning instance
102: *
103: * @throws cException if this function is called
104: */
105: public function __clone() {
106: throw new cException("cSmartyFrontend class is intended to be used as singleton. Do not clone.");
107: }
108:
109: /**
110: * destructor
111: * set cSmarty::bSmartyInstanciated to false
112: */
113: public function __destruct() {
114: self::$bSmartyInstanciated = false;
115: }
116:
117: /**
118: * static function to provide the smart object
119: *
120: * @param boolean $bResetTemplate true if the template values shall all be
121: * resetted
122: * @throws cException if singleton has not been instantiated yet
123: * @return cSmartyWrapper
124: */
125: public static function getInstance($bResetTemplate = false) {
126: if (!isset(self::$oSmarty)) {
127: // @TODO find a smart way to instanciate smarty object on demand
128: throw new cException("Smarty singleton not instantiated yet.");
129: }
130: if ($bResetTemplate) {
131: self::$oSmarty = new cSmartyWrapper();
132: self::resetPaths();
133: }
134: return self::$oSmarty;
135: }
136:
137: /**
138: * sets the default paths again
139: */
140: public static function resetPaths() {
141: self::$oSmarty->setTemplateDir(self::$aDefaultPaths['template_dir']);
142: self::$oSmarty->setCacheDir(self::$aDefaultPaths['cache_dir']);
143: self::$oSmarty->setCompileDir(self::$aDefaultPaths['compile_dir']);
144: }
145:
146: }
147:
148: ?>