Abstract.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_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. /**
  43. * @category Zend
  44. * @package Zend_Tool
  45. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. */
  48. abstract class Zend_Tool_Project_Provider_Abstract extends Zend_Tool_Framework_Provider_Abstract
  49. {
  50. const NO_PROFILE_THROW_EXCEPTION = true;
  51. const NO_PROFILE_RETURN_FALSE = false;
  52. /**
  53. * @var bool
  54. */
  55. protected static $_isInitialized = false;
  56. protected $_projectPath = null;
  57. /**
  58. * @var Zend_Tool_Project_Profile
  59. */
  60. protected $_loadedProfile = null;
  61. /**
  62. * constructor
  63. *
  64. * YOU SHOULD NOT OVERRIDE THIS, unless you know what you are doing
  65. *
  66. */
  67. public function __construct()
  68. {
  69. // initialize the ZF Contexts (only once per php request)
  70. if (!self::$_isInitialized) {
  71. $contextRegistry = Zend_Tool_Project_Context_Repository::getInstance();
  72. $contextRegistry->addContextsFromDirectory(
  73. dirname(dirname(__FILE__)) . '/Context/Zf/', 'Zend_Tool_Project_Context_Zf_'
  74. );
  75. self::$_isInitialized = true;
  76. }
  77. // load up the extending providers required context classes
  78. if ($contextClasses = $this->getContextClasses()) {
  79. $this->_loadContextClassesIntoRegistry($contextClasses);
  80. }
  81. }
  82. public function getContextClasses()
  83. {
  84. return array();
  85. }
  86. /**
  87. * _getProject is designed to find if there is project file in the context of where
  88. * the client has been called from.. The search order is as follows..
  89. * - traversing downwards from (PWD) - current working directory
  90. * - if an enpoint variable has been registered in teh client registry - key=workingDirectory
  91. * - if an ENV variable with the key ZFPROJECT_PATH is found
  92. *
  93. * @param $loadProfileFlag bool Whether or not to throw an exception when no profile is found
  94. * @param $projectDirectory string The project directory to use to search
  95. * @param $searchParentDirectories bool Whether or not to search upper level direcotries
  96. * @return Zend_Tool_Project_Profile
  97. */
  98. protected function _loadProfile($loadProfileFlag = self::NO_PROFILE_THROW_EXCEPTION, $projectDirectory = null, $searchParentDirectories = true)
  99. {
  100. // use the cwd if no directory was provided
  101. if ($projectDirectory == null) {
  102. $projectDirectory = getcwd();
  103. } elseif (realpath($projectDirectory) == false) {
  104. throw new Zend_Tool_Project_Provider_Exception('The $projectDirectory supplied does not exist.');
  105. }
  106. $profile = new Zend_Tool_Project_Profile();
  107. $parentDirectoriesArray = split(DIRECTORY_SEPARATOR, ltrim($projectDirectory, DIRECTORY_SEPARATOR));
  108. while ($parentDirectoriesArray) {
  109. $projectDirectoryAssembled = DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $parentDirectoriesArray);
  110. $profile->setAttribute('projectDirectory', $projectDirectoryAssembled);
  111. if ($profile->isLoadableFromFile()) {
  112. chdir($projectDirectoryAssembled);
  113. $profile->loadFromFile();
  114. $this->_loadedProfile = $profile;
  115. break;
  116. }
  117. // break after first run if we are not to check upper directories
  118. if ($searchParentDirectories == false) {
  119. break;
  120. }
  121. array_pop($parentDirectoriesArray);
  122. }
  123. if ($this->_loadedProfile == null) {
  124. if ($loadProfileFlag == self::NO_PROFILE_THROW_EXCEPTION) {
  125. throw new Zend_Tool_Project_Provider_Exception('A project profile was not found.');
  126. } elseif ($loadProfileFlag == self::NO_PROFILE_RETURN_FALSE) {
  127. return false;
  128. }
  129. }
  130. return $profile;
  131. }
  132. /**
  133. * Load the project profile from the current working directory, if not throw exception
  134. *
  135. * @return Zend_Tool_Project_Profile
  136. */
  137. protected function _loadProfileRequired()
  138. {
  139. $profile = $this->_loadProfile();
  140. if ($profile === false) {
  141. require_once 'Zend/Tool/Project/Provider/Exception.php';
  142. throw new Zend_Tool_Project_Provider_Exception('A project profile was not found in the current working directory.');
  143. }
  144. return $profile;
  145. }
  146. /**
  147. * Return the currently loaded profile
  148. *
  149. * @return Zend_Tool_Project_Profile
  150. */
  151. protected function _getProfile($loadProfileFlag = self::NO_PROFILE_THROW_EXCEPTION)
  152. {
  153. if (!$this->_loadedProfile) {
  154. if (($this->_loadProfile($loadProfileFlag) === false) && ($loadProfileFlag === self::NO_PROFILE_RETURN_FALSE)) {
  155. return false;
  156. }
  157. }
  158. return $this->_loadedProfile;
  159. }
  160. /**
  161. * _storeProfile()
  162. *
  163. * This method will store the profile into its proper location
  164. *
  165. */
  166. protected function _storeProfile()
  167. {
  168. $projectProfileFile = $this->_loadedProfile->search('ProjectProfileFile');
  169. $name = $projectProfileFile->getContext()->getPath();
  170. $this->_registry->getResponse()->appendContent('Updating project profile \'' . $name . '\'');
  171. $projectProfileFile->getContext()->save();
  172. }
  173. protected function _getContentForContext(Zend_Tool_Project_Context_Interface $context, $methodName, $parameters)
  174. {
  175. $storage = $this->_registry->getStorage();
  176. if (!$storage->isEnabled()) {
  177. return false;
  178. }
  179. if (!class_exists('Zend_Tool_Project_Context_Content_Engine')) {
  180. require_once 'Zend/Tool/Project/Context/Content/Engine.php';
  181. }
  182. $engine = new Zend_Tool_Project_Context_Content_Engine($storage);
  183. return $engine->getContent($context, $methodName, $parameters);
  184. }
  185. /**
  186. * _loadContextClassesIntoRegistry() - This is called by the constructor
  187. * so that child providers can provide a list of contexts to load into the
  188. * context repository
  189. *
  190. * @param array $contextClasses
  191. */
  192. private function _loadContextClassesIntoRegistry($contextClasses)
  193. {
  194. $registry = Zend_Tool_Project_Context_Repository::getInstance();
  195. foreach ($contextClasses as $contextClass) {
  196. $registry->addContextClass($contextClass);
  197. }
  198. }
  199. }