kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file BitSingleton.php

Documentation is available at BitSingleton.php

  1. <?php
  2. /**
  3.  * Base class for all objects where only one object should be created
  4.  *
  5.  * @version $Header$
  6.  *
  7.  *  Copyright (c) 2004 bitweaver.org
  8.  *  All Rights Reserved. See below for details and a complete list of authors.
  9.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  10.  *
  11.  *  Virtual base class (as much as one can have such things in PHP) for all
  12.  *  derived tikiwiki classes that require database access.
  13.  *
  14.  *  created 2004/8/15
  15.  *
  16.  * @author spider <spider@steelsun.com>
  17.  * @package  kernel
  18.  */
  19.  
  20. /**
  21.  * required setup
  22.  */
  23. require_onceKERNEL_PKG_PATH 'BitBase.php' );
  24.  
  25. /**
  26.  * @package  kernel
  27.  */
  28. abstract class BitSingleton extends BitBase implements BitCacheable {
  29.  
  30.     protected static $singletons null;
  31.  
  32.     function __construct({
  33.         parent::__construct();
  34.     }
  35.  
  36.     public static function loadSingleton$pVarName NULL {
  37.         $class static::getClass();
  38.         $globalVarName !empty$pVarName $pVarName 'g'.$class;
  39.         global $$globalVarName;
  40.  
  41.         if!($$globalVarName static::loadFromCache'Singleton' )) ) {
  42.             $$globalVarName new $class;
  43.         }
  44.         if(!isset(static::$singletons[$globalVarName])) {
  45.             static::$singletons[$globalVarName= $$globalVarName;
  46.             global $gBitSmarty;
  47.             $gBitSmarty->assign_by_ref$globalVarName$$globalVarName );
  48.         }
  49.         return static::$singletons[$globalVarName];
  50.     }
  51.  
  52.     public function getCacheKey({
  53.         return 'Singleton';
  54.     }
  55.  
  56.     public static function isCacheableClass({
  57.         return true;
  58.     }
  59.  
  60.     public function isCacheableObject({
  61.         return true;
  62.     }
  63.  
  64. }
  65.  
  66. // I don't remember where I found this, but this is to allow php < 5.3 to use this method.
  67. if (!function_exists('get_called_class')) {
  68.     function get_called_class($bt false$l 1{
  69.         if (!$bt)
  70.             $bt debug_backtrace();
  71.         if (!isset($bt[$l]))
  72.             throw new Exception("Cannot find called class -> stack level too deep.");
  73.         if (!isset($bt[$l]['type'])) {
  74.             throw new Exception('type not set');
  75.         }
  76.         else
  77.             switch ($bt[$l]['type']{
  78.                 case '::':
  79.                     $lines file($bt[$l]['file']);
  80.                     $i 0;
  81.                     $callerLine '';
  82.                     do {
  83.                         $i++;
  84.                         $callerLine $lines[$bt[$l]['line'$i$callerLine;
  85.                     while (stripos($callerLine$bt[$l]['function']=== false);
  86.                     preg_match('/([a-zA-Z0-9\_]+)::' $bt[$l]['function''/'$callerLine$matches);
  87.                     if (!isset($matches[1])) {
  88.                         // must be an edge case.
  89.                         throw new Exception("Could not find caller class: originating method call is obscured.");
  90.                     }
  91.                     switch ($matches[1]{
  92.                         case 'self':
  93.                         case 'parent':
  94.                             return get_called_class($bt$l 1);
  95.                         default:
  96.                             return $matches[1];
  97.                     }
  98.                 // won't get here.
  99.                 case '->'switch ($bt[$l]['function']{
  100.                         case '__get':
  101.                             // edge case -> get class of calling object
  102.                             if (!is_object($bt[$l]['object']))
  103.                                 throw new Exception("Edge case fail. __get called on non object.");
  104.                             return get_class($bt[$l]['object']);
  105.                         defaultreturn $bt[$l]['class'];
  106.                     }
  107.  
  108.                 default: throw new Exception("Unknown backtrace method type");
  109.             }
  110.     }
  111. }

Documentation generated on Wed, 29 Jul 2015 13:55:59 +0000 by phpDocumentor 1.5.0-lsces