Abstract.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. $contextRegistry->addContextsFromDirectory(
  76. dirname(dirname(__FILE__)) . '/Context/Filesystem/', 'Zend_Tool_Project_Context_Filesystem_'
  77. );
  78. self::$_isInitialized = true;
  79. }
  80. // load up the extending providers required context classes
  81. if ($contextClasses = $this->getContextClasses()) {
  82. $this->_loadContextClassesIntoRegistry($contextClasses);
  83. }
  84. }
  85. public function getContextClasses()
  86. {
  87. return array();
  88. }
  89. /**
  90. * _getProject is designed to find if there is project file in the context of where
  91. * the client has been called from.. The search order is as follows..
  92. * - traversing downwards from (PWD) - current working directory
  93. * - if an enpoint variable has been registered in teh client registry - key=workingDirectory
  94. * - if an ENV variable with the key ZFPROJECT_PATH is found
  95. *
  96. * @param $loadProfileFlag bool Whether or not to throw an exception when no profile is found
  97. * @param $projectDirectory string The project directory to use to search
  98. * @param $searchParentDirectories bool Whether or not to search upper level direcotries
  99. * @return Zend_Tool_Project_Profile
  100. */
  101. protected function _loadProfile($loadProfileFlag = self::NO_PROFILE_THROW_EXCEPTION, $projectDirectory = null, $searchParentDirectories = true)
  102. {
  103. // use the cwd if no directory was provided
  104. if ($projectDirectory == null) {
  105. $projectDirectory = getcwd();
  106. } elseif (realpath($projectDirectory) == false) {
  107. throw new Zend_Tool_Project_Provider_Exception('The $projectDirectory supplied does not exist.');
  108. }
  109. $profile = new Zend_Tool_Project_Profile();
  110. $parentDirectoriesArray = explode(DIRECTORY_SEPARATOR, ltrim($projectDirectory, DIRECTORY_SEPARATOR));
  111. while ($parentDirectoriesArray) {
  112. $projectDirectoryAssembled = implode(DIRECTORY_SEPARATOR, $parentDirectoriesArray);
  113. if (DIRECTORY_SEPARATOR !== "\\") {
  114. $projectDirectoryAssembled = DIRECTORY_SEPARATOR . $projectDirectoryAssembled;
  115. }
  116. $profile->setAttribute('projectDirectory', $projectDirectoryAssembled);
  117. if ($profile->isLoadableFromFile()) {
  118. chdir($projectDirectoryAssembled);
  119. $profile->loadFromFile();
  120. $this->_loadedProfile = $profile;
  121. break;
  122. }
  123. // break after first run if we are not to check upper directories
  124. if ($searchParentDirectories == false) {
  125. break;
  126. }
  127. array_pop($parentDirectoriesArray);
  128. }
  129. if ($this->_loadedProfile == null) {
  130. if ($loadProfileFlag == self::NO_PROFILE_THROW_EXCEPTION) {
  131. throw new Zend_Tool_Project_Provider_Exception('A project profile was not found.');
  132. } elseif ($loadProfileFlag == self::NO_PROFILE_RETURN_FALSE) {
  133. return false;
  134. }
  135. }
  136. return $profile;
  137. }
  138. /**
  139. * Load the project profile from the current working directory, if not throw exception
  140. *
  141. * @return Zend_Tool_Project_Profile
  142. */
  143. protected function _loadProfileRequired()
  144. {
  145. $profile = $this->_loadProfile();
  146. if ($profile === false) {
  147. require_once 'Zend/Tool/Project/Provider/Exception.php';
  148. throw new Zend_Tool_Project_Provider_Exception('A project profile was not found in the current working directory.');
  149. }
  150. return $profile;
  151. }
  152. /**
  153. * Return the currently loaded profile
  154. *
  155. * @return Zend_Tool_Project_Profile
  156. */
  157. protected function _getProfile($loadProfileFlag = self::NO_PROFILE_THROW_EXCEPTION)
  158. {
  159. if (!$this->_loadedProfile) {
  160. if (($this->_loadProfile($loadProfileFlag) === false) && ($loadProfileFlag === self::NO_PROFILE_RETURN_FALSE)) {
  161. return false;
  162. }
  163. }
  164. return $this->_loadedProfile;
  165. }
  166. /**
  167. * _storeProfile()
  168. *
  169. * This method will store the profile into its proper location
  170. *
  171. */
  172. protected function _storeProfile()
  173. {
  174. $projectProfileFile = $this->_loadedProfile->search('ProjectProfileFile');
  175. $name = $projectProfileFile->getContext()->getPath();
  176. $this->_registry->getResponse()->appendContent('Updating project profile \'' . $name . '\'');
  177. $projectProfileFile->getContext()->save();
  178. }
  179. protected function _getContentForContext(Zend_Tool_Project_Context_Interface $context, $methodName, $parameters)
  180. {
  181. $storage = $this->_registry->getStorage();
  182. if (!$storage->isEnabled()) {
  183. return false;
  184. }
  185. if (!class_exists('Zend_Tool_Project_Context_Content_Engine')) {
  186. require_once 'Zend/Tool/Project/Context/Content/Engine.php';
  187. }
  188. $engine = new Zend_Tool_Project_Context_Content_Engine($storage);
  189. return $engine->getContent($context, $methodName, $parameters);
  190. }
  191. /**
  192. * _loadContextClassesIntoRegistry() - This is called by the constructor
  193. * so that child providers can provide a list of contexts to load into the
  194. * context repository
  195. *
  196. * @param array $contextClasses
  197. */
  198. private function _loadContextClassesIntoRegistry($contextClasses)
  199. {
  200. $registry = Zend_Tool_Project_Context_Repository::getInstance();
  201. foreach ($contextClasses as $contextClass) {
  202. $registry->addContextClass($contextClass);
  203. }
  204. }
  205. }