1: <?php
2: /**
3: *
4: * @package XCube
5: * @version $Id: XCube_Root.class.php,v 1.10 2008/11/20 16:05:57 minahito Exp $
6: * @copyright Copyright 2005-2007 XOOPS Cube Project <https://github.com/xoopscube/legacy>
7: * @license https://github.com/xoopscube/legacy/blob/master/docs/bsd_licenses.txt Modified BSD license
8: *
9: */
10:
11: if (!defined('XCUBE_CORE_PATH')) define('XCUBE_CORE_PATH', dirname(__FILE__));
12:
13: require_once XCUBE_CORE_PATH . '/XCube_HttpContext.class.php';
14:
15: if (version_compare(PHP_VERSION, "5.0", ">=")) {
16: function XC_CLASS_EXISTS($className)
17: {
18: return class_exists($className, false);
19: }
20: } else {
21: function XC_CLASS_EXISTS($className)
22: {
23: return class_exists($className);
24: }
25: }
26:
27: /**
28: * @public
29: * @brief [FINAL CLASS] The root object which collects exchangable managers.
30: *
31: * This class offers the access course same as global variable for a logic in old mechanism.
32: * This class does not let you depend on a main controller class name
33: * You must not succeed to this class.
34: */
35: class XCube_Root
36: {
37: /**
38: * @public
39: * @brief [READ ONLY] XCube_Controller
40: */
41: var $mController = null;
42:
43: /**
44: * @public
45: * @brief [READ ONLY] XCube_LanguageManager
46: */
47: var $mLanguageManager = null;
48:
49: /**
50: * @public
51: * @brief [READ ONLY] XCube_DelegateManager
52: */
53: var $mDelegateManager = null;
54:
55: /**
56: * @public
57: * @brief [READ ONLY] XCube_ServiceManager
58: */
59: var $mServiceManager = null;
60:
61: /**
62: * @private
63: * @brief Hash-Map Array - std::map<string, XCube_RenderSystem*> - Caches for genereted render-systems.
64: * @attention
65: * Only the kernel system should access this member property.
66: */
67: var $_mRenderSystems = array();
68:
69: /**
70: * @public
71: * @brief [READ ONLY] Hash-Map Array - std::map<string, string>
72: */
73: var $mSiteConfig = array();
74:
75: /**
76: * @internal
77: * @access public
78: * @var XCube_AbstractPermissionProvider
79: */
80: var $mPermissionManager = null;
81:
82: /**
83: * @public
84: * @brief [READ ONLY] XCube_RoleManager
85: * @todo Let's implements!
86: */
87: var $mRoleManager = null;
88:
89: /**
90: * @internal
91: * @deprecated
92: * @todo Check! This is deprecated member.
93: */
94: var $mCacheSystem = null;
95:
96: /**
97: * @public
98: * @brief [READ ONLY] XCube_TextFilter
99: * @attention
100: * In some cases, this member is not initialized. Use getTextFilter().
101: *
102: * @see getTextFilter()
103: */
104: var $mTextFilter = null;
105:
106: /**
107: * @public
108: * @brief [READ ONLY] XCube_HttpContext
109: */
110: var $mContext = null;
111:
112: /**
113: * @public
114: * @brief [READ ONLY] XCube_Session
115: */
116: var $mSession = null;
117:
118: /**
119: * @internal
120: */
121: function XCube_Root()
122: {
123: }
124:
125: /**
126: * @public
127: * @brief [Static] Gets a object of XCube_Root as singleton.
128: * @return XCube_Root
129: */
130: static function &getSingleton()
131: {
132: static $instance;
133:
134: if (!isset($instance))
135: $instance = new XCube_Root();
136:
137: return $instance;
138: }
139:
140: /**
141: * @public
142: * @berif [Secret Agreement][Overload] Loads SiteConfig from plural files, and control set and override site config.
143: * @return void
144: *
145: * \par $root->loadSiteConfig(string $file1);
146: * Loads the site settings from file1.
147: *
148: * \par $root->loadSiteConfig(string $file1, string $file2);
149: * Loads the site setting from file1. After that, override file1's setting with file2's setting.
150: *
151: * @attention
152: * Only a base module's boot strap should call this method.
153: */
154: function loadSiteConfig()
155: {
156: $n = func_num_args();
157: if ($n == 0) {
158: die("FETAL: open error: site setting config.");
159: }
160:
161: $files = func_get_args();
162: $file = array_shift($files);
163:
164: if(!file_exists($file)) {
165: die("FETAL: open error: site setting config.");
166: }
167:
168: $this->setSiteConfig(parse_ini_file($file, true));
169:
170: //
171: // Override setting.
172: //
173: if ($n > 1) {
174: foreach ($files as $overrideFile) {
175: if (file_exists($overrideFile)) {
176: $this->overrideSiteConfig(parse_ini_file($overrideFile, true));
177: }
178: }
179: }
180: }
181:
182: /**
183: * @internal
184: * @public
185: * @brief Sets site configs.
186: * @param $config Array
187: * @return void
188: */
189: function setSiteConfig($config)
190: {
191: $this->mSiteConfig = $config;
192: }
193:
194: /**
195: * @public
196: * @brief [Secret Agreement] Overwrites the current site configs with $config.
197: *
198: * Override site config. SiteConfig is overridden by $config value. And, if
199: * $config has new key, that key is set.
200: *
201: * @attention
202: * Only the header of the current base module should call this method.
203: *
204: * @param array $config
205: */
206: function overrideSiteConfig($config)
207: {
208: foreach ($config as $_overKey=>$_overVal) {
209: if (array_key_exists($_overKey, $this->mSiteConfig)) {
210: $this->mSiteConfig[$_overKey] = array_merge($this->mSiteConfig[$_overKey], $_overVal);
211: }
212: else {
213: $this->mSiteConfig[$_overKey] = $_overVal;
214: }
215: }
216: }
217:
218: /**
219: * @public
220: * @brief [Overload] Gets a value of site config that is defined by .ini files.
221: * @return mixed - If the value specified by parameters is no, return null.
222: *
223: * \par $root->getSiteConfig();
224: * Gets array.
225: *
226: * \par $root->getSiteConfig(string $groupName);
227: * Gets array of the group specified by $groupName.
228: *
229: * \par $root->getSiteConfig(string $groupName, string $itemName);
230: * Gets a config value specified by $groupName & $itemName.
231: *
232: * \par $root->getSiteConfig(string $groupName, string $itemName, string $default);
233: * If the config value is NOT defined specified by $groupName & $itemName, gets $default.
234: */
235: function getSiteConfig()
236: {
237: //
238: // TODO Check keys with using 'isset'
239: //
240: $m = &$this->mSiteConfig;
241: $n = func_num_args();
242: if ($n == 0) return $m;
243: elseif ($n == 1) {
244: $a = func_get_arg(0);
245: if (isset($m[$a])) return $m[$a];
246: }
247: elseif ($n == 2) {
248: list($a, $b) = func_get_args();
249: if (isset($m[$a][$b])) return $m[$a][$b];
250: }
251: elseif ($n == 3) {
252: list($a, $b, $c) = func_get_args();
253: if (isset($m[$a][$b])) return $m[$a][$b];
254: else return $c; //return 3rd param as a default value;
255: }
256:
257: return null;
258: }
259:
260: /**
261: * @public
262: * @brief [Secret Agreement] Creates controller with the rule.
263: *
264: * Creates controller with the rule, and call member function prepare().
265: * The class of creating controller is defined in ini.php files.
266: *
267: * @attention
268: * Only the header of the current base module should call this method.
269: *
270: * @return void
271: */
272: function setupController()
273: {
274: //
275: // [NOTICE]
276: // We don't decide the style of SiteConfig.
277: //
278: $controllerName = $this->mSiteConfig['Cube']['Controller'];
279: $controller =& $this->mSiteConfig[$controllerName];
280: if(isset($controller['root'])) {
281: $this->mController =& $this->_createInstance($controller['class'], $controller['path'], $controller['root']);
282: }
283: else {
284: $this->mController =& $this->_createInstance($controller['class'], $controller['path']);
285: }
286: $this->mController->prepare($this);
287: }
288:
289: /**
290: * @public
291: * @public Gets a XCube_Controller object.
292: * @return XCube_Controller
293: */
294: function &getController()
295: {
296: return $this->mController;
297: }
298:
299: /**
300: * @public
301: * @brief Sets the XCube_LanguageManager object.
302: * @param $languageManager XCube_LanguageManager
303: * @return void
304: */
305: function setLanguageManager(&$languageManager)
306: {
307: $this->mLanguageManager =& $languageManager;
308: }
309:
310: /**
311: * @public
312: * @brief Gets a XCube_LanguageManager object.
313: * @return XCube_LanguageManager
314: */
315: function &getLanguageManager()
316: {
317: return $this->mLanguageManager;
318: }
319:
320: /**
321: * @public
322: * @brief Sets the XCube_DelegateManager object.
323: * @param $delegateManager XCube_DelegateManager
324: * @return void
325: */
326: function setDelegateManager(&$delegateManager)
327: {
328: $this->mDelegateManager =& $delegateManager;
329: }
330:
331: /**
332: * @public
333: * @brief Gets a XCube_DelegateManager object.
334: * @return XCube_DelegateManager
335: */
336: function &getDelegateManager()
337: {
338: return $this->mDelegateManager;
339: }
340:
341: /**
342: * @public
343: * @brief Sets the XCube_ServiceManager object.
344: * @param $serviceManager XCube_ServiceManager
345: * @return void
346: */
347: function setServiceManager(&$serviceManager)
348: {
349: $this->mServiceManager =& $serviceManager;
350: }
351:
352: /**
353: * @public
354: * @brief Gets a XCube_ServiceManager object.
355: * @return XCube_ServiceManager
356: */
357: function &getServiceManager()
358: {
359: return $this->mServiceManager;
360: }
361:
362: /**
363: * @public
364: * @brief Gets a RenderSystem object having specified name.
365: * @param $name string - the registed name of the render system.
366: * @return XCube_RenderSystem
367: *
368: * Return the instance of the render system by the name. If the render
369: * system specified by $name doesn't exist, raise fatal error. This member
370: * function does creating the instance and calling prepare().
371: *
372: */
373: function &getRenderSystem($name)
374: {
375: $mRS =& $this->_mRenderSystems;
376: if (isset($mRS[$name])) {
377: return $mRS[$name];
378: }
379:
380: //
381: // create
382: //
383: $config =& $this->mSiteConfig;
384: $chunkName = $config['RenderSystems'][$name];
385: $chunk =& $config[$chunkName];
386: if (isset($config[$chunkName]['root'])) {
387: $mRS[$name] =& $this->_createInstance($chunk['class'], $chunk['path'], $chunk['root']);
388: }
389: else {
390: $mRS[$name] =& $this->_createInstance($chunk['class'], $chunk['path']);
391: }
392:
393: if (!is_object($mRS[$name])) {
394: die("NO");
395: }
396:
397: $mRS[$name]->prepare($this->mController);
398:
399: return $mRS[$name];
400: }
401:
402: /**
403: * @internal
404: */
405: function setPermissionManager(&$manager)
406: {
407: $this->mPermissionManager =& $manager;
408: }
409:
410: /**
411: * @internal
412: */
413: function &getPermissionManager()
414: {
415: return $this->mPermissionManager;
416: }
417:
418: /**
419: * @public
420: * @brief Sets a XCube_TextFilter object.
421: * @param $textFilter XCube_TextFilter
422: * @return void
423: */
424: function setTextFilter(&$textFilter)
425: {
426: $this->mTextFilter =& $textFilter;
427: }
428:
429: /**
430: * @public
431: * @brief Gets a XCube_TextFilter object.
432: * @return XCube_TextFilter
433: * @attention
434: * If mTextFilter member has been not initialized, the root object tries to
435: * generate an instance though XCube_Controller's delegate. This is a special
436: * case. Basically, a class never calls degates of other classes directly.
437: */
438: function &getTextFilter()
439: {
440: if (!empty($this->mTextFilter)) return $this->mTextFilter;
441: if (!empty($this->mController)) { //ToDo: This case is for _LEGACY_PREVENT_EXEC_COMMON_ status;
442: $this->mController->mSetupTextFilter->call(new XCube_Ref($this->mTextFilter));
443: return $this->mTextFilter;
444: }
445:
446: // Exception
447: $ret = null;
448: return $ret;
449: }
450:
451: /**
452: * @public
453: * @brief Sets the role manager object.
454: * @param $manager XCube_RoleManager
455: * @return void
456: */
457: function setRoleManager(&$manager)
458: {
459: $this->mRoleManager =& $manager;
460: }
461:
462: /**
463: * @public
464: * @brief Sets the HTTP-context object.
465: * @param $context XCube_Context
466: * @return void
467: */
468: function setContext(&$context)
469: {
470: $this->mContext =& $context;
471: }
472:
473: /**
474: * @public
475: * @brief Gets a HTTP-context object.
476: * @return XCube_Context
477: */
478: function &getContext()
479: {
480: return $this->mContext;
481: }
482:
483: /**
484: * @public
485: * @brief Sets a Session object.
486: * @param $session XCube_Session
487: * @return void
488: */
489: function setSession(&$session)
490: {
491: $this->mSession =& $session;
492: }
493:
494: /**
495: * @public
496: * @brief Gets a Session object.
497: * @return XCube_Session
498: */
499: function &getSession()
500: {
501: return $this->mSession;
502: }
503:
504: /**
505: * @private
506: * @brief Create an instance.
507: *
508: * Create the instance dynamic with the rule and the string parameters.
509: * First, load the file from $classPath. The rule is XOOPS_ROOT_PATH +
510: * $classPath + $className + .class.php. Next, create the instance of the
511: * class if the class is defined rightly. This member function is called by
512: * other member functions of XCube_Root.
513: *
514: * @param $className string - the name of class.
515: * @param $classPath string - the path that $className is defined in.
516: * @param $root string - the root path instead of Cube.Root.
517: * @return Object
518: *
519: * @todo If the file doesn't exist, require_once() raises fatal errors.
520: */
521: function &_createInstance($className, $classPath = null, $root = null)
522: {
523: $ret = null;
524:
525: if ($classPath != null) {
526: if ($root == null) {
527: $root = $this->mSiteConfig['Cube']['Root'];
528: }
529:
530: if (is_file($root . $classPath)) {
531: // [secret trick] ... Normally, $classPath has to point a directory.
532: require_once $root . $classPath;
533: }
534: else {
535: require_once $root . $classPath . '/' . $className . '.class.php';
536: }
537: }
538:
539: if (XC_CLASS_EXISTS($className)) {
540: $ret = new $className();
541: }
542:
543: return $ret;
544: }
545: }
546:
547: ?>
548: