File indexing completed on 2025-01-19 05:21:35

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tool
0017  * @subpackage Framework
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Tool_Framework_Loader_Abstract
0025  */
0026 // require_once 'Zend/Tool/Framework/Loader/Interface.php';
0027 
0028 /**
0029  * @see Zend_Tool_Framework_Registry_EnabledInterface
0030  */
0031 // require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
0032 
0033 /**
0034  * @see Zend_Loader
0035  */
0036 // require_once 'Zend/Loader.php';
0037 // require_once 'Zend/Tool/Framework/Manifest/Interface.php';
0038 // require_once 'Zend/Tool/Framework/Provider/Interface.php';
0039 
0040 /**
0041  * @category   Zend
0042  * @package    Zend_Tool
0043  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0044  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0045  */
0046 class Zend_Tool_Framework_Loader_BasicLoader
0047     implements Zend_Tool_Framework_Loader_Interface, Zend_Tool_Framework_Registry_EnabledInterface
0048 {
0049     /**
0050      * @var Zend_Tool_Framework_Repository_Interface
0051      */
0052     protected $_registry = null;
0053 
0054     /**
0055      * @var array
0056      */
0057     protected $_classesToLoad = array();
0058 
0059     public function __construct($options = array())
0060     {
0061         if ($options) {
0062             $this->setOptions($options);
0063         }
0064     }
0065 
0066     public function setOptions(Array $options)
0067     {
0068         foreach ($options as $optionName => $optionValue) {
0069             $setMethod = 'set' . $optionName;
0070             if (method_exists($this, $setMethod)) {
0071                 $this->{$setMethod}($optionValue);
0072             }
0073         }
0074     }
0075 
0076     /**
0077      * setRegistry() - required by the enabled interface to get an instance of
0078      * the registry
0079      *
0080      * @param Zend_Tool_Framework_Registry_Interface $registry
0081      * @return Zend_Tool_Framework_Loader_Abstract
0082      */
0083     public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
0084     {
0085         $this->_registry = $registry;
0086         return $this;
0087     }
0088 
0089     /**
0090      * @param  array $classesToLoad
0091      * @return Zend_Tool_Framework_Loader_Abstract
0092      */
0093     public function setClassesToLoad(array $classesToLoad)
0094     {
0095         $this->_classesToLoad = $classesToLoad;
0096         return $this;
0097     }
0098 
0099     public function load()
0100     {
0101         $manifestRegistry = $this->_registry->getManifestRepository();
0102         $providerRegistry = $this->_registry->getProviderRepository();
0103 
0104         $loadedClasses = array();
0105 
0106         // loop through the loaded classes and ensure that
0107         foreach ($this->_classesToLoad as $class) {
0108 
0109             if (!class_exists($class)) {
0110                 Zend_Loader::loadClass($class);
0111             }
0112 
0113             // reflect class to see if its something we want to load
0114             $reflectionClass = new ReflectionClass($class);
0115             if ($this->_isManifestImplementation($reflectionClass)) {
0116                 $manifestRegistry->addManifest($reflectionClass->newInstance());
0117                 $loadedClasses[] = $class;
0118             }
0119 
0120             if ($this->_isProviderImplementation($reflectionClass)) {
0121                 $providerRegistry->addProvider($reflectionClass->newInstance());
0122                 $loadedClasses[] = $class;
0123             }
0124 
0125         }
0126 
0127         return $loadedClasses;
0128     }
0129 
0130     /**
0131      * @param  ReflectionClass $reflectionClass
0132      * @return bool
0133      */
0134     private function _isManifestImplementation($reflectionClass)
0135     {
0136         return (
0137             $reflectionClass->implementsInterface('Zend_Tool_Framework_Manifest_Interface')
0138                 && !$reflectionClass->isAbstract()
0139         );
0140     }
0141 
0142     /**
0143      * @param  ReflectionClass $reflectionClass
0144      * @return bool
0145      */
0146     private function _isProviderImplementation($reflectionClass)
0147     {
0148         $providerRegistry = $this->_registry->getProviderRepository();
0149 
0150         return (
0151             $reflectionClass->implementsInterface('Zend_Tool_Framework_Provider_Interface')
0152                 && !$reflectionClass->isAbstract()
0153                 && !$providerRegistry->hasProvider($reflectionClass->getName(), false)
0154         );
0155     }
0156 
0157 }