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