BasicLoader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Tool_Framework_Loader_Abstract
  24. */
  25. require_once 'Zend/Tool/Framework/Loader/Interface.php';
  26. /**
  27. * @see Zend_Tool_Framework_Registry_EnabledInterface
  28. */
  29. require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
  30. /**
  31. * @see Zend_Loader
  32. */
  33. require_once 'Zend/Loader.php';
  34. require_once 'Zend/Tool/Framework/Manifest/Interface.php';
  35. require_once 'Zend/Tool/Framework/Provider/Interface.php';
  36. /**
  37. * @category Zend
  38. * @package Zend_Tool
  39. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Tool_Framework_Loader_BasicLoader
  43. implements Zend_Tool_Framework_Loader_Interface, Zend_Tool_Framework_Registry_EnabledInterface
  44. {
  45. /**
  46. * @var Zend_Tool_Framework_Repository_Interface
  47. */
  48. protected $_registry = null;
  49. /**
  50. * @var array
  51. */
  52. protected $_classesToLoad = array();
  53. public function __construct($options = array())
  54. {
  55. if ($options) {
  56. $this->setOptions($options);
  57. }
  58. }
  59. public function setOptions(Array $options)
  60. {
  61. foreach ($options as $optionName => $optionValue) {
  62. $setMethod = 'set' . $optionName;
  63. if (method_exists($this, $setMethod)) {
  64. $this->{$setMethod}($optionValue);
  65. }
  66. }
  67. }
  68. /**
  69. * setRegistry() - required by the enabled interface to get an instance of
  70. * the registry
  71. *
  72. * @param Zend_Tool_Framework_Registry_Interface $registry
  73. * @return Zend_Tool_Framework_Loader_Abstract
  74. */
  75. public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
  76. {
  77. $this->_registry = $registry;
  78. return $this;
  79. }
  80. /**
  81. * @param array $classesToLoad
  82. * @return Zend_Tool_Framework_Loader_Abstract
  83. */
  84. public function setClassesToLoad(array $classesToLoad)
  85. {
  86. $this->_classesToLoad = $classesToLoad;
  87. return $this;
  88. }
  89. public function load()
  90. {
  91. $manifestRegistry = $this->_registry->getManifestRepository();
  92. $providerRegistry = $this->_registry->getProviderRepository();
  93. $loadedClasses = array();
  94. // loop through the loaded classes and ensure that
  95. foreach ($this->_classesToLoad as $class) {
  96. if (!class_exists($class)) {
  97. Zend_Loader::loadClass($class);
  98. }
  99. // reflect class to see if its something we want to load
  100. $reflectionClass = new ReflectionClass($class);
  101. if ($this->_isManifestImplementation($reflectionClass)) {
  102. $manifestRegistry->addManifest($reflectionClass->newInstance());
  103. $loadedClasses[] = $class;
  104. }
  105. if ($this->_isProviderImplementation($reflectionClass)) {
  106. $providerRegistry->addProvider($reflectionClass->newInstance());
  107. $loadedClasses[] = $class;
  108. }
  109. }
  110. return $loadedClasses;
  111. }
  112. /**
  113. * @param ReflectionClass $reflectionClass
  114. * @return bool
  115. */
  116. private function _isManifestImplementation($reflectionClass)
  117. {
  118. return (
  119. $reflectionClass->implementsInterface('Zend_Tool_Framework_Manifest_Interface')
  120. && !$reflectionClass->isAbstract()
  121. );
  122. }
  123. /**
  124. * @param ReflectionClass $reflectionClass
  125. * @return bool
  126. */
  127. private function _isProviderImplementation($reflectionClass)
  128. {
  129. $providerRegistry = $this->_registry->getProviderRepository();
  130. return (
  131. $reflectionClass->implementsInterface('Zend_Tool_Framework_Provider_Interface')
  132. && !$reflectionClass->isAbstract()
  133. && !$providerRegistry->hasProvider($reflectionClass->getName(), false)
  134. );
  135. }
  136. }