EclipseGc

ContextFactory::getInstance

Mar 7th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1.  
  2.   /**
  3.    * Implements FactoryInterface::getInstance().
  4.    */
  5.   public function getInstance($options) {
  6.     $config = $this->getConfiguration($options);
  7.     $plugin_class = $this->getPluginClass($config);
  8.  
  9.     // Lets figure out of there's a constructor for this class and pull
  10.     // arguments from the $options array if so to populate it.
  11.     $reflector = new ReflectionClass($plugin_class);
  12.     if ($reflector->hasMethod('__construct')) {
  13.       $params = $reflector->getMethod('__construct')->getParameters();
  14.       $args = array();
  15.       foreach ($params as $param) {
  16.         $args[$param->name] = $options[$param->name];
  17.       }
  18.       $instance = $reflector->newInstanceArgs($args);
  19.     }
  20.     else {
  21.       $instance = new $plugin_class();
  22.     }
  23.  
  24.     if ($instance instanceof PluginDerivativeInterface && isset($options['derivative'])) {
  25.       $instance->getDerivative($options['derivative']);
  26.     }
  27.     if ($instance instanceof PluginContextInterface) {
  28.       // @TODO: Once the Symfony Request component is in, we can revisit this.
  29.       /*$container = drupal_container();
  30.       $instance->setContext($container['context']);*/
  31.     }
  32.     return $instance;
  33.   }
Advertisement
Add Comment
Please, Sign In to add comment