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.
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: Xml.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 require_once 'Zend/Tool/Project/Profile/FileParser/Interface.php';
24 require_once 'Zend/Tool/Project/Context/Repository.php';
25 require_once 'Zend/Tool/Project/Profile.php';
26 require_once 'Zend/Tool/Project/Profile/Resource.php';
31 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Tool_Project_Profile_FileParser_Xml
implements Zend_Tool_Project_Profile_FileParser_Interface
38 * @var Zend_Tool_Project_Profile
40 protected $_profile = null;
43 * @var Zend_Tool_Project_Context_Repository
45 protected $_contextRepository = null;
51 public function __construct()
53 $this->_contextRepository
= Zend_Tool_Project_Context_Repository
::getInstance();
59 * create an xml string from the provided profile
61 * @param Zend_Tool_Project_Profile $profile
64 public function serialize(Zend_Tool_Project_Profile
$profile)
67 $profile = clone $profile;
69 $this->_profile
= $profile;
70 $xmlElement = new SimpleXMLElement('<projectProfile />');
72 self
::_serializeRecurser($profile, $xmlElement);
74 $doc = new DOMDocument('1.0');
75 $doc->formatOutput
= true;
76 $domnode = dom_import_simplexml($xmlElement);
77 $domnode = $doc->importNode($domnode, true);
78 $domnode = $doc->appendChild($domnode);
80 return $doc->saveXML();
86 * Create a structure in the object $profile from the structure specficied
87 * in the xml string provided
89 * @param string xml data
90 * @param Zend_Tool_Project_Profile The profile to use as the top node
91 * @return Zend_Tool_Project_Profile
93 public function unserialize($data, Zend_Tool_Project_Profile
$profile)
96 throw new Exception('contents not available to unserialize.');
99 $this->_profile
= $profile;
101 $xmlDataIterator = new SimpleXMLIterator($data);
103 if ($xmlDataIterator->getName() != 'projectProfile') {
104 throw new Exception('Profiles must start with a projectProfile node');
108 $this->_unserializeRecurser($xmlDataIterator);
110 $this->_lazyLoadContexts();
112 return $this->_profile
;
117 * _serializeRecurser()
119 * This method will be used to traverse the depths of the structure
120 * when *serializing* an xml structure into a string
122 * @param array $resources
123 * @param SimpleXmlElement $xmlNode
125 protected function _serializeRecurser($resources, SimpleXmlElement
$xmlNode)
127 // @todo find a better way to handle concurrency.. if no clone, _position in node gets messed up
128 //if ($resources instanceof Zend_Tool_Project_Profile_Resource) {
129 // $resources = clone $resources;
132 foreach ($resources as $resource) {
134 if ($resource->isDeleted()) {
138 $resourceName = $resource->getContext()->getName();
139 $resourceName[0] = strtolower($resourceName[0]);
141 $newNode = $xmlNode->addChild($resourceName);
143 //$reflectionClass = new ReflectionClass($resource->getContext());
145 if ($resource->isEnabled() == false) {
146 $newNode->addAttribute('enabled', 'false');
149 foreach ($resource->getPersistentAttributes() as $paramName => $paramValue) {
150 $newNode->addAttribute($paramName, $paramValue);
153 if ($resource->hasChildren()) {
154 self
::_serializeRecurser($resource, $newNode);
163 * _unserializeRecurser()
165 * This method will be used to traverse the depths of the structure
166 * as needed to *unserialize* the profile from an xmlIterator
168 * @param SimpleXMLIterator $xmlIterator
169 * @param Zend_Tool_Project_Profile_Resource $resource
171 protected function _unserializeRecurser(SimpleXMLIterator
$xmlIterator, Zend_Tool_Project_Profile_Resource
$resource = null)
174 foreach ($xmlIterator as $resourceName => $resourceData) {
176 $contextName = $resourceName;
177 $subResource = new Zend_Tool_Project_Profile_Resource($contextName);
178 $subResource->setProfile($this->_profile
);
180 if ($resourceAttributes = $resourceData->attributes()) {
181 $attributes = array();
182 foreach ($resourceAttributes as $attrName => $attrValue) {
183 $attributes[$attrName] = (string) $attrValue;
185 $subResource->setAttributes($attributes);
189 $resource->append($subResource, false);
191 $this->_profile
->append($subResource);
194 if ($this->_contextRepository
->isOverwritableContext($contextName) == false) {
195 $subResource->initializeContext();
198 if ($xmlIterator->hasChildren()) {
199 self
::_unserializeRecurser($xmlIterator->getChildren(), $subResource);
205 * _lazyLoadContexts()
207 * This method will call initializeContext on the resources in a profile
208 * @todo determine if this method belongs inside the profile
211 protected function _lazyLoadContexts()
214 foreach ($this->_profile
as $topResource) {
215 $rii = new RecursiveIteratorIterator($topResource, RecursiveIteratorIterator
::SELF_FIRST
);
216 foreach ($rii as $resource) {
217 $resource->initializeContext();