|
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: Profile.php 20851 2010-02-02 21:45:51Z ralph $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Tool_Project_Profile_FileParser_Xml |
|
25 */ |
|
26 require_once 'Zend/Tool/Project/Profile/FileParser/Xml.php'; |
|
27 |
|
28 /** |
|
29 * @see Zend_Tool_Project_Profile_Resource_Container |
|
30 */ |
|
31 require_once 'Zend/Tool/Project/Profile/Resource/Container.php'; |
|
32 |
|
33 /** |
|
34 * This class is the front most class for utilizing Zend_Tool_Project |
|
35 * |
|
36 * A profile is a hierarchical set of resources that keep track of |
|
37 * items within a specific project. |
|
38 * |
|
39 * @category Zend |
|
40 * @package Zend_Tool |
|
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
42 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
43 */ |
|
44 class Zend_Tool_Project_Profile extends Zend_Tool_Project_Profile_Resource_Container |
|
45 { |
|
46 |
|
47 /** |
|
48 * @var bool |
|
49 */ |
|
50 protected static $_traverseEnabled = false; |
|
51 |
|
52 /** |
|
53 * Constructor, standard usage would allow the setting of options |
|
54 * |
|
55 * @param array $options |
|
56 * @return bool |
|
57 */ |
|
58 public function __construct($options = null) |
|
59 { |
|
60 if ($options) { |
|
61 $this->setOptions($options); |
|
62 } |
|
63 |
|
64 $this->_topResources = new Zend_Tool_Project_Profile_Resource_Container(); |
|
65 } |
|
66 |
|
67 /** |
|
68 * Process options and either set a profile property or |
|
69 * set a profile 'attribute' |
|
70 * |
|
71 * @param array $options |
|
72 */ |
|
73 public function setOptions(Array $options) |
|
74 { |
|
75 $this->setAttributes($options); |
|
76 } |
|
77 |
|
78 /** |
|
79 * getIterator() - reqruied by the RecursiveIterator interface |
|
80 * |
|
81 * @return RecursiveIteratorIterator |
|
82 */ |
|
83 public function getIterator() |
|
84 { |
|
85 require_once 'Zend/Tool/Project/Profile/Iterator/EnabledResourceFilter.php'; |
|
86 |
|
87 return new RecursiveIteratorIterator( |
|
88 new Zend_Tool_Project_Profile_Iterator_EnabledResourceFilter($this), |
|
89 RecursiveIteratorIterator::SELF_FIRST |
|
90 ); |
|
91 } |
|
92 |
|
93 /** |
|
94 * loadFromData() - Load a profile from data provided by the |
|
95 * 'profilData' attribute |
|
96 * |
|
97 */ |
|
98 public function loadFromData() |
|
99 { |
|
100 if (!isset($this->_attributes['profileData'])) { |
|
101 require_once 'Zend/Tool/Project/Exception.php'; |
|
102 throw new Zend_Tool_Project_Exception('loadFromData() must have "profileData" set.'); |
|
103 } |
|
104 |
|
105 $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml(); |
|
106 $profileFileParser->unserialize($this->_attributes['profileData'], $this); |
|
107 |
|
108 $this->rewind(); |
|
109 } |
|
110 |
|
111 /** |
|
112 * isLoadableFromFile() - can a profile be loaded from a file |
|
113 * |
|
114 * wether or not a profile can be loaded from the |
|
115 * file in attribute 'projectProfileFile', or from a file named |
|
116 * '.zfproject.xml' inside a directory in key 'projectDirectory' |
|
117 * |
|
118 * @return bool |
|
119 */ |
|
120 public function isLoadableFromFile() |
|
121 { |
|
122 if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) { |
|
123 return false; |
|
124 } |
|
125 |
|
126 if (isset($this->_attributes['projectProfileFile'])) { |
|
127 $projectProfileFilePath = $this->_attributes['projectProfileFile']; |
|
128 if (!file_exists($projectProfileFilePath)) { |
|
129 return false; |
|
130 } |
|
131 } else { |
|
132 $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml'; |
|
133 if (!file_exists($projectProfileFilePath)) { |
|
134 return false; |
|
135 } |
|
136 } |
|
137 |
|
138 return true; |
|
139 } |
|
140 |
|
141 /** |
|
142 * loadFromFile() - Load data from file |
|
143 * |
|
144 * this attempts to load a project profile file from a variety of locations depending |
|
145 * on what information the user provided vie $options or attributes, specifically the |
|
146 * 'projectDirectory' or 'projectProfileFile' |
|
147 * |
|
148 */ |
|
149 public function loadFromFile() |
|
150 { |
|
151 // if no data is supplied, need either a projectProfileFile or a projectDirectory |
|
152 if (!isset($this->_attributes['projectProfileFile']) && !isset($this->_attributes['projectDirectory'])) { |
|
153 require_once 'Zend/Tool/Project/Exception.php'; |
|
154 throw new Zend_Tool_Project_Exception('loadFromFile() must have at least "projectProfileFile" or "projectDirectory" set.'); |
|
155 } |
|
156 |
|
157 if (isset($this->_attributes['projectProfileFile'])) { |
|
158 $projectProfileFilePath = $this->_attributes['projectProfileFile']; |
|
159 if (!file_exists($projectProfileFilePath)) { |
|
160 require_once 'Zend/Tool/Project/Exception.php'; |
|
161 throw new Zend_Tool_Project_Exception('"projectProfileFile" was supplied but file was not found at location ' . $projectProfileFilePath); |
|
162 } |
|
163 $this->_attributes['projectDirectory'] = dirname($projectProfileFilePath); |
|
164 } else { |
|
165 $projectProfileFilePath = rtrim($this->_attributes['projectDirectory'], '/\\') . '/.zfproject.xml'; |
|
166 if (!file_exists($projectProfileFilePath)) { |
|
167 require_once 'Zend/Tool/Project/Exception.php'; |
|
168 throw new Zend_Tool_Project_Exception('"projectDirectory" was supplied but no profile file file was not found at location ' . $projectProfileFilePath); |
|
169 } |
|
170 $this->_attributes['projectProfileFile'] = $projectProfileFilePath; |
|
171 } |
|
172 |
|
173 $profileData = file_get_contents($projectProfileFilePath); |
|
174 |
|
175 $profileFileParser = new Zend_Tool_Project_Profile_FileParser_Xml(); |
|
176 $profileFileParser->unserialize($profileData, $this); |
|
177 |
|
178 $this->rewind(); |
|
179 } |
|
180 |
|
181 /** |
|
182 * storeToFile() - store the current profile to file |
|
183 * |
|
184 * This will store the profile in memory to a place on disk determined by the attributes |
|
185 * available, specifically if the key 'projectProfileFile' is available |
|
186 * |
|
187 */ |
|
188 public function storeToFile() |
|
189 { |
|
190 $file = null; |
|
191 |
|
192 if (isset($this->_attributes['projectProfileFile'])) { |
|
193 $file = $this->_attributes['projectProfileFile']; |
|
194 } |
|
195 |
|
196 if ($file == null) { |
|
197 require_once 'Zend/Tool/Project/Exception.php'; |
|
198 throw new Zend_Tool_Project_Exception('storeToFile() must have a "projectProfileFile" attribute set.'); |
|
199 } |
|
200 |
|
201 $parser = new Zend_Tool_Project_Profile_FileParser_Xml(); |
|
202 $xml = $parser->serialize($this); |
|
203 file_put_contents($file, $xml); |
|
204 } |
|
205 |
|
206 /** |
|
207 * storeToData() - create a string representation of the profile in memory |
|
208 * |
|
209 * @return string |
|
210 */ |
|
211 public function storeToData() |
|
212 { |
|
213 $parser = new Zend_Tool_Project_Profile_FileParser_Xml(); |
|
214 $xml = $parser->serialize($this); |
|
215 return $xml; |
|
216 } |
|
217 |
|
218 /** |
|
219 * __toString() - cast this profile to string to be able to view it. |
|
220 * |
|
221 * @return string |
|
222 */ |
|
223 public function __toString() |
|
224 { |
|
225 $string = ''; |
|
226 foreach ($this as $resource) { |
|
227 $string .= $resource->getName() . PHP_EOL; |
|
228 $rii = new RecursiveIteratorIterator($resource, RecursiveIteratorIterator::SELF_FIRST); |
|
229 foreach ($rii as $item) { |
|
230 $string .= str_repeat(' ', $rii->getDepth()+1) . $item->getName() |
|
231 . ((count($attributes = $item->getAttributes()) > 0) ? ' [' . http_build_query($attributes) . ']' : '') |
|
232 . PHP_EOL; |
|
233 } |
|
234 } |
|
235 return $string; |
|
236 } |
|
237 } |