/Tool/Project/Profile.php

https://github.com/komola/ZendFramework · PHP · 237 lines · 114 code · 26 blank · 97 comment · 17 complexity · 11636f28d4419dbd9543da88acc906db 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-2011 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_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-2011 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. * Constructor, standard usage would allow the setting of options
  49. *
  50. * @param array $options
  51. * @return bool
  52. */
  53. public function __construct($options = null)
  54. {
  55. if ($options) {
  56. $this->setOptions($options);
  57. }
  58. $this->_topResources = new Zend_Tool_Project_Profile_Resource_Container();
  59. }
  60. /**
  61. * Process options and either set a profile property or
  62. * set a profile 'attribute'
  63. *
  64. * @param array $options
  65. */
  66. public function setOptions(Array $options)
  67. {
  68. $this->setAttributes($options);
  69. }
  70. /**
  71. * getIterator() - reqruied by the RecursiveIterator interface
  72. *
  73. * @return RecursiveIteratorIterator
  74. */
  75. public function getIterator()
  76. {
  77. require_once 'Zend/Tool/Project/Profile/Iterator/EnabledResourceFilter.php';
  78. return new RecursiveIteratorIterator(
  79. new Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter($this),
  80. RecursiveIteratorIterator::SELF_FIRST
  81. );
  82. }
  83. /**
  84. * loadFromData() - Load a profile from data provided by the
  85. * 'profilData' attribute
  86. *
  87. */
  88. public function loadFromData()
  89. {
  90. if (!isset($this->_attributes['profileData'])) {
  91. require_once 'Zend/Tool/Project/Exception.php';
  92. throw new Zend_Tool_Project_Exception('loadFromData() must have "profileData" set.');
  93. }
  94. $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml();
  95. $profileFileParser->unserialize($this->_attributes['profileData'], $this);
  96. $this->rewind();
  97. }
  98. /**
  99. * isLoadableFromFile() - can a profile be loaded from a file
  100. *
  101. * wether or not a profile can be loaded from the
  102. * file in attribute 'projectProfileFile', or from a file named
  103. * '.zfproject.xml' inside a directory in key 'projectDirectory'
  104. *
  105. * @return bool
  106. */
  107. public function isLoadableFromFile()
  108. {
  109. if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) {
  110. return false;
  111. }
  112. if (isset($this->_attributes['projectProfileFile'])) {
  113. $projectProfileFilePath = $this->_attributes['projectProfileFile'];
  114. if (!file_exists($projectProfileFilePath)) {
  115. return false;
  116. }
  117. } else {
  118. $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml';
  119. if (!file_exists($projectProfileFilePath)) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. /**
  126. * loadFromFile() - Load data from file
  127. *
  128. * this attempts to load a project profile file from a variety of locations depending
  129. * on what information the user provided vie $options or attributes, specifically the
  130. * 'projectDirectory' or 'projectProfileFile'
  131. *
  132. */
  133. public function loadFromFile()
  134. {
  135. // if no data is supplied, need either a projectProfileFile or a projectDirectory
  136. if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) {
  137. require_once 'Zend/Tool/Project/Exception.php';
  138. throw new Zend_Tool_Project_Exception('loadFromFile() must have at least "projectProfileFile" or "projectDirectory" set.');
  139. }
  140. if (isset($this->_attributes['projectProfileFile'])) {
  141. $projectProfileFilePath = $this->_attributes['projectProfileFile'];
  142. if (!file_exists($projectProfileFilePath)) {
  143. require_once 'Zend/Tool/Project/Exception.php';
  144. throw new Zend_Tool_Project_Exception('"projectProfileFile" was supplied but file was not found at location ' . $projectProfileFilePath);
  145. }
  146. $this->_attributes['projectDirectory'] = dirname($projectProfileFilePath);
  147. } else {
  148. $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml';
  149. if (!file_exists($projectProfileFilePath)) {
  150. require_once 'Zend/Tool/Project/Exception.php';
  151. throw new Zend_Tool_Project_Exception('"projectDirectory" was supplied but no profile file file was not found at location ' . $projectProfileFilePath);
  152. }
  153. $this->_attributes['projectProfileFile'] = $projectProfileFilePath;
  154. }
  155. $profileData = file_get_contents($projectProfileFilePath);
  156. $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml();
  157. $profileFileParser->unserialize($profileData, $this);
  158. $this->rewind();
  159. }
  160. /**
  161. * storeToFile() - store the current profile to file
  162. *
  163. * This will store the profile in memory to a place on disk determined by the attributes
  164. * available, specifically if the key 'projectProfileFile' is available
  165. *
  166. */
  167. public function storeToFile()
  168. {
  169. $file = null;
  170. if (isset($this->_attributes['projectProfileFile'])) {
  171. $file = $this->_attributes['projectProfileFile'];
  172. }
  173. if ($file == null) {
  174. require_once 'Zend/Tool/Project/Exception.php';
  175. throw new Zend_Tool_Project_Exception('storeToFile() must have a "projectProfileFile" attribute set.');
  176. }
  177. $parser = new Zend_Tool_Project_Profile_FileParser_Xml();
  178. $xml = $parser->serialize($this);
  179. file_put_contents($file, $xml);
  180. }
  181. /**
  182. * storeToData() - create a string representation of the profile in memory
  183. *
  184. * @return string
  185. */
  186. public function storeToData()
  187. {
  188. $parser = new Zend_Tool_Project_Profile_FileParser_Xml();
  189. $xml = $parser->serialize($this);
  190. return $xml;
  191. }
  192. /**
  193. * __toString() - cast this profile to string to be able to view it.
  194. *
  195. * @return string
  196. */
  197. public function __toString()
  198. {
  199. $string = '';
  200. foreach ($this as $resource) {
  201. $string .= $resource->getName() . PHP_EOL;
  202. $rii = new RecursiveIteratorIterator($resource, RecursiveIteratorIterator::SELF_FIRST);
  203. foreach ($rii as $item) {
  204. $string .= str_repeat(' ', $rii->getDepth()+1) . $item->getName()
  205. . ((count($attributes = $item->getAttributes()) > 0) ? ' [' . http_build_query($attributes) . ']' : '')
  206. . PHP_EOL;
  207. }
  208. }
  209. return $string;
  210. }
  211. }