Abstract.php 4.4 KB

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