Abstract.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_Registry_EnabledInterface
  24. */
  25. require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
  26. require_once 'Zend/Tool/Framework/Loader/Interface.php';
  27. require_once 'Zend/Tool/Framework/Manifest/Interface.php';
  28. require_once 'Zend/Tool/Framework/Provider/Interface.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Tool
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_Tool_Framework_Loader_Abstract
  36. implements Zend_Tool_Framework_Loader_Interface, Zend_Tool_Framework_Registry_EnabledInterface
  37. {
  38. /**
  39. * @var Zend_Tool_Framework_Repository_Interface
  40. */
  41. protected $_registry = null;
  42. /**
  43. * @var array
  44. */
  45. private $_retrievedFiles = array();
  46. /**
  47. * @var array
  48. */
  49. private $_loadedClasses = array();
  50. /**
  51. * _getFiles
  52. *
  53. * @return array Array Of Files
  54. */
  55. abstract protected function _getFiles();
  56. /**
  57. * setRegistry() - required by the enabled interface to get an instance of
  58. * the registry
  59. *
  60. * @param Zend_Tool_Framework_Registry_Interface $registry
  61. * @return Zend_Tool_Framework_Loader_Abstract
  62. */
  63. public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
  64. {
  65. $this->_registry = $registry;
  66. return $this;
  67. }
  68. /**
  69. * load() - called by the client initialize routine to load files
  70. *
  71. */
  72. public function load()
  73. {
  74. $this->_retrievedFiles = $this->getRetrievedFiles();
  75. $this->_loadedClasses = array();
  76. $manifestRepository = $this->_registry->getManifestRepository();
  77. $providerRepository = $this->_registry->getProviderRepository();
  78. $loadedClasses = array();
  79. // loop through files and find the classes declared by loading the file
  80. foreach ($this->_retrievedFiles as $file) {
  81. if(is_dir($file)) {
  82. continue;
  83. }
  84. $classesLoadedBefore = get_declared_classes();
  85. $oldLevel = error_reporting(E_ALL | ~E_STRICT); // remove strict so that other packages wont throw warnings
  86. // should we lint the files here? i think so
  87. include_once $file;
  88. error_reporting($oldLevel); // restore old error level
  89. $classesLoadedAfter = get_declared_classes();
  90. $loadedClasses = array_merge($loadedClasses, array_diff($classesLoadedAfter, $classesLoadedBefore));
  91. }
  92. // loop through the loaded classes and ensure that
  93. foreach ($loadedClasses as $loadedClass) {
  94. // reflect class to see if its something we want to load
  95. $reflectionClass = new ReflectionClass($loadedClass);
  96. if ($reflectionClass->implementsInterface('Zend_Tool_Framework_Manifest_Interface')
  97. && !$reflectionClass->isAbstract())
  98. {
  99. $manifestRepository->addManifest($reflectionClass->newInstance());
  100. $this->_loadedClasses[] = $loadedClass;
  101. }
  102. if ($reflectionClass->implementsInterface('Zend_Tool_Framework_Provider_Interface')
  103. && !$reflectionClass->isAbstract()
  104. && !$providerRepository->hasProvider($reflectionClass->getName(), false))
  105. {
  106. $providerRepository->addProvider($reflectionClass->newInstance());
  107. $this->_loadedClasses[] = $loadedClass;
  108. }
  109. }
  110. return $this->_loadedClasses;
  111. }
  112. /**
  113. * getRetrievedFiles()
  114. *
  115. * @return array Array of Files Retrieved
  116. */
  117. public function getRetrievedFiles()
  118. {
  119. if ($this->_retrievedFiles == null) {
  120. $this->_retrievedFiles = $this->_getFiles();
  121. }
  122. return $this->_retrievedFiles;
  123. }
  124. /**
  125. * getLoadedClasses()
  126. *
  127. * @return array Array of Loaded Classes
  128. */
  129. public function getLoadedClasses()
  130. {
  131. return $this->_loadedClasses;
  132. }
  133. }