web/lib/Zend/Tool/Framework/Loader/BasicLoader.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Tool
       
    17  * @subpackage Framework
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: BasicLoader.php 20785 2010-01-31 09:43:03Z mikaelkael $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Framework_Loader_Abstract
       
    25  */
       
    26 require_once 'Zend/Tool/Framework/Loader/Interface.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Tool_Framework_Registry_EnabledInterface
       
    30  */
       
    31 require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
       
    32 
       
    33 /**
       
    34  * @see Zend_Loader
       
    35  */
       
    36 require_once 'Zend/Loader.php';
       
    37 require_once 'Zend/Tool/Framework/Manifest/Interface.php';
       
    38 require_once 'Zend/Tool/Framework/Provider/Interface.php';
       
    39 
       
    40 /**
       
    41  * @category   Zend
       
    42  * @package    Zend_Tool
       
    43  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    44  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    45  */
       
    46 class Zend_Tool_Framework_Loader_BasicLoader 
       
    47     implements Zend_Tool_Framework_Loader_Interface, Zend_Tool_Framework_Registry_EnabledInterface
       
    48 {
       
    49     /**
       
    50      * @var Zend_Tool_Framework_Repository_Interface
       
    51      */
       
    52     protected $_registry = null;
       
    53 
       
    54     /**
       
    55      * @var array
       
    56      */
       
    57     protected $_classesToLoad = array();
       
    58     
       
    59     public function __construct($options = array())
       
    60     {
       
    61         if ($options) {
       
    62             $this->setOptions($options);
       
    63         }
       
    64     }
       
    65     
       
    66     public function setOptions(Array $options)
       
    67     {
       
    68         foreach ($options as $optionName => $optionValue) {
       
    69             $setMethod = 'set' . $optionName;
       
    70             if (method_exists($this, $setMethod)) {
       
    71                 $this->{$setMethod}($optionValue);
       
    72             }
       
    73         }
       
    74     }
       
    75     
       
    76     /**
       
    77      * setRegistry() - required by the enabled interface to get an instance of
       
    78      * the registry
       
    79      *
       
    80      * @param Zend_Tool_Framework_Registry_Interface $registry
       
    81      * @return Zend_Tool_Framework_Loader_Abstract
       
    82      */
       
    83     public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
       
    84     {
       
    85         $this->_registry = $registry;
       
    86         return $this;
       
    87     }
       
    88 
       
    89     /**
       
    90      * @param  array $classesToLoad
       
    91      * @return Zend_Tool_Framework_Loader_Abstract
       
    92      */
       
    93     public function setClassesToLoad(array $classesToLoad)
       
    94     {
       
    95         $this->_classesToLoad = $classesToLoad;
       
    96         return $this;
       
    97     }
       
    98     
       
    99     public function load()
       
   100     {
       
   101         $manifestRegistry = $this->_registry->getManifestRepository();
       
   102         $providerRegistry = $this->_registry->getProviderRepository();
       
   103         
       
   104         $loadedClasses = array();
       
   105         
       
   106         // loop through the loaded classes and ensure that
       
   107         foreach ($this->_classesToLoad as $class) {
       
   108             
       
   109             if (!class_exists($class)) {
       
   110                 Zend_Loader::loadClass($class);
       
   111             }
       
   112 
       
   113             // reflect class to see if its something we want to load
       
   114             $reflectionClass = new ReflectionClass($class);
       
   115             if ($this->_isManifestImplementation($reflectionClass)) {
       
   116                 $manifestRegistry->addManifest($reflectionClass->newInstance());
       
   117                 $loadedClasses[] = $class;
       
   118             }
       
   119 
       
   120             if ($this->_isProviderImplementation($reflectionClass)) {
       
   121                 $providerRegistry->addProvider($reflectionClass->newInstance());
       
   122                 $loadedClasses[] = $class;
       
   123             }
       
   124 
       
   125         }
       
   126 
       
   127         return $loadedClasses;
       
   128     }
       
   129 
       
   130     /**
       
   131      * @param  ReflectionClass $reflectionClass
       
   132      * @return bool
       
   133      */
       
   134     private function _isManifestImplementation($reflectionClass)
       
   135     {
       
   136         return (
       
   137             $reflectionClass->implementsInterface('Zend_Tool_Framework_Manifest_Interface')
       
   138                 && !$reflectionClass->isAbstract()
       
   139         );
       
   140     }
       
   141 
       
   142     /**
       
   143      * @param  ReflectionClass $reflectionClass
       
   144      * @return bool
       
   145      */
       
   146     private function _isProviderImplementation($reflectionClass)
       
   147     {
       
   148         $providerRegistry = $this->_registry->getProviderRepository();
       
   149 
       
   150         return (
       
   151             $reflectionClass->implementsInterface('Zend_Tool_Framework_Provider_Interface')
       
   152                 && !$reflectionClass->isAbstract()
       
   153                 && !$providerRegistry->hasProvider($reflectionClass->getName(), false)
       
   154         );
       
   155     }
       
   156     
       
   157 }