Xml.php 7.3 KB

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