PageRenderTime 41ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/urlcatcher.org/htdocs/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Lib.php

https://github.com/bigcalm/urlcatcher
PHP | 388 lines | 194 code | 31 blank | 163 comment | 38 complexity | 3b0b1ef1d60b9ef218d2c6d54e03175e MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: Lib.php 6484 2009-10-12 17:40:41Z jwage $
  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.org>.
  20. */
  21. /**
  22. * Doctrine_Lib has not commonly used static functions, mostly for debugging purposes
  23. *
  24. * @package Doctrine
  25. * @subpackage Lib
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.phpdoctrine.org
  28. * @since 1.0
  29. * @version $Revision: 6484 $
  30. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  31. */
  32. class Doctrine_Lib
  33. {
  34. /**
  35. * Generates a human readable representation of a record's state.
  36. *
  37. * This method translates a Doctrine_Record state (integer constant)
  38. * in an english string.
  39. * @see Doctrine_Record::STATE_* constants
  40. *
  41. * @param integer $state the state of record
  42. * @return string description of given state
  43. */
  44. public static function getRecordStateAsString($state)
  45. {
  46. switch ($state) {
  47. case Doctrine_Record::STATE_PROXY:
  48. return "proxy";
  49. break;
  50. case Doctrine_Record::STATE_CLEAN:
  51. return "persistent clean";
  52. break;
  53. case Doctrine_Record::STATE_DIRTY:
  54. return "persistent dirty";
  55. break;
  56. case Doctrine_Record::STATE_TDIRTY:
  57. return "transient dirty";
  58. break;
  59. case Doctrine_Record::STATE_TCLEAN:
  60. return "transient clean";
  61. break;
  62. }
  63. }
  64. /**
  65. * Dumps a record.
  66. *
  67. * This method returns an html representation of a given
  68. * record, containing keys, state and data.
  69. *
  70. * @param Doctrine_Record $record
  71. * @return string
  72. */
  73. public static function getRecordAsString(Doctrine_Record $record)
  74. {
  75. $r[] = '<pre>';
  76. $r[] = 'Component : ' . $record->getTable()->getComponentName();
  77. $r[] = 'ID : ' . Doctrine_Core::dump($record->identifier());
  78. $r[] = 'References : ' . count($record->getReferences());
  79. $r[] = 'State : ' . Doctrine_Lib::getRecordStateAsString($record->state());
  80. $r[] = 'OID : ' . $record->getOID();
  81. $r[] = 'data : ' . Doctrine_Core::dump($record->getData(), false);
  82. $r[] = '</pre>';
  83. return implode("\n",$r)."<br />";
  84. }
  85. /**
  86. * Generates a human readable representation of a connection's state.
  87. *
  88. * This method translates a Doctrine_Connection state (integer constant)
  89. * in a english description.
  90. * @see Doctrine_Transaction::STATE_* constants
  91. * @param integer $state state of the connection as a string
  92. * @return string
  93. */
  94. public static function getConnectionStateAsString($state)
  95. {
  96. switch ($state) {
  97. case Doctrine_Transaction::STATE_SLEEP:
  98. return "open";
  99. break;
  100. case Doctrine_Transaction::STATE_BUSY:
  101. return "busy";
  102. break;
  103. case Doctrine_Transaction::STATE_ACTIVE:
  104. return "active";
  105. break;
  106. }
  107. }
  108. /**
  109. * Generates a string representation of a connection.
  110. *
  111. * This method returns an html dump of a connection, containing state, open
  112. * transactions and loaded tables.
  113. *
  114. * @param Doctrine_Connection $connection
  115. * @return string
  116. */
  117. public static function getConnectionAsString(Doctrine_Connection $connection)
  118. {
  119. $r[] = '<pre>';
  120. $r[] = 'Doctrine_Connection object';
  121. $r[] = 'State : ' . Doctrine_Lib::getConnectionStateAsString($connection->transaction->getState());
  122. $r[] = 'Open Transactions : ' . $connection->transaction->getTransactionLevel();
  123. $r[] = 'Table in memory : ' . $connection->count();
  124. $r[] = 'Driver name : ' . $connection->getAttribute(Doctrine_Core::ATTR_DRIVER_NAME);
  125. $r[] = "</pre>";
  126. return implode("\n",$r)."<br>";
  127. }
  128. /**
  129. * Generates a string representation of a table.
  130. *
  131. * This method returns an html dump of a table, containing component name
  132. * and table physical name.
  133. * @param Doctrine_Table $table
  134. * @return string
  135. */
  136. public static function getTableAsString(Doctrine_Table $table)
  137. {
  138. $r[] = "<pre>";
  139. $r[] = "Component : ".$table->getComponentName();
  140. $r[] = "Table : ".$table->getTableName();
  141. $r[] = "</pre>";
  142. return implode("\n",$r)."<br>";
  143. }
  144. /**
  145. * Generates a colored sql query.
  146. *
  147. * This methods parses a plain text query and generates the html needed
  148. * for visual formatting.
  149. *
  150. * @todo: What about creating a config varialbe for the color?
  151. * @param string $sql plain text query
  152. * @return string the formatted sql code
  153. */
  154. public static function formatSql($sql)
  155. {
  156. $e = explode("\n",$sql);
  157. $color = "367FAC";
  158. $l = $sql;
  159. $l = str_replace("SELECT ", "<font color='$color'><b>SELECT </b></font><br \> ",$l);
  160. $l = str_replace("FROM ", "<font color='$color'><b>FROM </b></font><br \>",$l);
  161. $l = str_replace(" LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
  162. $l = str_replace(" INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
  163. $l = str_replace(" WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
  164. $l = str_replace(" GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
  165. $l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
  166. $l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \> ",$l);
  167. $l = str_replace(" ON ", "<font color='$color'><b> ON </b></font>",$l);
  168. $l = str_replace(" ORDER BY ", "<font color='$color'><b> ORDER BY </b></font><br \>",$l);
  169. $l = str_replace(" LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
  170. $l = str_replace(" OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
  171. $l = str_replace(" ", "<dd>",$l);
  172. return $l;
  173. }
  174. /**
  175. * Generates a string representation of a collection.
  176. *
  177. * This method returns an html dump of a collection of records, containing
  178. * all data.
  179. *
  180. * @param Doctrine_Collection $collection
  181. * @return string
  182. */
  183. public static function getCollectionAsString(Doctrine_Collection $collection)
  184. {
  185. $r[] = "<pre>";
  186. $r[] = get_class($collection);
  187. $r[] = 'data : ' . Doctrine_Core::dump($collection->getData(), false);
  188. //$r[] = 'snapshot : ' . Doctrine_Core::dump($collection->getSnapshot());
  189. $r[] = "</pre>";
  190. return implode("\n",$r);
  191. }
  192. // Code from symfony sfToolkit class. See LICENSE
  193. // code from php at moechofe dot com (array_merge comment on php.net)
  194. /*
  195. * arrayDeepMerge
  196. *
  197. * array arrayDeepMerge ( array array1 [, array array2 [, array ...]] )
  198. *
  199. * Like array_merge
  200. *
  201. * arrayDeepMerge() merges the elements of one or more arrays together so
  202. * that the values of one are appended to the end of the previous one. It
  203. * returns the resulting array.
  204. * If the input arrays have the same string keys, then the later value for
  205. * that key will overwrite the previous one. If, however, the arrays contain
  206. * numeric keys, the later value will not overwrite the original value, but
  207. * will be appended.
  208. * If only one array is given and the array is numerically indexed, the keys
  209. * get reindexed in a continuous way.
  210. *
  211. * Different from array_merge
  212. * If string keys have arrays for values, these arrays will merge recursively.
  213. */
  214. public static function arrayDeepMerge()
  215. {
  216. switch (func_num_args()) {
  217. case 0:
  218. return false;
  219. case 1:
  220. return func_get_arg(0);
  221. case 2:
  222. $args = func_get_args();
  223. $args[2] = array();
  224. if (is_array($args[0]) && is_array($args[1]))
  225. {
  226. foreach (array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key)
  227. {
  228. $isKey0 = array_key_exists($key, $args[0]);
  229. $isKey1 = array_key_exists($key, $args[1]);
  230. if ($isKey0 && $isKey1 && is_array($args[0][$key]) && is_array($args[1][$key]))
  231. {
  232. $args[2][$key] = self::arrayDeepMerge($args[0][$key], $args[1][$key]);
  233. } else if ($isKey0 && $isKey1) {
  234. $args[2][$key] = $args[1][$key];
  235. } else if ( ! $isKey1) {
  236. $args[2][$key] = $args[0][$key];
  237. } else if ( ! $isKey0) {
  238. $args[2][$key] = $args[1][$key];
  239. }
  240. }
  241. return $args[2];
  242. } else {
  243. return $args[1];
  244. }
  245. default:
  246. $args = func_get_args();
  247. $args[1] = self::arrayDeepMerge($args[0], $args[1]);
  248. array_shift($args);
  249. return call_user_func_array(array('Doctrine_Lib', 'arrayDeepMerge'), $args);
  250. break;
  251. }
  252. }
  253. /**
  254. * Makes the directories for a path recursively.
  255. *
  256. * This method creates a given path issuing mkdir commands for all folders
  257. * that do not exist yet. Equivalent to 'mkdir -p'.
  258. *
  259. * @param string $path
  260. * @param integer $mode an integer (octal) chmod parameter for the
  261. * created directories
  262. * @return boolean true if succeeded
  263. */
  264. public static function makeDirectories($path, $mode = 0777)
  265. {
  266. if ( ! $path) {
  267. return false;
  268. }
  269. if (is_dir($path) || is_file($path)) {
  270. return true;
  271. }
  272. return mkdir(trim($path), $mode, true);
  273. }
  274. /**
  275. * Removes a non empty directory.
  276. *
  277. * This method recursively removes a directory and all its descendants.
  278. * Equivalent to 'rm -rf'.
  279. *
  280. * @param string $folderPath
  281. * @return boolean success of the operation
  282. */
  283. public static function removeDirectories($folderPath)
  284. {
  285. if (is_dir($folderPath))
  286. {
  287. foreach (scandir($folderPath) as $value)
  288. {
  289. if ($value != '.' && $value != '..')
  290. {
  291. $value = $folderPath . "/" . $value;
  292. if (is_dir($value)) {
  293. self::removeDirectories($value);
  294. } else if (is_file($value)) {
  295. unlink($value);
  296. }
  297. }
  298. }
  299. return rmdir($folderPath);
  300. } else {
  301. return false;
  302. }
  303. }
  304. /**
  305. * Copy all directory content in another one.
  306. *
  307. * This method recursively copies all $source files and subdirs in $dest.
  308. * If $source is a file, only it will be copied in $dest.
  309. *
  310. * @param string $source a directory path
  311. * @param string $dest a directory path
  312. * @return
  313. */
  314. public static function copyDirectory($source, $dest)
  315. {
  316. // Simple copy for a file
  317. if (is_file($source)) {
  318. return copy($source, $dest);
  319. }
  320. // Make destination directory
  321. if ( ! is_dir($dest)) {
  322. mkdir($dest);
  323. }
  324. // Loop through the folder
  325. $dir = dir($source);
  326. while (false !== $entry = $dir->read()) {
  327. // Skip pointers
  328. if ($entry == '.' || $entry == '..') {
  329. continue;
  330. }
  331. // Deep copy directories
  332. if ($dest !== "$source/$entry") {
  333. self::copyDirectory("$source/$entry", "$dest/$entry");
  334. }
  335. }
  336. // Clean up
  337. $dir->close();
  338. return true;
  339. }
  340. /**
  341. * Checks for a valid class name for Doctrine coding standards.
  342. *
  343. * This methods tests if $className is a valid class name for php syntax
  344. * and for Doctrine coding standards. $className must use camel case naming
  345. * and underscores for directory separation.
  346. *
  347. * @param string $classname
  348. * @return boolean
  349. */
  350. public static function isValidClassName($className)
  351. {
  352. if (preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $className)) {
  353. return false;
  354. }
  355. return true;
  356. }
  357. }