Xml.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // $xmlElement->addAttribute('version', $profile->getVersion());
  64. // $xmlElement->addAttribute('type', $profile->getType());
  65. self::_serializeRecurser($profile, $xmlElement);
  66. $doc = new DOMDocument('1.0');
  67. $doc->formatOutput = true;
  68. $domnode = dom_import_simplexml($xmlElement);
  69. $domnode = $doc->importNode($domnode, true);
  70. $domnode = $doc->appendChild($domnode);
  71. return $doc->saveXML();
  72. }
  73. /**
  74. * unserialize()
  75. *
  76. * Create a structure in the object $profile from the structure specficied
  77. * in the xml string provided
  78. *
  79. * @param string xml data
  80. * @param Zend_Tool_Project_Profile The profile to use as the top node
  81. * @return Zend_Tool_Project_Profile
  82. */
  83. public function unserialize($data, Zend_Tool_Project_Profile $profile)
  84. {
  85. if ($data == null) {
  86. throw new Exception('contents not available to unserialize.');
  87. }
  88. $this->_profile = $profile;
  89. $xmlDataIterator = new SimpleXMLIterator($data);
  90. if ($xmlDataIterator->getName() != 'projectProfile') {
  91. throw new Exception('Profiles must start with a projectProfile node');
  92. }
  93. $this->_unserializeRecurser($xmlDataIterator);
  94. $this->_lazyLoadContexts();
  95. return $this->_profile;
  96. }
  97. /**
  98. * _serializeRecurser()
  99. *
  100. * This method will be used to traverse the depths of the structure
  101. * when *serializing* an xml structure into a string
  102. *
  103. * @param array $resources
  104. * @param SimpleXmlElement $xmlNode
  105. */
  106. protected function _serializeRecurser($resources, SimpleXmlElement $xmlNode)
  107. {
  108. // @todo find a better way to handle concurrency.. if no clone, _position in node gets messed up
  109. //if ($resources instanceof Zend_Tool_Project_Profile_Resource) {
  110. // $resources = clone $resources;
  111. //}
  112. foreach ($resources as $resource) {
  113. if ($resource->isDeleted()) {
  114. continue;
  115. }
  116. $resourceName = $resource->getContext()->getName();
  117. $resourceName[0] = strtolower($resourceName[0]);
  118. $newNode = $xmlNode->addChild($resourceName);
  119. //$reflectionClass = new ReflectionClass($resource->getContext());
  120. if ($resource->isEnabled() == false) {
  121. $newNode->addAttribute('enabled', 'false');
  122. }
  123. foreach ($resource->getPersistentAttributes() as $paramName => $paramValue) {
  124. $newNode->addAttribute($paramName, $paramValue);
  125. }
  126. if ($resource->hasChildren()) {
  127. self::_serializeRecurser($resource, $newNode);
  128. }
  129. }
  130. }
  131. /**
  132. * _unserializeRecurser()
  133. *
  134. * This method will be used to traverse the depths of the structure
  135. * as needed to *unserialize* the profile from an xmlIterator
  136. *
  137. * @param SimpleXMLIterator $xmlIterator
  138. * @param Zend_Tool_Project_Profile_Resource $resource
  139. */
  140. protected function _unserializeRecurser(SimpleXMLIterator $xmlIterator, Zend_Tool_Project_Profile_Resource $resource = null)
  141. {
  142. foreach ($xmlIterator as $resourceName => $resourceData) {
  143. $contextName = $resourceName;
  144. $subResource = new Zend_Tool_Project_Profile_Resource($contextName);
  145. $subResource->setProfile($this->_profile);
  146. if ($resourceAttributes = $resourceData->attributes()) {
  147. $attributes = array();
  148. foreach ($resourceAttributes as $attrName => $attrValue) {
  149. $attributes[$attrName] = (string) $attrValue;
  150. }
  151. $subResource->setAttributes($attributes);
  152. }
  153. if ($resource) {
  154. $resource->append($subResource, false);
  155. } else {
  156. $this->_profile->append($subResource);
  157. }
  158. if ($this->_contextRepository->isOverwritableContext($contextName) == false) {
  159. $subResource->initializeContext();
  160. }
  161. if ($xmlIterator->hasChildren()) {
  162. self::_unserializeRecurser($xmlIterator->getChildren(), $subResource);
  163. }
  164. }
  165. }
  166. /**
  167. * _lazyLoadContexts()
  168. *
  169. * This method will call initializeContext on the resources in a profile
  170. * @todo determine if this method belongs inside the profile
  171. *
  172. */
  173. protected function _lazyLoadContexts()
  174. {
  175. foreach ($this->_profile as $topResource) {
  176. $rii = new RecursiveIteratorIterator($topResource, RecursiveIteratorIterator::SELF_FIRST);
  177. foreach ($rii as $resource) {
  178. $resource->initializeContext();
  179. }
  180. }
  181. }
  182. }