Abstract.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-2012 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_Project_Profile
  24. */
  25. require_once 'Zend/Tool/Project/Profile.php';
  26. /**
  27. * @see Zend_Tool_Framework_Provider_Abstract
  28. */
  29. require_once 'Zend/Tool/Framework/Provider/Abstract.php';
  30. /**
  31. * @see Zend_Tool_Project_Context_Repository
  32. */
  33. require_once 'Zend/Tool/Project/Context/Repository.php';
  34. /**
  35. * @see Zend_Tool_Project_Profile_FileParser_Xml
  36. */
  37. require_once 'Zend/Tool/Project/Profile/FileParser/Xml.php';
  38. /**
  39. * @see Zend_Tool_Framework_Registry
  40. */
  41. require_once 'Zend/Tool/Framework/Registry.php';
  42. require_once 'Zend/Tool/Framework/Provider/Initializable.php';
  43. /**
  44. * @category Zend
  45. * @package Zend_Tool
  46. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. */
  49. abstract class Zend_Tool_Project_Provider_Abstract
  50. extends Zend_Tool_Framework_Provider_Abstract
  51. implements Zend_Tool_Framework_Provider_Initializable
  52. {
  53. const NO_PROFILE_THROW_EXCEPTION = true;
  54. const NO_PROFILE_RETURN_FALSE = false;
  55. /**
  56. * @var bool
  57. */
  58. protected static $_isInitialized = false;
  59. protected $_projectPath = null;
  60. /**
  61. * @var Zend_Tool_Project_Profile
  62. */
  63. protected $_loadedProfile = null;
  64. public function initialize()
  65. {
  66. // initialize the ZF Contexts (only once per php request)
  67. if (!self::$_isInitialized) {
  68. // load all base contexts ONCE
  69. $contextRegistry = Zend_Tool_Project_Context_Repository::getInstance();
  70. $contextRegistry->addContextsFromDirectory(
  71. dirname(dirname(__FILE__)) . '/Context/Zf/', 'Zend_Tool_Project_Context_Zf_'
  72. );
  73. $contextRegistry->addContextsFromDirectory(
  74. dirname(dirname(__FILE__)) . '/Context/Filesystem/', 'Zend_Tool_Project_Context_Filesystem_'
  75. );
  76. // determine if there are project specfic providers ONCE
  77. $profilePath = $this->_findProfileDirectory();
  78. if ($this->_hasProjectProviderDirectory($profilePath . DIRECTORY_SEPARATOR . '.zfproject.xml')) {
  79. $profile = $this->_loadProfile();
  80. // project providers directory resource
  81. $ppd = $profile->search('ProjectProvidersDirectory');
  82. $ppd->loadProviders($this->_registry);
  83. }
  84. self::$_isInitialized = true;
  85. }
  86. // load up the extending providers required context classes
  87. if ($contextClasses = $this->getContextClasses()) {
  88. $this->_loadContextClassesIntoRegistry($contextClasses);
  89. }
  90. }
  91. public function getContextClasses()
  92. {
  93. return array();
  94. }
  95. /**
  96. * _getProject is designed to find if there is project file in the context of where
  97. * the client has been called from.. The search order is as follows..
  98. * - traversing downwards from (PWD) - current working directory
  99. * - if an enpoint variable has been registered in teh client registry - key=workingDirectory
  100. * - if an ENV variable with the key ZFPROJECT_PATH is found
  101. *
  102. * @param bool $loadProfileFlag Whether or not to throw an exception when no profile is found
  103. * @param string $projectDirectory The project directory to use to search
  104. * @param bool $searchParentDirectories Whether or not to search upper level direcotries
  105. * @return Zend_Tool_Project_Profile
  106. */
  107. protected function _loadProfile($loadProfileFlag = self::NO_PROFILE_THROW_EXCEPTION, $projectDirectory = null, $searchParentDirectories = true)
  108. {
  109. $foundPath = $this->_findProfileDirectory($projectDirectory, $searchParentDirectories);
  110. if ($foundPath == false) {
  111. if ($loadProfileFlag == self::NO_PROFILE_THROW_EXCEPTION) {
  112. throw new Zend_Tool_Project_Provider_Exception('A project profile was not found.');
  113. } else {
  114. return false;
  115. }
  116. }
  117. $profile = new Zend_Tool_Project_Profile();
  118. $profile->setAttribute('projectDirectory', $foundPath);
  119. $profile->loadFromFile();
  120. $this->_loadedProfile = $profile;
  121. return $profile;
  122. }
  123. protected function _findProfileDirectory($projectDirectory = null, $searchParentDirectories = true)
  124. {
  125. // use the cwd if no directory was provided
  126. if ($projectDirectory == null) {
  127. $projectDirectory = getcwd();
  128. } elseif (realpath($projectDirectory) == false) {
  129. throw new Zend_Tool_Project_Provider_Exception('The $projectDirectory supplied does not exist.');
  130. }
  131. $profile = new Zend_Tool_Project_Profile();
  132. $parentDirectoriesArray = explode(DIRECTORY_SEPARATOR, ltrim($projectDirectory, DIRECTORY_SEPARATOR));
  133. while ($parentDirectoriesArray) {
  134. $projectDirectoryAssembled = implode(DIRECTORY_SEPARATOR, $parentDirectoriesArray);
  135. if (DIRECTORY_SEPARATOR !== "\\") {
  136. $projectDirectoryAssembled = DIRECTORY_SEPARATOR . $projectDirectoryAssembled;
  137. }
  138. $profile->setAttribute('projectDirectory', $projectDirectoryAssembled);
  139. if ($profile->isLoadableFromFile()) {
  140. unset($profile);
  141. return $projectDirectoryAssembled;
  142. }
  143. // break after first run if we are not to check upper directories
  144. if ($searchParentDirectories == false) {
  145. break;
  146. }
  147. array_pop($parentDirectoriesArray);
  148. }
  149. return false;
  150. }
  151. /**
  152. * Load the project profile from the current working directory, if not throw exception
  153. *
  154. * @return Zend_Tool_Project_Profile
  155. */
  156. protected function _loadProfileRequired()
  157. {
  158. $profile = $this->_loadProfile();
  159. if ($profile === false) {
  160. require_once 'Zend/Tool/Project/Provider/Exception.php';
  161. throw new Zend_Tool_Project_Provider_Exception('A project profile was not found in the current working directory.');
  162. }
  163. return $profile;
  164. }
  165. /**
  166. * Return the currently loaded profile
  167. *
  168. * @return Zend_Tool_Project_Profile
  169. */
  170. protected function _getProfile($loadProfileFlag = self::NO_PROFILE_THROW_EXCEPTION)
  171. {
  172. if (!$this->_loadedProfile) {
  173. if (($this->_loadProfile($loadProfileFlag) === false) && ($loadProfileFlag === self::NO_PROFILE_RETURN_FALSE)) {
  174. return false;
  175. }
  176. }
  177. return $this->_loadedProfile;
  178. }
  179. /**
  180. * _storeProfile()
  181. *
  182. * This method will store the profile into its proper location
  183. *
  184. */
  185. protected function _storeProfile()
  186. {
  187. $projectProfileFile = $this->_loadedProfile->search('ProjectProfileFile');
  188. $name = $projectProfileFile->getContext()->getPath();
  189. $this->_registry->getResponse()->appendContent('Updating project profile \'' . $name . '\'');
  190. $projectProfileFile->getContext()->save();
  191. }
  192. protected function _getContentForContext(Zend_Tool_Project_Context_Interface $context, $methodName, $parameters)
  193. {
  194. $storage = $this->_registry->getStorage();
  195. if (!$storage->isEnabled()) {
  196. return false;
  197. }
  198. if (!class_exists('Zend_Tool_Project_Context_Content_Engine')) {
  199. require_once 'Zend/Tool/Project/Context/Content/Engine.php';
  200. }
  201. $engine = new Zend_Tool_Project_Context_Content_Engine($storage);
  202. return $engine->getContent($context, $methodName, $parameters);
  203. }
  204. protected function _hasProjectProviderDirectory($pathToProfileFile)
  205. {
  206. // do some static analysis of the file so that we can determin whether or not to incure
  207. // the cost of loading the profile before the system is fully bootstrapped
  208. if (!file_exists($pathToProfileFile)) {
  209. return false;
  210. }
  211. $contents = file_get_contents($pathToProfileFile);
  212. if (strstr($contents, '<projectProvidersDirectory') === false) {
  213. return false;
  214. }
  215. if (strstr($contents, '<projectProvidersDirectory enabled="false"')) {
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * _loadContextClassesIntoRegistry() - This is called by the constructor
  222. * so that child providers can provide a list of contexts to load into the
  223. * context repository
  224. *
  225. * @param array $contextClasses
  226. */
  227. private function _loadContextClassesIntoRegistry($contextClasses)
  228. {
  229. $registry = Zend_Tool_Project_Context_Repository::getInstance();
  230. foreach ($contextClasses as $contextClass) {
  231. $registry->addContextClass($contextClass);
  232. }
  233. }
  234. }