|
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-2010 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 20851 2010-02-02 21:45:51Z ralph $ |
|
21 */ |
|
22 |
|
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'; |
|
27 |
|
28 /** |
|
29 * @category Zend |
|
30 * @package Zend_Tool |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_Tool_Project_Profile_FileParser_Xml implements Zend_Tool_Project_Profile_FileParser_Interface |
|
35 { |
|
36 |
|
37 /** |
|
38 * @var Zend_Tool_Project_Profile |
|
39 */ |
|
40 protected $_profile = null; |
|
41 |
|
42 /** |
|
43 * @var Zend_Tool_Project_Context_Repository |
|
44 */ |
|
45 protected $_contextRepository = null; |
|
46 |
|
47 /** |
|
48 * __construct() |
|
49 * |
|
50 */ |
|
51 public function __construct() |
|
52 { |
|
53 $this->_contextRepository = Zend_Tool_Project_Context_Repository::getInstance(); |
|
54 } |
|
55 |
|
56 /** |
|
57 * serialize() |
|
58 * |
|
59 * create an xml string from the provided profile |
|
60 * |
|
61 * @param Zend_Tool_Project_Profile $profile |
|
62 * @return string |
|
63 */ |
|
64 public function serialize(Zend_Tool_Project_Profile $profile) |
|
65 { |
|
66 |
|
67 $profile = clone $profile; |
|
68 |
|
69 $this->_profile = $profile; |
|
70 $xmlElement = new SimpleXMLElement('<projectProfile />'); |
|
71 |
|
72 if ($profile->hasAttribute('type')) { |
|
73 $xmlElement->addAttribute('type', $profile->getAttribute('type')); |
|
74 } |
|
75 |
|
76 if ($profile->hasAttribute('version')) { |
|
77 $xmlElement->addAttribute('version', $profile->getAttribute('version')); |
|
78 } |
|
79 |
|
80 self::_serializeRecurser($profile, $xmlElement); |
|
81 |
|
82 $doc = new DOMDocument('1.0'); |
|
83 $doc->formatOutput = true; |
|
84 $domnode = dom_import_simplexml($xmlElement); |
|
85 $domnode = $doc->importNode($domnode, true); |
|
86 $domnode = $doc->appendChild($domnode); |
|
87 |
|
88 return $doc->saveXML(); |
|
89 } |
|
90 |
|
91 /** |
|
92 * unserialize() |
|
93 * |
|
94 * Create a structure in the object $profile from the structure specficied |
|
95 * in the xml string provided |
|
96 * |
|
97 * @param string xml data |
|
98 * @param Zend_Tool_Project_Profile The profile to use as the top node |
|
99 * @return Zend_Tool_Project_Profile |
|
100 */ |
|
101 public function unserialize($data, Zend_Tool_Project_Profile $profile) |
|
102 { |
|
103 if ($data == null) { |
|
104 throw new Exception('contents not available to unserialize.'); |
|
105 } |
|
106 |
|
107 $this->_profile = $profile; |
|
108 |
|
109 $xmlDataIterator = new SimpleXMLIterator($data); |
|
110 |
|
111 if ($xmlDataIterator->getName() != 'projectProfile') { |
|
112 throw new Exception('Profiles must start with a projectProfile node'); |
|
113 } |
|
114 |
|
115 if (isset($xmlDataIterator['type'])) { |
|
116 $this->_profile->setAttribute('type', (string) $xmlDataIterator['type']); |
|
117 } |
|
118 |
|
119 if (isset($xmlDataIterator['version'])) { |
|
120 $this->_profile->setAttribute('version', (string) $xmlDataIterator['version']); |
|
121 } |
|
122 |
|
123 // start un-serialization of the xml doc |
|
124 $this->_unserializeRecurser($xmlDataIterator); |
|
125 |
|
126 // contexts should be initialized after the unwinding of the profile structure |
|
127 $this->_lazyLoadContexts(); |
|
128 |
|
129 return $this->_profile; |
|
130 |
|
131 } |
|
132 |
|
133 /** |
|
134 * _serializeRecurser() |
|
135 * |
|
136 * This method will be used to traverse the depths of the structure |
|
137 * when *serializing* an xml structure into a string |
|
138 * |
|
139 * @param array $resources |
|
140 * @param SimpleXmlElement $xmlNode |
|
141 */ |
|
142 protected function _serializeRecurser($resources, SimpleXmlElement $xmlNode) |
|
143 { |
|
144 // @todo find a better way to handle concurrency.. if no clone, _position in node gets messed up |
|
145 //if ($resources instanceof Zend_Tool_Project_Profile_Resource) { |
|
146 // $resources = clone $resources; |
|
147 //} |
|
148 |
|
149 foreach ($resources as $resource) { |
|
150 |
|
151 if ($resource->isDeleted()) { |
|
152 continue; |
|
153 } |
|
154 |
|
155 $resourceName = $resource->getContext()->getName(); |
|
156 $resourceName[0] = strtolower($resourceName[0]); |
|
157 |
|
158 $newNode = $xmlNode->addChild($resourceName); |
|
159 |
|
160 //$reflectionClass = new ReflectionClass($resource->getContext()); |
|
161 |
|
162 if ($resource->isEnabled() == false) { |
|
163 $newNode->addAttribute('enabled', 'false'); |
|
164 } |
|
165 |
|
166 foreach ($resource->getPersistentAttributes() as $paramName => $paramValue) { |
|
167 $newNode->addAttribute($paramName, $paramValue); |
|
168 } |
|
169 |
|
170 if ($resource->hasChildren()) { |
|
171 self::_serializeRecurser($resource, $newNode); |
|
172 } |
|
173 |
|
174 } |
|
175 |
|
176 } |
|
177 |
|
178 |
|
179 /** |
|
180 * _unserializeRecurser() |
|
181 * |
|
182 * This method will be used to traverse the depths of the structure |
|
183 * as needed to *unserialize* the profile from an xmlIterator |
|
184 * |
|
185 * @param SimpleXMLIterator $xmlIterator |
|
186 * @param Zend_Tool_Project_Profile_Resource $resource |
|
187 */ |
|
188 protected function _unserializeRecurser(SimpleXMLIterator $xmlIterator, Zend_Tool_Project_Profile_Resource $resource = null) |
|
189 { |
|
190 |
|
191 foreach ($xmlIterator as $resourceName => $resourceData) { |
|
192 |
|
193 $contextName = $resourceName; |
|
194 $subResource = new Zend_Tool_Project_Profile_Resource($contextName); |
|
195 $subResource->setProfile($this->_profile); |
|
196 |
|
197 if ($resourceAttributes = $resourceData->attributes()) { |
|
198 $attributes = array(); |
|
199 foreach ($resourceAttributes as $attrName => $attrValue) { |
|
200 $attributes[$attrName] = (string) $attrValue; |
|
201 } |
|
202 $subResource->setAttributes($attributes); |
|
203 } |
|
204 |
|
205 if ($resource) { |
|
206 $resource->append($subResource, false); |
|
207 } else { |
|
208 $this->_profile->append($subResource); |
|
209 } |
|
210 |
|
211 if ($this->_contextRepository->isOverwritableContext($contextName) == false) { |
|
212 $subResource->initializeContext(); |
|
213 } |
|
214 |
|
215 if ($xmlIterator->hasChildren()) { |
|
216 self::_unserializeRecurser($xmlIterator->getChildren(), $subResource); |
|
217 } |
|
218 } |
|
219 } |
|
220 |
|
221 /** |
|
222 * _lazyLoadContexts() |
|
223 * |
|
224 * This method will call initializeContext on the resources in a profile |
|
225 * @todo determine if this method belongs inside the profile |
|
226 * |
|
227 */ |
|
228 protected function _lazyLoadContexts() |
|
229 { |
|
230 |
|
231 foreach ($this->_profile as $topResource) { |
|
232 $rii = new RecursiveIteratorIterator($topResource, RecursiveIteratorIterator::SELF_FIRST); |
|
233 foreach ($rii as $resource) { |
|
234 $resource->initializeContext(); |
|
235 } |
|
236 } |
|
237 |
|
238 } |
|
239 |
|
240 } |