1 |
<?php
|
2 |
/*
|
3 |
* $Id: Lib.php 3189 2007-11-18 20:37:44Z meus $
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Lib has not commonly used static functions, mostly for debugging purposes
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage Lib
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 3189 $
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
*/
|
33 |
class Doctrine_Lib
|
34 |
{
|
35 |
/**
|
36 |
* @param integer $state the state of record
|
37 |
* @see Doctrine_Record::STATE_* constants
|
38 |
* @return string string representation of given state
|
39 |
*/
|
40 |
public static function getRecordStateAsString($state)
|
41 |
{
|
42 |
switch ($state) {
|
43 |
case Doctrine_Record::STATE_PROXY:
|
44 |
return "proxy";
|
45 |
break;
|
46 |
case Doctrine_Record::STATE_CLEAN:
|
47 |
return "persistent clean";
|
48 |
break;
|
49 |
case Doctrine_Record::STATE_DIRTY:
|
50 |
return "persistent dirty";
|
51 |
break;
|
52 |
case Doctrine_Record::STATE_TDIRTY:
|
53 |
return "transient dirty";
|
54 |
break;
|
55 |
case Doctrine_Record::STATE_TCLEAN:
|
56 |
return "transient clean";
|
57 |
break;
|
58 |
}
|
59 |
}
|
60 |
|
61 |
/**
|
62 |
* returns a string representation of Doctrine_Record object
|
63 |
* @param Doctrine_Record $record
|
64 |
* @return string
|
65 |
*/
|
66 |
public static function getRecordAsString(Doctrine_Record $record)
|
67 |
{
|
68 |
$r[] = '<pre>';
|
69 |
$r[] = 'Component : ' . $record->getTable()->getComponentName();
|
70 |
$r[] = 'ID : ' . $record->obtainIdentifier();
|
71 |
$r[] = 'References : ' . count($record->getReferences());
|
72 |
$r[] = 'State : ' . Doctrine_Lib::getRecordStateAsString($record->getState());
|
73 |
$r[] = 'OID : ' . $record->getOID();
|
74 |
$r[] = 'data : ' . Doctrine::dump($record->getData(), false);
|
75 |
$r[] = '</pre>';
|
76 |
return implode("\n",$r)."<br />";
|
77 |
}
|
78 |
|
79 |
/**
|
80 |
* Return an collection of records as XML.
|
81 |
*
|
82 |
* @see getRecordAsXml for options to set in the record class to control this.
|
83 |
*
|
84 |
* @param Doctrine_Collection $collection
|
85 |
* @param SimpleXMLElement $xml
|
86 |
* @return string Xml as string
|
87 |
*/
|
88 |
public static function getCollectionAsXml(Doctrine_Collection $collection, SimpleXMLElement $incomming_xml = null) {
|
89 |
|
90 |
$collectionName = Doctrine_Lib::plurelize($collection->getTable()->tableName);
|
91 |
if ( $collection->count() != 0) {
|
92 |
$record = $collection[0];
|
93 |
$xml_options = $record->option("xml");
|
94 |
if ( isset($xml_options["collection_name"])) {
|
95 |
$collectionName = $xml_options["collection_name"];
|
96 |
}
|
97 |
}
|
98 |
|
99 |
if ( ! isset($incomming_xml)) {
|
100 |
$new_xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><" . $collectionName . "></" . $collectionName . ">";
|
101 |
$xml = new SimpleXMLElement($new_xml_string);
|
102 |
} else {
|
103 |
$xml = $incomming_xml->addChild($collectionName);
|
104 |
}
|
105 |
foreach ($collection as $key => $record) {
|
106 |
Doctrine_Lib::getRecordAsXml($record, $xml);
|
107 |
}
|
108 |
return $xml->asXML();
|
109 |
}
|
110 |
|
111 |
public static function plurelize($string) {
|
112 |
return $string . "s";
|
113 |
}
|
114 |
|
115 |
/**
|
116 |
* Return a recrd as XML.
|
117 |
*
|
118 |
* In order to control how this is done set the "xml" option in a record.
|
119 |
* This option is an array that has the keys "ignore_fields" and "include_relations". Both of these are arrays that list the name of fields/relations to include/process.
|
120 |
*
|
121 |
* If you want to insert this xml as a part inside another xml send a
|
122 |
* SimpleXMLElement to the function. Because of the nature of SimpleXML the
|
123 |
* content you add to this element will be avilable after the function is
|
124 |
* complete.
|
125 |
*
|
126 |
* @param Doctrine_Record $record
|
127 |
* @param SimpleXMLElement $xml
|
128 |
* @return string Xml as string
|
129 |
*/
|
130 |
public static function getRecordAsXml(Doctrine_Record $record, SimpleXMlElement $incomming_xml = NULL)
|
131 |
{
|
132 |
$recordname = $record->getTable()->tableName;
|
133 |
if ( !isset($incomming_xml)) {
|
134 |
$new_xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><" . $recordname . "></" . $recordname . ">";
|
135 |
$xml = new SimpleXMLElement($new_xml_string);
|
136 |
} else {
|
137 |
$xml = $incomming_xml->addChild($recordname);
|
138 |
}
|
139 |
$xml_options = $record->option("xml");
|
140 |
if ( isset($xml_options["record_name"])) {
|
141 |
$recordname = $xml_options["record_name"];
|
142 |
}
|
143 |
foreach ($record->getData() as $field => $value) {
|
144 |
if ((isset($xml_options["ignore_fields"]) && !in_array($field, $xml_options["ignore_fields"])) || !isset($xml_options["ignore_fields"])) {
|
145 |
if ($value instanceOf Doctrine_Null) {
|
146 |
$xml->addChild($field);
|
147 |
} else {
|
148 |
$xml->addChild($field, $value);
|
149 |
}
|
150 |
}
|
151 |
}
|
152 |
if ( ! isset($xml_options["include_relations"])) {
|
153 |
return $xml->asXML();
|
154 |
}
|
155 |
$relations = $record->getTable()->getRelations();
|
156 |
foreach ($relations as $name => $relation) {
|
157 |
if (in_array($name, $xml_options["include_relations"])) {
|
158 |
$relation_type = $relation->getType();
|
159 |
$related_records = $record->get($name);
|
160 |
if ($relation_type == Doctrine_Relation::ONE && $related_records instanceOf Doctrine_Record) {
|
161 |
Doctrine_Lib::getRecordAsXml($related_records, $xml);
|
162 |
} else {
|
163 |
Doctrine_Lib::getCollectionAsXml($related_records, $xml);
|
164 |
}
|
165 |
}
|
166 |
}
|
167 |
return $xml->asXML();
|
168 |
}
|
169 |
|
170 |
|
171 |
/**
|
172 |
* getStateAsString
|
173 |
* returns a given connection state as string
|
174 |
* @param integer $state connection state
|
175 |
*/
|
176 |
public static function getConnectionStateAsString($state)
|
177 |
{
|
178 |
switch ($state) {
|
179 |
case Doctrine_Transaction::STATE_SLEEP:
|
180 |
return "open";
|
181 |
break;
|
182 |
case Doctrine_Transaction::STATE_BUSY:
|
183 |
return "busy";
|
184 |
break;
|
185 |
case Doctrine_Transaction::STATE_ACTIVE:
|
186 |
return "active";
|
187 |
break;
|
188 |
}
|
189 |
}
|
190 |
|
191 |
/**
|
192 |
* returns a string representation of Doctrine_Connection object
|
193 |
* @param Doctrine_Connection $connection
|
194 |
* @return string
|
195 |
*/
|
196 |
public static function getConnectionAsString(Doctrine_Connection $connection)
|
197 |
{
|
198 |
$r[] = '<pre>';
|
199 |
$r[] = 'Doctrine_Connection object';
|
200 |
$r[] = 'State : ' . Doctrine_Lib::getConnectionStateAsString($connection->transaction->getState());
|
201 |
$r[] = 'Open Transactions : ' . $connection->transaction->getTransactionLevel();
|
202 |
$r[] = 'Table in memory : ' . $connection->count();
|
203 |
$r[] = 'Driver name : ' . $connection->getAttribute(Doctrine::ATTR_DRIVER_NAME);
|
204 |
|
205 |
$r[] = "</pre>";
|
206 |
return implode("\n",$r)."<br>";
|
207 |
}
|
208 |
|
209 |
/**
|
210 |
* returns a string representation of Doctrine_Table object
|
211 |
* @param Doctrine_Table $table
|
212 |
* @return string
|
213 |
*/
|
214 |
public static function getTableAsString(Doctrine_Table $table)
|
215 |
{
|
216 |
$r[] = "<pre>";
|
217 |
$r[] = "Component : ".$table->getComponentName();
|
218 |
$r[] = "Table : ".$table->getTableName();
|
219 |
$r[] = "</pre>";
|
220 |
return implode("\n",$r)."<br>";
|
221 |
}
|
222 |
|
223 |
/**
|
224 |
* formatSql
|
225 |
*
|
226 |
* @todo: What about creating a config varialbe for the color?
|
227 |
* @param mixed $sql
|
228 |
* @return string the formated sql
|
229 |
*/
|
230 |
public static function formatSql($sql)
|
231 |
{
|
232 |
$e = explode("\n",$sql);
|
233 |
$color = "367FAC";
|
234 |
$l = $sql;
|
235 |
$l = str_replace("SELECT ", "<font color='$color'><b>SELECT </b></font><br \> ",$l);
|
236 |
$l = str_replace("FROM ", "<font color='$color'><b>FROM </b></font><br \>",$l);
|
237 |
$l = str_replace(" LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
|
238 |
$l = str_replace(" INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
|
239 |
$l = str_replace(" WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
|
240 |
$l = str_replace(" GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
|
241 |
$l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
|
242 |
$l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \> ",$l);
|
243 |
$l = str_replace(" ON ", "<font color='$color'><b> ON </b></font>",$l);
|
244 |
$l = str_replace(" ORDER BY ", "<font color='$color'><b> ORDER BY </b></font><br \>",$l);
|
245 |
$l = str_replace(" LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
|
246 |
$l = str_replace(" OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
|
247 |
$l = str_replace(" ", "<dd>",$l);
|
248 |
|
249 |
return $l;
|
250 |
}
|
251 |
|
252 |
/**
|
253 |
* returns a string representation of Doctrine_Collection object
|
254 |
* @param Doctrine_Collection $collection
|
255 |
* @return string
|
256 |
*/
|
257 |
public static function getCollectionAsString(Doctrine_Collection $collection)
|
258 |
{
|
259 |
$r[] = "<pre>";
|
260 |
$r[] = get_class($collection);
|
261 |
$r[] = 'data : ' . Doctrine::dump($collection->getData(), false);
|
262 |
//$r[] = 'snapshot : ' . Doctrine::dump($collection->getSnapshot());
|
263 |
|
264 |
$r[] = "</pre>";
|
265 |
return implode("\n",$r);
|
266 |
}
|
267 |
}
|