Xml.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. require_once 'Zend/Tool/Project/Profile/FileParser/Interface.php';
  23. require_once 'Zend/Tool/Project/Context/Repository.php';
  24. require_once 'Zend/Tool/Project/Profile.php';
  25. require_once 'Zend/Tool/Project/Profile/Resource.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Tool
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Tool_Project_Profile_FileParser_Xml implements Zend_Tool_Project_Profile_FileParser_Interface
  33. {
  34. /**
  35. * @var Zend_Tool_Project_Profile
  36. */
  37. protected $_profile = null;
  38. /**
  39. * @var Zend_Tool_Project_Context_Repository
  40. */
  41. protected $_contextRepository = null;
  42. /**
  43. * __construct()
  44. *
  45. */
  46. public function __construct()
  47. {
  48. $this->_contextRepository = Zend_Tool_Project_Context_Repository::getInstance();
  49. }
  50. /**
  51. * serialize()
  52. *
  53. * create an xml string from the provided profile
  54. *
  55. * @param Zend_Tool_Project_Profile $profile
  56. * @return string
  57. */
  58. public function serialize(Zend_Tool_Project_Profile $profile)
  59. {
  60. $profile = clone $profile;
  61. $this->_profile = $profile;
  62. $xmlElement = new SimpleXMLElement('<projectProfile />');
  63. self::_serializeRecurser($profile, $xmlElement);
  64. $doc = new DOMDocument('1.0');
  65. $doc->formatOutput = true;
  66. $domnode = dom_import_simplexml($xmlElement);
  67. $domnode = $doc->importNode($domnode, true);
  68. $domnode = $doc->appendChild($domnode);
  69. return $doc->saveXML();
  70. }
  71. /**
  72. * unserialize()
  73. *
  74. * Create a structure in the object $profile from the structure specficied
  75. * in the xml string provided
  76. *
  77. * @param string xml data
  78. * @param Zend_Tool_Project_Profile The profile to use as the top node
  79. * @return Zend_Tool_Project_Profile
  80. */
  81. public function unserialize($data, Zend_Tool_Project_Profile $profile)
  82. {
  83. if ($data == null) {
  84. throw new Exception('contents not available to unserialize.');
  85. }
  86. $this->_profile = $profile;
  87. $xmlDataIterator = new SimpleXMLIterator($data);
  88. if ($xmlDataIterator->getName() != 'projectProfile') {
  89. throw new Exception('Profiles must start with a projectProfile node');
  90. }
  91. $this->_unserializeRecurser($xmlDataIterator);
  92. $this->_lazyLoadContexts();
  93. return $this->_profile;
  94. }
  95. /**
  96. * _serializeRecurser()
  97. *
  98. * This method will be used to traverse the depths of the structure
  99. * when *serializing* an xml structure into a string
  100. *
  101. * @param array $resources
  102. * @param SimpleXmlElement $xmlNode
  103. */
  104. protected function _serializeRecurser($resources, SimpleXmlElement $xmlNode)
  105. {
  106. // @todo find a better way to handle concurrency.. if no clone, _position in node gets messed up
  107. //if ($resources instanceof Zend_Tool_Project_Profile_Resource) {
  108. // $resources = clone $resources;
  109. //}
  110. foreach ($resources as $resource) {
  111. if ($resource->isDeleted()) {
  112. continue;
  113. }
  114. $resourceName = $resource->getContext()->getName();
  115. $resourceName[0] = strtolower($resourceName[0]);
  116. $newNode = $xmlNode->addChild($resourceName);
  117. //$reflectionClass = new ReflectionClass($resource->getContext());
  118. if ($resource->isEnabled() == false) {
  119. $newNode->addAttribute('enabled', 'false');
  120. }
  121. foreach ($resource->getPersistentAttributes() as $paramName => $paramValue) {
  122. $newNode->addAttribute($paramName, $paramValue);
  123. }
  124. if ($resource->hasChildren()) {
  125. self::_serializeRecurser($resource, $newNode);
  126. }
  127. }
  128. }
  129. /**
  130. * _unserializeRecurser()
  131. *
  132. * This method will be used to traverse the depths of the structure
  133. * as needed to *unserialize* the profile from an xmlIterator
  134. *
  135. * @param SimpleXMLIterator $xmlIterator
  136. * @param Zend_Tool_Project_Profile_Resource $resource
  137. */
  138. protected function _unserializeRecurser(SimpleXMLIterator $xmlIterator, Zend_Tool_Project_Profile_Resource $resource = null)
  139. {
  140. foreach ($xmlIterator as $resourceName => $resourceData) {
  141. $contextName = $resourceName;
  142. $subResource = new Zend_Tool_Project_Profile_Resource($contextName);
  143. $subResource->setProfile($this->_profile);
  144. if ($resourceAttributes = $resourceData->attributes()) {
  145. $attributes = array();
  146. foreach ($resourceAttributes as $attrName => $attrValue) {
  147. $attributes[$attrName] = (string) $attrValue;
  148. }
  149. $subResource->setAttributes($attributes);
  150. }
  151. if ($resource) {
  152. $resource->append($subResource, false);
  153. } else {
  154. $this->_profile->append($subResource);
  155. }
  156. if ($this->_contextRepository->isOverwritableContext($contextName) == false) {
  157. $subResource->initializeContext();
  158. }
  159. if ($xmlIterator->hasChildren()) {
  160. self::_unserializeRecurser($xmlIterator->getChildren(), $subResource);
  161. }
  162. }
  163. }
  164. /**
  165. * _lazyLoadContexts()
  166. *
  167. * This method will call initializeContext on the resources in a profile
  168. * @todo determine if this method belongs inside the profile
  169. *
  170. */
  171. protected function _lazyLoadContexts()
  172. {
  173. foreach ($this->_profile as $topResource) {
  174. $rii = new RecursiveIteratorIterator($topResource, RecursiveIteratorIterator::SELF_FIRST);
  175. foreach ($rii as $resource) {
  176. $resource->initializeContext();
  177. }
  178. }
  179. }
  180. }