Source for file jClasses.class.php

Documentation is available at jClasses.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  utils
  5. @author      Laurent Jouanneau
  6. @contributor Loic Mathaud
  7. @contributor Christophe Thiriot
  8. @copyright   2005-2007 Laurent Jouanneau
  9. @copyright   2008 Christophe Thiriot
  10. @link        http://www.jelix.org
  11. @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  12. */
  13.  
  14. /**
  15. * This object is responsible to include and instancy some classes stored in the classes directory of modules.
  16. @package     jelix
  17. @subpackage  utils
  18. @static
  19. */
  20. class jClasses {
  21.  
  22.     static protected $_instances = array();
  23.  
  24.     static protected $_bindings = array();
  25.  
  26.     private function __construct(){}
  27.  
  28.     /**
  29.      * include the given class and return an instance
  30.      * @param string $selector the jelix selector correponding to the class
  31.      * @return object an instance of the classe
  32.      */
  33.     static public function create($selector{
  34.         $sel new jSelectorClass($selector);
  35.         require_once($sel->getPath());
  36.         $class $sel->className;
  37.         return new $class ();
  38.     }
  39.  
  40.     /**
  41.      * Shortcut to corresponding jClassBinding::getInstance() but without singleton
  42.      * The binding is recreated each time (be careful about performance)
  43.      * 
  44.      * @param string $selector  Selector to a bindable class|interface
  45.      * @return object           Corresponding instance
  46.      * @since 1.1
  47.      * @experimental  This method is EXPERIMENTAL. It could be changed in future version
  48.      */
  49.     static public function createBinded($selector{
  50.         return self::bind($selector)->getInstance(false);
  51.     }
  52.  
  53.     /**
  54.      * alias of create method
  55.      * @see jClasses::create()
  56.      */
  57.     static public function createInstance($selector{
  58.         return self::create($selector);
  59.     }
  60.  
  61.     /**
  62.      * include the given class and return always the same instance
  63.      *
  64.      * @param string $selector the jelix selector correponding to the class
  65.      * @return object an instance of the classe
  66.      */
  67.     static public function getService($selector{
  68.         $sel new jSelectorClass($selector);
  69.         $s $sel->toString();
  70.         if (isset(self::$_instances[$s])) {
  71.             return self::$_instances[$s];
  72.         else {
  73.             $o self::create($selector);
  74.             self::$_instances[$s]=$o;
  75.             return $o;
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * Shortcut to corresponding jClassBinding::getInstance()
  81.      * 
  82.      * @param string $selector  Selector to a bindable class|interface
  83.      * @return object           Corresponding instance
  84.      * @since 1.1
  85.      * @experimental  This method is EXPERIMENTAL. It could be changed in future version
  86.      */
  87.     static public function getBindedService($selector{
  88.         return self::bind($selector)->getInstance();
  89.     }
  90.  
  91.     /**
  92.      * Get the binding corresponding to the specified selector.
  93.      * Better for use like this : jClasses::bind($selector)->getClassName()
  94.      * 
  95.      * @param string $selector 
  96.      * @param bool   $singleton if this binding should be a singleton or not
  97.      * @return jClassBinding 
  98.      * @see jClasses::bind
  99.      * @since 1.1
  100.      * @experimental  This method is EXPERIMENTAL. It could be changed in future version
  101.      */
  102.     static public function bind($selector{
  103.         $osel jSelectorFactory::create($selector'iface');
  104.         $s    $osel->toString(true);
  105.  
  106.         if (!isset(self::$_bindings[$s])) {
  107.             self::$_bindings[$snew jClassBinding($osel);
  108.         }
  109.  
  110.         return self::$_bindings[$s];
  111.     }
  112.  
  113.     /**
  114.      * Reset the defined bindings (should only use it for unit tests)
  115.      * 
  116.      * @return void 
  117.      * @since 1.1
  118.      * @experimental  This method is EXPERIMENTAL. It could be changed in future version
  119.      */
  120.     static public function resetBindings({
  121.         self::$_bindings array();
  122.     }
  123.  
  124.     /**
  125.      * only include a class
  126.      * @param string $selector the jelix selector correponding to the class
  127.      */
  128.     static public function inc($selector{
  129.         $sel new jSelectorClass($selector);
  130.         require_once($sel->getPath());
  131.     }
  132.  
  133.     /**
  134.      * include an interface
  135.      * @param string $selector the jelix selector correponding to the interface
  136.      * @since 1.0b2
  137.      */
  138.     static public function incIface($selector{
  139.         $sel new jSelectorIface($selector);
  140.         require_once($sel->getPath());
  141.     }
  142. }

Documentation generated on Thu, 19 Sep 2013 00:02:33 +0200 by phpDocumentor 1.4.3