Advertisement
fruffl

Multiplexer Programm

Mar 2nd, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.40 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_Runtime
  6.      * @package    ILLI
  7.      * @subpackage Runtime
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\Runtime;
  13.  
  14.     /**
  15.      * ILLI Thread
  16.      *
  17.      * @category   ILLI_Runtime
  18.      * @package    ILLI
  19.      * @subpackage Runtime
  20.      * @namespace  ILLI\Runtime
  21.      * @link       http://illi.be
  22.      * @license    http://l.illi.be
  23.      * @copyright  ILLI Conference
  24.      * @since      2.0.0-1
  25.      * @version    2.0.0-1
  26.      */
  27.     ABSTRACT CLASS Thread
  28.     {
  29.         private static $__instances = [];
  30.        
  31.         public static function getThread()
  32.         {
  33.             $thread = self::getThreadName();
  34.            
  35.             if(FALSE === array_key_exists($thread, self::$__instances))
  36.                 self::$__instances[$thread] = new $thread;
  37.            
  38.             return self::$__instances[$thread];
  39.         }
  40.        
  41.         public static function getThreadName()
  42.         {
  43.             return get_called_class();
  44.         }
  45.        
  46.         protected function __construct()
  47.         {
  48.         }
  49.        
  50.         protected function __clone()
  51.         {
  52.         }
  53.     }
  54. <?PHP
  55.     /**
  56.      * ILLI
  57.      *
  58.      * @category   ILLI_Runtime
  59.      * @package    ILLI
  60.      * @subpackage Runtime
  61.      * @link       http://illi.be
  62.      * @license    http://l.illi.be
  63.      * @copyright  ILLI Conference
  64.      */
  65.     NAMESPACE ILLI\Runtime;
  66.  
  67.     /**
  68.      * ILLI Load Class
  69.      *
  70.      * Extract namespace and real-name of requested class and create the file-path
  71.      *
  72.      * Example: load class \ILLI\System\Collection\Test_Loader
  73.      *
  74.      *      Base NS: \ILLI
  75.      *      NS:      \System\Collection
  76.      *      Class    Test_Loader
  77.      *
  78.      *      File:   /ILLI/System.Collection/Test/Loader.php
  79.      *
  80.      * <code>
  81.      *  ILLI\Runtime\Load_Class Object
  82.      *  (
  83.      *      [__requestOrig  :ILLI\Runtime\Load_Class:private]   => ILLI\System\Collection\Test_Loader
  84.      *      [__request      :ILLI\Runtime\Load_Class:private]   => ILLI/System/Collection/Test_Loader
  85.      *      [__class        :ILLI\Runtime\Load_Class:private]   => Test_Loader
  86.      *      [__namespace    :ILLI\Runtime\Load_Class:private]   => ILLI\System\Collection
  87.      *      [__ns       :ILLI\Runtime\Load_Class:private]   => System\Collection
  88.      *      [__nsBase       :ILLI\Runtime\Load_Class:private]   => ILLI/
  89.      *      [__nsDir        :ILLI\Runtime\Load_Class:private]   => System.Collection/
  90.      *      [__nsPath       :ILLI\Runtime\Load_Class:private]   => ILLI/System.Collection/
  91.      *      [__classDir     :ILLI\Runtime\Load_Class:private]   => Test/Loader
  92.      *      [__file     :ILLI\Runtime\Load_Class:private]   => ILLI/System.Collection/Test/Loader
  93.      *      [__filePath     :ILLI\Runtime\Load_Class:private]   => ILLI/System.Collection/Test/Loader.php
  94.      *  )
  95.      * </code>
  96.      *
  97.      * @category   ILLI_Runtime
  98.      * @package    ILLI
  99.      * @subpackage Runtime
  100.      * @namespace  ILLI\Runtime
  101.      * @link       http://illi.be
  102.      * @license    http://l.illi.be
  103.      * @copyright  ILLI Conference
  104.      * @since      2.0.0-1
  105.      * @version    2.0.0-1
  106.      */
  107.      
  108.      CLASS Loader
  109.      {
  110.         private static $__cache     = [];      
  111.         private static $__NDS       = '.';
  112.         private static $__CDS       = DIRECTORY_SEPARATOR;
  113.        
  114.         private $__requestOrig      = NULL;
  115.         private $__request      = NULL;
  116.         private $__class        = NULL;
  117.         private $__namespace        = NULL;
  118.         private $__ns           = NULL;
  119.         private $__nsBase       = NULL;
  120.         private $__nsDir        = NULL;
  121.         private $__nsPath       = NULL;
  122.         private $__classDir     = NULL;
  123.         private $__file         = NULL;
  124.         private $__filePath     = NULL;
  125.        
  126.         public function __construct($request)
  127.         {
  128.             if(TRUE === array_key_exists($request, self::$__cache))
  129.                 return;
  130.            
  131.             $this->__requestOrig    = $request;
  132.             $this->__request    = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $this->__requestOrig);
  133.            
  134.             $_nA    =
  135.             $_nB    = explode(DIRECTORY_SEPARATOR, $this->__request);
  136.             $f  = 0;
  137.             $s  = sizeof($_nA);
  138.             $l  = $s - 1;
  139.            
  140.             $this->__nsBase = ($l === 0) ? NULL : $_nA[$f].DIRECTORY_SEPARATOR;
  141.             $this->__class  = $_nA[$l];
  142.                        
  143.             unset($_nA[$l], $_nB[$l], $_nB[$f], $request);
  144.            
  145.             $this->__namespace  = ($l === 0) ? NULL : implode('\\', $_nA);
  146.             $this->__ns     = ($l === 0) ? NULL : implode('\\', $_nB);
  147.             $this->__nsDir      = ($l <=  1) ? NULL : implode(self::$__NDS, $_nB).DIRECTORY_SEPARATOR;
  148.             $this->__nsPath     = ($l === 0) ? NULL : $this->__nsBase.$this->__nsDir;
  149.             $this->__classDir   = str_replace('_', self::$__CDS, $this->__class);
  150.             $this->__file       = $this->__nsBase.$this->__nsDir.$this->__classDir;
  151.             $this->__filePath   = $this->__file.'.php';
  152.             /*
  153.             $current="";           
  154.             foreach($namespaces as $namepart)
  155.             {
  156.                 $current.='\\' . $namepart;
  157.                
  158.                 if(in_array($current, self::$loadedNamespaces))
  159.                     continue;
  160.                
  161.                 $fnload = $current . DIRECTORY_SEPARATOR . "__init.php";
  162.                 if(file_exists($fnload))
  163.                     require($fnload);
  164.                
  165.                 self::$loadedNamespaces[] = $current;
  166.             }*/
  167.         }
  168.        
  169.         public static function extract($request)
  170.         {
  171.             return new self($request);
  172.         }
  173.        
  174.         public static function getCache()
  175.         {
  176.             return self::$__cache;
  177.         }
  178.        
  179.         public function getFilePath()
  180.         {
  181.             return $this->__filePath;
  182.         }
  183.        
  184.         public function getClassRealName()
  185.         {
  186.             return $this->__class;
  187.         }
  188.        
  189.         public function getClassPath()
  190.         {
  191.             return $this->__file;
  192.         }
  193.        
  194.         public function getClassDir()
  195.         {
  196.             return $this->__classDir;
  197.         }
  198.        
  199.         public function getNs()
  200.         {
  201.             return $this->__ns;
  202.         }
  203.        
  204.         public function getNsDir()
  205.         {
  206.             return $this->__nsDir;
  207.         }
  208.        
  209.         public function getNsPath()
  210.         {
  211.             return $this->__nsPath;
  212.         }
  213.        
  214.         public function getNsBase()
  215.         {
  216.             return $this->__nsBase;
  217.         }
  218.        
  219.         public function getRequestOriginal()
  220.         {
  221.             return $this->__requestOrig;
  222.         }
  223.        
  224.         public function getRequest()
  225.         {
  226.             return $this->__request;
  227.         }
  228.        
  229.         public function getNamespace()
  230.         {
  231.             return $this->__namespace;
  232.         }
  233.      }
  234.  
  235. <?PHP
  236.     /**
  237.      * ILLI
  238.      *
  239.      * @category   ILLI_Runtime
  240.      * @package    ILLI
  241.      * @subpackage Runtime
  242.      * @link       http://illi.be
  243.      * @license    http://l.illi.be
  244.      * @copyright  ILLI Conference
  245.      */
  246.     NAMESPACE ILLI\Runtime;
  247.     USE \ILLI\Application       AS Application;
  248.     USE \ILLI\System\IO\Directory   AS Directory;
  249.     USE \ILLI\Runtime\Thread    AS Thread;
  250.    
  251.     require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Thread.php';
  252.     require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'Loader.php';
  253.  
  254.     /**
  255.      * ILLI Initial Program
  256.      *
  257.      * Note:
  258.      * An instance of ILLI\Program must be extended by ILLI\Application.
  259.      *
  260.      * @category   ILLI_Runtime
  261.      * @package    ILLI
  262.      * @subpackage Runtime
  263.      * @namespace  ILLI\Runtime
  264.      * @link       http://illi.be
  265.      * @license    http://l.illi.be
  266.      * @copyright  ILLI Conference
  267.      * @since      2.0.0-1
  268.      * @version    2.0.0-1
  269.      * @abstract
  270.      */
  271.     ABSTRACT CLASS Program EXTENDS Thread
  272.     {
  273.         private static $__installDir    = NULL;
  274.         private static $__vendorDir = NULL;
  275.         private static $__instances = [];
  276.        
  277.         private $__threadName       = NULL;
  278.         private $__autoLoader       = [];
  279.        
  280.         final public static function getThread()
  281.         {
  282.             $thread = self::getThreadName();
  283.            
  284.             if(TRUE === array_key_exists($thread, self::$__instances))
  285.                 return self::$__instances[$thread];
  286.            
  287.             self::$__instances[$thread] = parent::getThread();
  288.             self::$__instances[$thread]->__threadName = $thread;
  289.             self::$__instances[$thread]->__autoLoader = [$thread, 'loadClass'];
  290.             self::registerAutoloadFunction();
  291.            
  292.             Directory::registerIncludePath(self::getVendorDir());
  293.             Directory::registerIncludePath(self::getInstallDir());
  294.            
  295.             if(FALSE === (self::$__instances[$thread] instanceOf Application))
  296.                 throw new ProgramException(self::$__instances[$thread], E::INSTANCE_OF_APPLICATION_EXPECTED,
  297.                     ['thread' => $thread, 'class' => __CLASS__, 'required' => __NAMESPACE__.'\Application']);
  298.                
  299.             return self::$__instances[$thread];
  300.         }
  301.        
  302.         final public static function loadClass($request)
  303.         {
  304.             $file = self::getVendorDir().(new Loader($request))->getFilePath();
  305.            
  306.             if(FALSE === is_file($file))
  307.                 throw new ProgramException(self::$__instances[self::getThreadName()], E::CLASSFILE_NOT_FOUND,
  308.                     ['thread' => parent::getThreadName(), 'request' => $request, 'file' => $file]);
  309.                
  310.             if(FALSE === is_readable($file))
  311.                 throw new ProgramException(self::$__instances[self::getThreadName()], E::CLASSFILE_NOT_ACCESSIBLE,
  312.                     ['thread' => parent::getThreadName(), 'request' => $request, 'file' => $file]);
  313.                
  314.             require_once $file;
  315.            
  316.             if(FALSE === class_exists($request, FALSE)
  317.             && FALSE === interface_exists($request, FALSE))
  318.                 throw new ProgramException(self::$__instances[self::getThreadName()], E::CLASS_OR_INTERFACE_EXPECTED,
  319.                     ['thread' => parent::getThreadName(), 'request' => $request, 'file' => $file]);
  320.         }
  321.        
  322.         final public static function getVendorDir()
  323.         {
  324.             return (NULL !== self::$__vendorDir)
  325.                 ? self::$__vendorDir
  326.                 : self::$__vendorDir = realpath(dirname(self::getInstallDir())).DIRECTORY_SEPARATOR;
  327.         }
  328.        
  329.         final public static function getInstallDir()
  330.         {
  331.             return (NULL !== self::$__installDir)
  332.                 ? self::$__installDir
  333.                 : self::$__installDir = realpath(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR;
  334.         }
  335.        
  336.         final public function __destruct()
  337.         {
  338.             self::unregisterAutoloadFunction();
  339.         }
  340.        
  341.         private static function registerAutoloadFunction()
  342.         {
  343.             spl_autoload_register(self::$__instances[self::getThreadName()]->__autoLoader);
  344.         }
  345.        
  346.         private static function unregisterAutoloadFunction()
  347.         {
  348.             spl_autoload_unregister(self::$__instances[self::getThreadName()]->__autoLoader);
  349.         }
  350.        
  351.         //abstract protected function getConfig();
  352.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement