XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
XCube_Root.class.php
1<?php
16
17if ( ! defined( 'XCUBE_CORE_PATH' ) ) {
18 define( 'XCUBE_CORE_PATH', __DIR__ );
19}
20
21require_once XCUBE_CORE_PATH . '/XCube_HttpContext.class.php';
22
23if ( PHP_VERSION_ID >= 50000 ) {
24 function XC_CLASS_EXISTS( $className ) {
25 return $className && class_exists( $className, false );
26 }
27} else {
28 function XC_CLASS_EXISTS( $className ) {
29 return class_exists( $className );
30 }
31}
32
33class XCube_Root {
38 public $mController;
39
44 public $mLanguageManager;
45
50 public $mDelegateManager;
51
56 public $mServiceManager;
57
64 public $_mRenderSystems = [];
65
70 public $mSiteConfig = [];
71
77 public $mPermissionManager;
78
84 public $mRoleManager;
85
91 public $mCacheSystem;
92
101 public $mTextFilter;
102
107 public $mContext;
108
113 public $mSession;
114
118 public function __construct() {
119 }
120
126 public static function &getSingleton() {
127 static $instance;
128
129 if ( ! isset( $instance ) ) {
130 $instance = new XCube_Root();
131 }
132
133 return $instance;
134 }
135
150 public function loadSiteConfig() {
151 $n = func_num_args();
152 if ( 0 == $n ) {
153 die( 'FETAL: open error: site setting config.' );
154 }
155
156 $files = func_get_args();
157 $file = array_shift( $files );
158
159 if ( ! file_exists( $file ) ) {
160 die( 'FETAL: open error: site setting config.' );
161 }
162
163 $this->setSiteConfig( parse_ini_file( $file, true ) );
164
165 //
166 // Override setting.
167 //
168 if ( $n > 1 ) {
169 foreach ( $files as $overrideFile ) {
170 if ( file_exists( $overrideFile ) ) {
171 $this->overrideSiteConfig( parse_ini_file( $overrideFile, true ) );
172 }
173 }
174 }
175 }
176
187 public function setSiteConfig( $config ) {
188 $this->mSiteConfig = $config;
189 }
190
203 public function overrideSiteConfig( $config ) {
204 foreach ( $config as $_overKey => $_overVal ) {
205 if ( array_key_exists( $_overKey, $this->mSiteConfig ) ) {
206 $this->mSiteConfig[ $_overKey ] = array_merge( $this->mSiteConfig[ $_overKey ], $_overVal );
207 } else {
208 $this->mSiteConfig[ $_overKey ] = $_overVal;
209 }
210 }
211 }
212
230 public function getSiteConfig() {
231 //
232 // ! TODO Check keys with using 'isset'
233 //
234 $m = &$this->mSiteConfig;
235 $n = func_num_args();
236 if ( 0 === $n ) {
237 return $m;
238 }
239 if ( 1 === $n ) {
240 $a = func_get_arg( 0 );
241 if ( isset( $m[ $a ] ) ) {
242 return $m[ $a ];
243 }
244 } elseif ( 2 === $n ) {
245 [$a, $b] = func_get_args();
246 if ( isset( $m[ $a ][ $b ] ) ) {
247 return $m[ $a ][ $b ];
248 }
249 } elseif ( 3 === $n ) {
250 [$a, $b, $c] = func_get_args();
251 if ( isset( $m[ $a ][ $b ] ) ) {
252 return $m[ $a ][ $b ];
253 }
254
255 return $c; //return 3rd param as a default value;
256 }
257
258 return null;
259 }
260
273 public function setupController() {
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 } else {
283 $this->mController =& $this->_createInstance( $controller['class'], $controller['path'] );
284 }
285 $this->mController->prepare( $this );
286 }
287
293 public function &getController() {
294 return $this->mController;
295 }
296
305 public function setLanguageManager( &$languageManager ) {
306 $this->mLanguageManager =& $languageManager;
307 }
308
314 public function &getLanguageManager() {
315 return $this->mLanguageManager;
316 }
317
326 public function setDelegateManager( &$delegateManager ) {
327 $this->mDelegateManager =& $delegateManager;
328 }
329
335 public function &getDelegateManager() {
336 return $this->mDelegateManager;
337 }
338
347 public function setServiceManager( &$serviceManager ) {
348 $this->mServiceManager =& $serviceManager;
349 }
350
356 public function &getServiceManager() {
357 return $this->mServiceManager;
358 }
359
373 public function &getRenderSystem( $name ) {
374 $mRS =& $this->_mRenderSystems;
375 if ( isset( $mRS[ $name ] ) ) {
376 return $mRS[ $name ];
377 }
378
379 //
380 // create
381 //
382 $config =& $this->mSiteConfig;
383 $chunkName = $config['RenderSystems'][ $name ];
384 $chunk =& $config[ $chunkName ];
385 if ( isset( $config[ $chunkName ]['root'] ) ) {
386 $mRS[ $name ] =& $this->_createInstance( $chunk['class'], $chunk['path'], $chunk['root'] );
387 } else {
388 $mRS[ $name ] =& $this->_createInstance( $chunk['class'], $chunk['path'] );
389 }
390
391 if ( ! is_object( $mRS[ $name ] ) ) {
392 die( 'NO' );
393 }
394
395 $mRS[ $name ]->prepare( $this->mController );
396
397 return $mRS[ $name ];
398 }
399
405 public function setPermissionManager( &$manager ) {
406 $this->mPermissionManager =& $manager;
407 }
408
412 public function &getPermissionManager() {
413 return $this->mPermissionManager;
414 }
415
424 public function setTextFilter( &$textFilter ) {
425 $this->mTextFilter =& $textFilter;
426 }
427
437 public function &getTextFilter() {
438 if ( ! empty( $this->mTextFilter ) ) {
439 return $this->mTextFilter;
440 }
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
444 return $this->mTextFilter;
445 }
446
447 // Exception
448 $ret = null;
449
450 return $ret;
451 }
452
461 public function setRoleManager( &$manager ) {
462 $this->mRoleManager =& $manager;
463 }
464
474 public function setContext( &$context ) {
475 $this->mContext =& $context;
476 }
477
484 public function &getContext() {
485 return $this->mContext;
486 }
487
496 public function setSession( &$session ) {
497 $this->mSession =& $session;
498 }
499
505 public function &getSession() {
506 return $this->mSession;
507 }
508
527 public function &_createInstance( $className, $classPath = null, $root = null ) {
528 $ret = null;
529
530 if ( null !== $classPath ) {
531 if ( null === $root ) {
532 $root = $this->mSiteConfig['Cube']['Root'];
533 }
534
535 if ( is_file( $root . $classPath ) ) {
536 // [secret trick] ... Normally, $classPath has to point a directory.
537 require_once $root . $classPath;
538 } else {
539 require_once $root . $classPath . '/' . $className . '.class.php';
540 }
541 }
542
543 if ( XC_CLASS_EXISTS( $className ) ) {
544 $ret = new $className();
545 }
546
547 return $ret;
548 }
549}