Source for file jClassBinding.class.php

Documentation is available at jClassBinding.class.php

  1. <?php
  2. /**
  3.  * @package     jelix
  4.  * @subpackage  utils
  5.  * @author      Christophe Thiriot
  6.  * @contributor Laurent Jouanneau
  7.  * @copyright   2008 Christophe Thiriot, 2008-2010 Laurent Jouanneau
  8.  * @link        http://www.jelix.org
  9.  * @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10.  * @since 1.1
  11.  */
  12.  
  13. /**
  14.  * Services binding for jelix
  15.  *
  16.  * @package     jelix
  17.  * @subpackage  utils
  18.  * @experimental  This class is EXPERIMENTAL. Its API and its behaviors could
  19.  *  change in future version
  20.  */
  21. class jClassBinding {
  22.     /**
  23.      * @var jSelectorIface|jSelectorClassCalled selector
  24.      */
  25.     protected $fromSelector = null;
  26.  
  27.     /**
  28.      * selector to the binded class (string form)
  29.      */
  30.     protected $toSelector = null;
  31.  
  32.     /**
  33.      * resulting binded instance
  34.      */
  35.     protected $instance = null;
  36.  
  37.     /**
  38.      * __constructor
  39.      * @param jSelectorIface|jSelectorClass$selector the selector of the class
  40.      * @return void 
  41.      */
  42.     public function __construct($selector{
  43.         require_once($selector->getPath());
  44.         $this->fromSelector = $selector;
  45.     }
  46.  
  47.     /**
  48.      * Bind the selector to the class specified
  49.      * Even if this instance is already defined (BE CAREFUL !!! singleton is bypassed)
  50.      *
  51.      * @param string $toselector 
  52.      * @return jClassBinding $this
  53.      */
  54.     public function to($toselector{
  55.         $this->toSelector = new jSelectorClass($toselector);
  56.         $this->instance   = null;
  57.         return $this;
  58.     }
  59.  
  60.     /**
  61.      * Bind the selector to the specified instance
  62.      * Even if this instance is already defined (BE CAREFUL !!! singleton is bypassed)
  63.      *
  64.      * @param  mixed    $instance 
  65.      * @return jClassBinding $this
  66.      */
  67.     public function toInstance($instance{
  68.         $this->instance   = $instance;
  69.         $this->toSelector = null;
  70.         return $this;
  71.     }
  72.  
  73.     /**
  74.      * Get the binded instance
  75.      *
  76.      * @return mixed 
  77.      */
  78.     public function getInstance($singleton=true{
  79.         if (true === $singleton && $this->instance !== null{
  80.             return $this->instance;
  81.         }
  82.         $this->instance = $this->_createInstance();
  83.         return $this->instance;
  84.     }
  85.  
  86.     /**
  87.      * Create the binded selector if not initialzed yet
  88.      * 
  89.      * @return mixed 
  90.      */
  91.     protected function _createInstance({
  92.         if ($this->toSelector === null{
  93.             $this->instance   = null;
  94.             $this->toSelector = $this->_getClassSelector();    
  95.         }
  96.         return jClasses::create($this->toSelector->toString());
  97.     }
  98.  
  99.     /**
  100.      * Get the name of the binded class without creating this class
  101.      *
  102.      * @return string 
  103.      */
  104.     public function getClassName({
  105.         if ($this->instance !== null{
  106.             return get_class($this->instance);
  107.         elseif ($this->toSelector !== null{
  108.             return $this->toSelector->className;
  109.         else {
  110.             return $this->_getClassSelector()->className;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Get the selector to the binded class
  116.      * Protected because this does not work if called after a simple __construct() and a toInstance()
  117.      *
  118.      * @return string 
  119.      */
  120.     protected function _getClassSelector({
  121.         $class_selector null;
  122.  
  123.         // the instance is not already created
  124.         if ($this->toSelector === null && $this->instance === null{
  125.             $str_selector      $this->fromSelector->toString();
  126.             $str_selector_long $this->fromSelector->toString(true);
  127.  
  128.             // 1) verify that a default implementation is specified in the jelix config file
  129.             global $gJConfig;
  130.             if (isset($gJConfig->classbindings&& count($gJConfig->classbindings)) {
  131.                 $conf $gJConfig->classbindings;
  132.  
  133.                 // No '~' allowed as key of a ini file, we use '-' instead
  134.                 $conf_selector      str_replace('~''-'$str_selector);
  135.                 $conf_selector_long str_replace('~''-'$str_selector_long);
  136.                 // get the binding corresponding to selector, long or not
  137.                 $str_fromselector null;
  138.                 if (isset($conf[$conf_selector])) {
  139.                     $str_fromselector $conf_selector;
  140.                 elseif (isset($conf[$conf_selector_long])) {
  141.                     $str_fromselector $conf_selector_long;
  142.                 }
  143.  
  144.                 if ($str_fromselector !== null{
  145.                     $this->fromSelector = jSelectorFactory::create($str_selector_long'iface');
  146.                     return $this->toSelector = new jSelectorClass($conf[$str_fromselector]);
  147.                 }
  148.             }
  149.  
  150.             // 2) see if a default implementation is specified in the source class
  151.             $constname $this->fromSelector->className '::JBINDING_BINDED_IMPLEMENTATION';
  152.             if (defined($constname)) // check first, constant() crashes on some php version when the const does not exist
  153.                 $class_selector constant($constname);
  154.                 if ($class_selector!==null)
  155.                     return $this->toSelector = new jSelectorClass($class_selector);
  156.             }
  157.  
  158.             // 3) If the source is a class, then use it as the default implementation
  159.             if (true === ($this->fromSelector instanceof jSelectorClass)) {
  160.                 return $this->toSelector = $this->fromSelector;
  161.             }
  162.  
  163.             throw new jException('jelix~errors.bindings.nobinding'array($this->fromSelector->toString(true)));
  164.         }
  165.     }
  166. }

Documentation generated on Wed, 24 Sep 2014 21:56:37 +0200 by phpDocumentor 1.4.3