PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Zend/Tool/Project/Profile.php

https://github.com/grandison/budo16
PHP | 242 lines | 121 code | 24 blank | 97 comment | 11 complexity | 7614f973190e7137877f18ec98eb6289 MD5 | raw file
  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: Profile.php 16971 2009-07-22 18:05:45Z mikaelkael $
  21. */
  22. /**
  23. * @see Zend_Tool_Project_Profile_FileParser_Xml
  24. */
  25. // require_once 'Zend/Tool/Project/Profile/FileParser/Xml.php';
  26. /**
  27. * @see Zend_Tool_Project_Profile_Resource_Container
  28. */
  29. // require_once 'Zend/Tool/Project/Profile/Resource/Container.php';
  30. /**
  31. * This class is the front most class for utilizing Zend_Tool_Project
  32. *
  33. * A profile is a hierarchical set of resources that keep track of
  34. * items within a specific project.
  35. *
  36. * @category Zend
  37. * @package Zend_Tool
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Tool_Project_Profile extends Zend_Tool_Project_Profile_Resource_Container
  42. {
  43. /**
  44. * @var bool
  45. */
  46. protected static $_traverseEnabled = false;
  47. /**
  48. * @var array
  49. */
  50. protected $_attributes = array();
  51. /**
  52. * Constructor, standard usage would allow the setting of options
  53. *
  54. * @param array $options
  55. * @return bool
  56. */
  57. public function __construct($options = null)
  58. {
  59. if ($options) {
  60. $this->setOptions($options);
  61. }
  62. $this->_topResources = new Zend_Tool_Project_Profile_Resource_Container();
  63. }
  64. /**
  65. * Process options and either set a profile property or
  66. * set a profile 'attribute'
  67. *
  68. * @param array $options
  69. */
  70. public function setOptions(Array $options)
  71. {
  72. $this->setAttributes($options);
  73. }
  74. /**
  75. * getIterator() - reqruied by the RecursiveIterator interface
  76. *
  77. * @return RecursiveIteratorIterator
  78. */
  79. public function getIterator()
  80. {
  81. // require_once 'Zend/Tool/Project/Profile/Iterator/EnabledResourceFilter.php';
  82. return new RecursiveIteratorIterator(
  83. new Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter($this),
  84. RecursiveIteratorIterator::SELF_FIRST
  85. );
  86. }
  87. /**
  88. * loadFromData() - Load a profile from data provided by the
  89. * 'profilData' attribute
  90. *
  91. */
  92. public function loadFromData()
  93. {
  94. if (!isset($this->_attributes['profileData'])) {
  95. // require_once 'Zend/Tool/Project/Exception.php';
  96. throw new Zend_Tool_Project_Exception('loadFromData() must have "profileData" set.');
  97. }
  98. $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml();
  99. $profileFileParser->unserialize($this->_attributes['profileData'], $this);
  100. $this->rewind();
  101. }
  102. /**
  103. * isLoadableFromFile() - can a profile be loaded from a file
  104. *
  105. * wether or not a profile can be loaded from the
  106. * file in attribute 'projectProfileFile', or from a file named
  107. * '.zfproject.xml' inside a directory in key 'projectDirectory'
  108. *
  109. * @return bool
  110. */
  111. public function isLoadableFromFile()
  112. {
  113. if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) {
  114. return false;
  115. }
  116. if (isset($this->_attributes['projectProfileFile'])) {
  117. $projectProfileFilePath = $this->_attributes['projectProfileFile'];
  118. if (!file_exists($projectProfileFilePath)) {
  119. return false;
  120. }
  121. } else {
  122. $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml';
  123. if (!file_exists($projectProfileFilePath)) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. /**
  130. * loadFromFile() - Load data from file
  131. *
  132. * this attempts to load a project profile file from a variety of locations depending
  133. * on what information the user provided vie $options or attributes, specifically the
  134. * 'projectDirectory' or 'projectProfileFile'
  135. *
  136. */
  137. public function loadFromFile()
  138. {
  139. // if no data is supplied, need either a projectProfileFile or a projectDirectory
  140. if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) {
  141. // require_once 'Zend/Tool/Project/Exception.php';
  142. throw new Zend_Tool_Project_Exception('loadFromFile() must have at least "projectProfileFile" or "projectDirectory" set.');
  143. }
  144. if (isset($this->_attributes['projectProfileFile'])) {
  145. $projectProfileFilePath = $this->_attributes['projectProfileFile'];
  146. if (!file_exists($projectProfileFilePath)) {
  147. // require_once 'Zend/Tool/Project/Exception.php';
  148. throw new Zend_Tool_Project_Exception('"projectProfileFile" was supplied but file was not found at location ' . $projectProfileFilePath);
  149. }
  150. $this->_attributes['projectDirectory'] = dirname($projectProfileFilePath);
  151. } else {
  152. $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml';
  153. if (!file_exists($projectProfileFilePath)) {
  154. // require_once 'Zend/Tool/Project/Exception.php';
  155. throw new Zend_Tool_Project_Exception('"projectDirectory" was supplied but no profile file file was not found at location ' . $projectProfileFilePath);
  156. }
  157. $this->_attributes['projectProfileFile'] = $projectProfileFilePath;
  158. }
  159. $profileData = file_get_contents($projectProfileFilePath);
  160. $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml();
  161. $profileFileParser->unserialize($profileData, $this);
  162. $this->rewind();
  163. }
  164. /**
  165. * storeToFile() - store the current profile to file
  166. *
  167. * This will store the profile in memory to a place on disk determined by the attributes
  168. * available, specifically if the key 'projectProfileFile' is available
  169. *
  170. */
  171. public function storeToFile()
  172. {
  173. $file = null;
  174. if (isset($this->_attributes['projectProfileFile'])) {
  175. $file = $this->_attributes['projectProfileFile'];
  176. }
  177. if ($file == null) {
  178. // require_once 'Zend/Tool/Project/Exception.php';
  179. throw new Zend_Tool_Project_Exception('storeToFile() must have a "projectProfileFile" attribute set.');
  180. }
  181. $parser = new Zend_Tool_Project_Profile_FileParser_Xml();
  182. $xml = $parser->serialize($this);
  183. file_put_contents($file, $xml);
  184. }
  185. /**
  186. * storeToData() - create a string representation of the profile in memory
  187. *
  188. * @return string
  189. */
  190. public function storeToData()
  191. {
  192. $parser = new Zend_Tool_Project_Profile_FileParser_Xml();
  193. $xml = $parser->serialize($this);
  194. return $xml;
  195. }
  196. /**
  197. * __toString() - cast this profile to string to be able to view it.
  198. *
  199. * @return string
  200. */
  201. public function __toString()
  202. {
  203. $string = '';
  204. foreach ($this as $resource) {
  205. $string .= $resource->getName() . PHP_EOL;
  206. $rii = new RecursiveIteratorIterator($resource, RecursiveIteratorIterator::SELF_FIRST);
  207. foreach ($rii as $item) {
  208. $string .= str_repeat(' ', $rii->getDepth()+1) . $item->getName()
  209. . ((count($attributes = $item->getAttributes()) > 0) ? ' [' . http_build_query($attributes) . ']' : '')
  210. . PHP_EOL;
  211. }
  212. }
  213. return $string;
  214. }
  215. }