Advertisement
Guest User

Untitled

a guest
Oct 12th, 2011
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. <?php
  2. if (!defined('FaabBB'))
  3.     exit();
  4.  
  5. /**
  6.  * The {@link CoreAccessManager} allows non-core classes to communicate with
  7.  *  and invoke methods from core classes.
  8.  * Core classes that implements the {@link NonCoreAccessible} interface can be invoked
  9.  *  using this manager. This is a simple example of how it should look like core-side.
  10.  * <code>
  11.  * class CoreClass implements NonCoreAccessible {
  12.  *      public static $function_access = array(
  13.  *          'hello_world'
  14.  *      );
  15.  *      public static $constructor_access = true;
  16.  *     
  17.  *      public static $variable_access = array(
  18.  *          'hello_var'
  19.  *      );
  20.  *
  21.  *      public $hello_var = "YES"; // Can be accessed by non-core classes.
  22.  *      public $var = "NO"; // Can't be accessed by non-core classes.
  23.  *      
  24.  *      public function hello_world() {
  25.  *             return $this->var; // Valid
  26.  *      }
  27.  * }
  28.  * </code>
  29.  *
  30.  * Non-core-side:
  31.  *
  32.  * <code>
  33.  * // This class represents the core class.
  34.  * interface CoreClassAccess extends Object implements CoreAccess {
  35.  *       public $core_class = "CoreClass";
  36.  * }
  37.  * </code>
  38.  *
  39.  * <code>
  40.  * class Test extends Object {
  41.  *       function test() {
  42.  *          $i = CoreAccessLoader::load('CoreClassAccess');
  43.  *          $cls = new $i;
  44.  *          echo $cls->hello_world();
  45.  *       }
  46.  * }
  47.  * </code>
  48.  */
  49. class CoreAccessManager implements NonCoreAccessible {
  50.  
  51.     /**
  52.      * Name of property that contains all allowed variables.
  53.      */
  54.     const VARIABLE_ACCESS = "variable_access";
  55.  
  56.     /**
  57.      * Array that contains reflectors.
  58.      */
  59.     private static $reflectors = array();
  60.  
  61.     /**
  62.      * The class that requested a core class.
  63.      */
  64.     private $cls = null;
  65.  
  66.     /**
  67.      * Initializes a new {@link CoreAccessManager} instance.
  68.      *
  69.      * @param $cls The class that requested a core class.
  70.      */
  71.     public static function init($cls) {
  72.         if (!class_exists($cls))
  73.             throw new CoreException("Class " . $cls . " doesn't exists, but requested a" .
  74.                 " core class.");
  75.         return new CoreAccessManager($cls);
  76.     }
  77.  
  78.     /**
  79.      * Constructs a new {@link CoreAccessManager}.
  80.      *
  81.      * @param $cls The class that requested a core class.
  82.      */
  83.     private function __construct($cls) {
  84.         $this->cls = $cls;
  85.     }
  86.  
  87.     /**
  88.      * Access a variable from a core class.
  89.      *
  90.      * @param $var_name The variable name.
  91.      * @param $class_name The name of the class.
  92.      * @return the core class.
  93.      */
  94.     public function accessVar($var_name, $class_name) {
  95.         $accessible = $this->getAccessibleVariables($class_name);
  96.  
  97.         if (!in_array($accessible, $var_name))
  98.             throw new CoreException($this->cls . " tries to access variable " . $var_name
  99.                 . " that can't be accessed.");
  100.         $reflector = $this->getReflector($class_name);
  101.  
  102.         if ($reflector->hasProperty($var_name)) {
  103.             throw new CoreException($this->cls . " tries to access variable " . $var_name
  104.                 . " that doesn't exists.");
  105.         }
  106.  
  107.         $var = $reflector->getProperty($var_name);
  108.  
  109.         if (!$var->isStatic() || !$var->isPublic()) {
  110.             throw new CoreException($this->cls . " tries to access variable " . $var_name
  111.                 . " that can't be accessed.");
  112.         }
  113.  
  114.         return $var->getValue();
  115.     }
  116.  
  117.     /**
  118.      * Returns a list with accessible variables of this core class.
  119.      *
  120.      * @param $class_name The name of the class.
  121.      * @return a list with accessible variables.
  122.      */
  123.     public function getAccessibleVariables($class_name) {
  124.         $reflector = $this->getReflector($class_name);
  125.  
  126.         if (!$reflector->hasProperty(self::VARIABLE_ACCESS))
  127.             return array();
  128.         $property = $reflector->getProperty(self::VARIABLE_ACCESS);
  129.         if (!$property->isStatic()) {
  130.             CoreLogger::warning("Class " . $this->cls . " tries to access a variable " .
  131.                 "from a core class, but the array with accessible variable names isn't static.");
  132.             return array();
  133.         }
  134.         if (!$property->isPublic()) {
  135.             CoreLogger::warning("Class " . $this->cls . " tries to access a variable " .
  136.                 "from a core class, but the array with accessible variable names isn't public.");
  137.             return array();
  138.         }
  139.         $val = $property->getValue();
  140.         if (!is_array($val)) {
  141.             CoreLogger::warning("Class " . $this->cls . " tries to access a variable " .
  142.                 "from a core class, but the accessible variable names variable isn't an array.");
  143.             return array();
  144.         }
  145.         return $val;
  146.     }
  147.  
  148.  
  149.     /**
  150.      * Creates a new reflector instance for the given class
  151.      *  or return the exist one.
  152.      *
  153.      * @param $class_name The name of the class.
  154.      * @return the reflector of this class.
  155.      */
  156.     private function getReflector($class_name) {
  157.         // Does the class exists?
  158.         if (!class_exists($class_name))
  159.             throw new CoreException($cls . " requested a core class that " .
  160.                 "doesn't exists.");
  161.         if (!isset(self::$reflectors[$class_name]))
  162.             return self::$reflectors[$class_name] = new ReflectionClass($class_name);
  163.         return self::$reflectors[$class_name];
  164.     }
  165.  
  166. }
  167.  
  168.  
  169. ?>
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement