Advertisement
fruffl

multiplexer

Mar 1st, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.75 KB | None | 0 0
  1. // thread
  2.  
  3.     NAMESPACE ILLI\Runtime;
  4.  
  5.     ABSTRACT CLASS Thread
  6.     {
  7.         private static $__instances = array();
  8.        
  9.         public static function getThread()
  10.         {
  11.             $thread = self::getThreadName();
  12.            
  13.             if(FALSE === array_key_exists($thread, self::$__instances))
  14.                 self::$__instances[$thread] = new $thread;
  15.            
  16.             return self::$__instances[$thread];
  17.         }
  18.        
  19.         public static function getThreadName()
  20.         {
  21.             return get_called_class();
  22.         }
  23.        
  24.         protected function __construct()
  25.         {
  26.         }
  27.        
  28.         protected function __clone()
  29.         {
  30.         }
  31.     }
  32.  
  33. // program
  34.  
  35.     NAMESPACE ILLI\Runtime;
  36.     USE \ILLI\Application       AS Application;
  37.     USE \ILLI\System\IO\Directory   AS Directory;
  38.     USE \ILLI\Runtime\Thread    AS Thread;
  39.    
  40.     require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Thread.php';
  41.     require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Loader.php';
  42.  
  43.     ABSTRACT CLASS Program EXTENDS Thread
  44.     {
  45.         private static $__autoloadFunction  = 'loadClass';
  46.         private static $__installDir        = NULL;
  47.         private static $__vendorDir     = NULL;
  48.         private static $__instances     = array();
  49.        
  50.         private $__threadName           = NULL;
  51.         private $__autoloader           = array();
  52.        
  53.         final public static function getThread()
  54.         {
  55.             $thread = self::getThreadName();
  56.            
  57.             if(TRUE === array_key_exists($thread, self::$__instances))
  58.                 return self::$__instances[$thread];
  59.            
  60.             self::$__instances[$thread] = parent::getThread();
  61.             self::$__instances[$thread]->__threadName = $thread;
  62.             self::$__instances[$thread]->__autoloader = array($thread, self::$__autoloadFunction);         
  63.             self::registerAutoloadFunction();
  64.            
  65.             Directory::registerIncludePath(self::getVendorDir());
  66.             Directory::registerIncludePath(self::getInstallDir());
  67.            
  68.             if(FALSE === (self::$__instances[$thread] instanceOf Application))
  69.                 throw new ProgramException(E::NO_INSTANCE_OF_APPLICATION, array(
  70.                     'thread'    => $thread,
  71.                     'class'     => __CLASS__,
  72.                     'required'  => __NAMESPACE__.'\Application'));
  73.                
  74.             return self::$__instances[$thread];
  75.         }
  76.        
  77.         final public static function loadClass($request)
  78.         {
  79.             $load = Loader::extract($request);
  80.             $file = self::getVendorDir().$load->getFilePath();
  81.            
  82.             if(FALSE === is_file($file))
  83.                 throw new ProgramException(E::CLASSFILE_NOT_FOUND, array(
  84.                     'thread'    => parent::getThreadName(),
  85.                     'class'     => $request,
  86.                     'path'      => $file));
  87.                
  88.             if(FALSE === is_readable($file))
  89.                 throw new ProgramException(E::CLASSFILE_NOT_ACCESSIBLE, array(
  90.                     'thread'    => parent::getThreadName(),
  91.                     'class'     => $request,
  92.                     'path'      => $file));
  93.                
  94.             require_once $file;
  95.         }
  96.        
  97.         final public static function getVendorDir()
  98.         {
  99.             return ((NULL !== self::$__vendorDir)
  100.                 ? self::$__vendorDir
  101.                 : (self::$__vendorDir = realpath(dirname(self::getInstallDir())).DIRECTORY_SEPARATOR));
  102.         }
  103.        
  104.         final public static function getInstallDir()
  105.         {
  106.             return ((NULL !== self::$__installDir)
  107.                 ? self::$__installDir
  108.                 : (self::$__installDir = realpath(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR));
  109.         }
  110.        
  111.         final public function __destruct()
  112.         {
  113.             self::unregisterAutoloadFunction();
  114.         }
  115.        
  116.         private static function registerAutoloadFunction()
  117.         {
  118.             spl_autoload_register(self::$__instances[self::getThreadName()]->__autoloader);
  119.         }
  120.        
  121.         private static function unregisterAutoloadFunction()
  122.         {
  123.             spl_autoload_unregister(self::$__instances[self::getThreadName()]->__autoloader);
  124.         }
  125.        
  126.         //abstract protected function getConfig();
  127.     }
  128.  
  129.  
  130. //index.php
  131. namespace localhost\illiFW\dev;
  132. require_once(ILLI_SYSTEM_ROOT.'Runtime/Program.php');
  133. require_once(ILLI_SYSTEM_ROOT.'Application.php');
  134.  
  135. use ILLI\Application as Application;
  136. use ILLI\Runtime\Program as Program;
  137. //class root  extends Program{} // exception
  138. class root extends Application{}
  139. class lucas extends Application{}
  140.  
  141. try
  142. {
  143.     $l = root::getThread();
  144.     $i = lucas::getThread();
  145.  
  146.     var_dump($l, $i);
  147.     var_dump($l->getThreadName(), $i->getThreadName());
  148.     var_dump(new \ILLI\Runtime\ProgramInfo($l));
  149.     var_dump(new \ILLI\Runtime\ProgramInfo($i));
  150.  
  151. }
  152. catch(\ILLI\Exception\Base $e)
  153. {
  154.     print $e->export()->asText();
  155. }
  156.  
  157.  
  158.  
  159. // result:
  160. object(localhost\illiFW\dev\root)#1 (2) {
  161.  ["__threadName":"ILLI\Runtime\Program":private]=>
  162.   string(25) "localhost\illiFW\dev\root"
  163.   ["__autoloader":"ILLI\Runtime\Program":private]=>
  164.   array(2) {
  165.     [0]=>
  166.     string(25) "localhost\illiFW\dev\root"
  167.     [1]=>
  168.     string(9) "loadClass"
  169.   }
  170. }
  171. object(localhost\illiFW\dev\lucas)#3 (2) {
  172.  ["__threadName":"ILLI\Runtime\Program":private]=>
  173.   string(26) "localhost\illiFW\dev\lucas"
  174.   ["__autoloader":"ILLI\Runtime\Program":private]=>
  175.   array(2) {
  176.     [0]=>
  177.     string(26) "localhost\illiFW\dev\lucas"
  178.     [1]=>
  179.     string(9) "loadClass"
  180.   }
  181. }
  182. string(25) "localhost\illiFW\dev\root"
  183. string(26) "localhost\illiFW\dev\lucas"
  184. object(ILLI\Runtime\ProgramInfo)#5 (5) {
  185.  ["__fileName":"ILLI\Runtime\ProgramInfo":private]=>
  186.   string(48) "/var/www/clients/client5/web22/web/dev/index.php"
  187.   ["__startLine":"ILLI\Runtime\ProgramInfo":private]=>
  188.   int(15)
  189.   ["__endLine":"ILLI\Runtime\ProgramInfo":private]=>
  190.   int(17)
  191.   ["__hash":"ILLI\Runtime\ProgramInfo":private]=>
  192.   string(32) "0000000025550f220000000018c79413"
  193.   ["__threadName":"ILLI\Runtime\ProgramInfo":private]=>
  194.   string(25) "localhost\illiFW\dev\root"
  195. }
  196. object(ILLI\Runtime\ProgramInfo)#5 (5) {
  197.  ["__fileName":"ILLI\Runtime\ProgramInfo":private]=>
  198.   string(48) "/var/www/clients/client5/web22/web/dev/index.php"
  199.   ["__startLine":"ILLI\Runtime\ProgramInfo":private]=>
  200.   int(18)
  201.   ["__endLine":"ILLI\Runtime\ProgramInfo":private]=>
  202.   int(18)
  203.   ["__hash":"ILLI\Runtime\ProgramInfo":private]=>
  204.   string(32) "0000000025550f200000000018c79413"
  205.   ["__threadName":"ILLI\Runtime\ProgramInfo":private]=>
  206.   string(26) "localhost\illiFW\dev\lucas"
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement