PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Group-I/jobeet/lib/vendor/symfony/lib/util/sfToolkit.class.php

https://bitbucket.org/hosseinzolfi/db-lab-spring-2011/
PHP | 611 lines | 412 code | 46 blank | 153 comment | 72 complexity | 49db4c75b1540ae6d84edda4b2b8776a MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfToolkit provides basic utility methods.
  12. *
  13. * @package symfony
  14. * @subpackage util
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Sean Kerr <sean@code-box.org>
  17. * @version SVN: $Id: sfToolkit.class.php 29525 2010-05-19 13:01:43Z fabien $
  18. */
  19. class sfToolkit
  20. {
  21. /**
  22. * Extract the class or interface name from filename.
  23. *
  24. * @param string $filename A filename.
  25. *
  26. * @return string A class or interface name, if one can be extracted, otherwise null.
  27. */
  28. public static function extractClassName($filename)
  29. {
  30. $retval = null;
  31. if (self::isPathAbsolute($filename))
  32. {
  33. $filename = basename($filename);
  34. }
  35. $pattern = '/(.*?)\.(class|interface)\.php/i';
  36. if (preg_match($pattern, $filename, $match))
  37. {
  38. $retval = $match[1];
  39. }
  40. return $retval;
  41. }
  42. /**
  43. * Clear all files in a given directory.
  44. *
  45. * @param string $directory An absolute filesystem path to a directory.
  46. */
  47. public static function clearDirectory($directory)
  48. {
  49. if (!is_dir($directory))
  50. {
  51. return;
  52. }
  53. // open a file point to the cache dir
  54. $fp = opendir($directory);
  55. // ignore names
  56. $ignore = array('.', '..', 'CVS', '.svn');
  57. while (($file = readdir($fp)) !== false)
  58. {
  59. if (!in_array($file, $ignore))
  60. {
  61. if (is_link($directory.'/'.$file))
  62. {
  63. // delete symlink
  64. unlink($directory.'/'.$file);
  65. }
  66. else if (is_dir($directory.'/'.$file))
  67. {
  68. // recurse through directory
  69. self::clearDirectory($directory.'/'.$file);
  70. // delete the directory
  71. rmdir($directory.'/'.$file);
  72. }
  73. else
  74. {
  75. // delete the file
  76. unlink($directory.'/'.$file);
  77. }
  78. }
  79. }
  80. // close file pointer
  81. closedir($fp);
  82. }
  83. /**
  84. * Clear all files and directories corresponding to a glob pattern.
  85. *
  86. * @param string $pattern An absolute filesystem pattern.
  87. */
  88. public static function clearGlob($pattern)
  89. {
  90. if (false === $files = glob($pattern))
  91. {
  92. return;
  93. }
  94. // order is important when removing directories
  95. sort($files);
  96. foreach ($files as $file)
  97. {
  98. if (is_dir($file))
  99. {
  100. // delete directory
  101. self::clearDirectory($file);
  102. }
  103. else
  104. {
  105. // delete file
  106. unlink($file);
  107. }
  108. }
  109. }
  110. /**
  111. * Determine if a filesystem path is absolute.
  112. *
  113. * @param path $path A filesystem path.
  114. *
  115. * @return bool true, if the path is absolute, otherwise false.
  116. */
  117. public static function isPathAbsolute($path)
  118. {
  119. if ($path[0] == '/' || $path[0] == '\\' ||
  120. (strlen($path) > 3 && ctype_alpha($path[0]) &&
  121. $path[1] == ':' &&
  122. ($path[2] == '\\' || $path[2] == '/')
  123. )
  124. )
  125. {
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * Strips comments from php source code
  132. *
  133. * @param string $source PHP source code.
  134. *
  135. * @return string Comment free source code.
  136. */
  137. public static function stripComments($source)
  138. {
  139. if (!function_exists('token_get_all'))
  140. {
  141. return $source;
  142. }
  143. $ignore = array(T_COMMENT => true, T_DOC_COMMENT => true);
  144. $output = '';
  145. foreach (token_get_all($source) as $token)
  146. {
  147. // array
  148. if (isset($token[1]))
  149. {
  150. // no action on comments
  151. if (!isset($ignore[$token[0]]))
  152. {
  153. // anything else -> output "as is"
  154. $output .= $token[1];
  155. }
  156. }
  157. else
  158. {
  159. // simple 1-character token
  160. $output .= $token;
  161. }
  162. }
  163. return $output;
  164. }
  165. /**
  166. * Strip slashes recursively from array
  167. *
  168. * @param array $value the value to strip
  169. *
  170. * @return array clean value with slashes stripped
  171. */
  172. public static function stripslashesDeep($value)
  173. {
  174. return is_array($value) ? array_map(array('sfToolkit', 'stripslashesDeep'), $value) : stripslashes($value);
  175. }
  176. // code from php at moechofe dot com (array_merge comment on php.net)
  177. /*
  178. * array arrayDeepMerge ( array array1 [, array array2 [, array ...]] )
  179. *
  180. * Like array_merge
  181. *
  182. * arrayDeepMerge() merges the elements of one or more arrays together so
  183. * that the values of one are appended to the end of the previous one. It
  184. * returns the resulting array.
  185. * If the input arrays have the same string keys, then the later value for
  186. * that key will overwrite the previous one. If, however, the arrays contain
  187. * numeric keys, the later value will not overwrite the original value, but
  188. * will be appended.
  189. * If only one array is given and the array is numerically indexed, the keys
  190. * get reindexed in a continuous way.
  191. *
  192. * Different from array_merge
  193. * If string keys have arrays for values, these arrays will merge recursively.
  194. */
  195. public static function arrayDeepMerge()
  196. {
  197. switch (func_num_args())
  198. {
  199. case 0:
  200. return false;
  201. case 1:
  202. return func_get_arg(0);
  203. case 2:
  204. $args = func_get_args();
  205. $args[2] = array();
  206. if (is_array($args[0]) && is_array($args[1]))
  207. {
  208. foreach (array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key)
  209. {
  210. $isKey0 = array_key_exists($key, $args[0]);
  211. $isKey1 = array_key_exists($key, $args[1]);
  212. if ($isKey0 && $isKey1 && is_array($args[0][$key]) && is_array($args[1][$key]))
  213. {
  214. $args[2][$key] = self::arrayDeepMerge($args[0][$key], $args[1][$key]);
  215. }
  216. else if ($isKey0 && $isKey1)
  217. {
  218. $args[2][$key] = $args[1][$key];
  219. }
  220. else if (!$isKey1)
  221. {
  222. $args[2][$key] = $args[0][$key];
  223. }
  224. else if (!$isKey0)
  225. {
  226. $args[2][$key] = $args[1][$key];
  227. }
  228. }
  229. return $args[2];
  230. }
  231. else
  232. {
  233. return $args[1];
  234. }
  235. default :
  236. $args = func_get_args();
  237. $args[1] = sfToolkit::arrayDeepMerge($args[0], $args[1]);
  238. array_shift($args);
  239. return call_user_func_array(array('sfToolkit', 'arrayDeepMerge'), $args);
  240. break;
  241. }
  242. }
  243. /**
  244. * Converts string to array
  245. *
  246. * @param string $string the value to convert to array
  247. *
  248. * @return array
  249. */
  250. public static function stringToArray($string)
  251. {
  252. preg_match_all('/
  253. \s*(\w+) # key \\1
  254. \s*=\s* # =
  255. (\'|")? # values may be included in \' or " \\2
  256. (.*?) # value \\3
  257. (?(2) \\2) # matching \' or " if needed \\4
  258. \s*(?:
  259. (?=\w+\s*=) | \s*$ # followed by another key= or the end of the string
  260. )
  261. /x', $string, $matches, PREG_SET_ORDER);
  262. $attributes = array();
  263. foreach ($matches as $val)
  264. {
  265. $attributes[$val[1]] = self::literalize($val[3]);
  266. }
  267. return $attributes;
  268. }
  269. /**
  270. * Finds the type of the passed value, returns the value as the new type.
  271. *
  272. * @param string $value
  273. * @param bool $quoted Quote?
  274. *
  275. * @return mixed
  276. */
  277. public static function literalize($value, $quoted = false)
  278. {
  279. // lowercase our value for comparison
  280. $value = trim($value);
  281. $lvalue = strtolower($value);
  282. if (in_array($lvalue, array('null', '~', '')))
  283. {
  284. $value = null;
  285. }
  286. else if (in_array($lvalue, array('true', 'on', '+', 'yes')))
  287. {
  288. $value = true;
  289. }
  290. else if (in_array($lvalue, array('false', 'off', '-', 'no')))
  291. {
  292. $value = false;
  293. }
  294. else if (ctype_digit($value))
  295. {
  296. $value = (int) $value;
  297. }
  298. else if (is_numeric($value))
  299. {
  300. $value = (float) $value;
  301. }
  302. else
  303. {
  304. $value = self::replaceConstants($value);
  305. if ($quoted)
  306. {
  307. $value = '\''.str_replace('\'', '\\\'', $value).'\'';
  308. }
  309. }
  310. return $value;
  311. }
  312. /**
  313. * Replaces constant identifiers in a scalar value.
  314. *
  315. * @param string $value the value to perform the replacement on
  316. *
  317. * @return string the value with substitutions made
  318. */
  319. public static function replaceConstants($value)
  320. {
  321. return is_string($value) ? preg_replace_callback('/%(.+?)%/', create_function('$v', 'return sfConfig::has(strtolower($v[1])) ? sfConfig::get(strtolower($v[1])) : "%{$v[1]}%";'), $value) : $value;
  322. }
  323. /**
  324. * Returns subject replaced with regular expression matchs
  325. *
  326. * @param mixed $search subject to search
  327. * @param array $replacePairs array of search => replace pairs
  328. */
  329. public static function pregtr($search, $replacePairs)
  330. {
  331. return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
  332. }
  333. /**
  334. * Checks if array values are empty
  335. *
  336. * @param array $array the array to check
  337. * @return boolean true if empty, otherwise false
  338. */
  339. public static function isArrayValuesEmpty($array)
  340. {
  341. static $isEmpty = true;
  342. foreach ($array as $value)
  343. {
  344. $isEmpty = (is_array($value)) ? self::isArrayValuesEmpty($value) : (strlen($value) == 0);
  345. if (!$isEmpty)
  346. {
  347. break;
  348. }
  349. }
  350. return $isEmpty;
  351. }
  352. /**
  353. * Checks if a string is an utf8.
  354. *
  355. * Yi Stone Li<yili@yahoo-inc.com>
  356. * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
  357. * Licensed under the BSD open source license
  358. *
  359. * @param string
  360. *
  361. * @return bool true if $string is valid UTF-8 and false otherwise.
  362. */
  363. public static function isUTF8($string)
  364. {
  365. for ($idx = 0, $strlen = strlen($string); $idx < $strlen; $idx++)
  366. {
  367. $byte = ord($string[$idx]);
  368. if ($byte & 0x80)
  369. {
  370. if (($byte & 0xE0) == 0xC0)
  371. {
  372. // 2 byte char
  373. $bytes_remaining = 1;
  374. }
  375. else if (($byte & 0xF0) == 0xE0)
  376. {
  377. // 3 byte char
  378. $bytes_remaining = 2;
  379. }
  380. else if (($byte & 0xF8) == 0xF0)
  381. {
  382. // 4 byte char
  383. $bytes_remaining = 3;
  384. }
  385. else
  386. {
  387. return false;
  388. }
  389. if ($idx + $bytes_remaining >= $strlen)
  390. {
  391. return false;
  392. }
  393. while ($bytes_remaining--)
  394. {
  395. if ((ord($string[++$idx]) & 0xC0) != 0x80)
  396. {
  397. return false;
  398. }
  399. }
  400. }
  401. }
  402. return true;
  403. }
  404. /**
  405. * Returns an array value for a path.
  406. *
  407. * @param array $values The values to search
  408. * @param string $name The token name
  409. * @param array $default Default if not found
  410. *
  411. * @return array
  412. */
  413. public static function getArrayValueForPath($values, $name, $default = null)
  414. {
  415. if (false === $offset = strpos($name, '['))
  416. {
  417. return isset($values[$name]) ? $values[$name] : $default;
  418. }
  419. if (!isset($values[substr($name, 0, $offset)]))
  420. {
  421. return $default;
  422. }
  423. $array = $values[substr($name, 0, $offset)];
  424. while (false !== $pos = strpos($name, '[', $offset))
  425. {
  426. $end = strpos($name, ']', $pos);
  427. if ($end == $pos + 1)
  428. {
  429. // reached a []
  430. if (!is_array($array))
  431. {
  432. return $default;
  433. }
  434. break;
  435. }
  436. else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)]))
  437. {
  438. return $default;
  439. }
  440. else if (is_array($array))
  441. {
  442. $array = $array[substr($name, $pos + 1, $end - $pos - 1)];
  443. $offset = $end;
  444. }
  445. else
  446. {
  447. return $default;
  448. }
  449. }
  450. return $array;
  451. }
  452. /**
  453. * Get path to php cli.
  454. *
  455. * @throws sfException If no php cli found
  456. * @return string
  457. */
  458. public static function getPhpCli()
  459. {
  460. $path = getenv('PATH') ? getenv('PATH') : getenv('Path');
  461. $suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array('');
  462. foreach (array('php5', 'php') as $phpCli)
  463. {
  464. foreach ($suffixes as $suffix)
  465. {
  466. foreach (explode(PATH_SEPARATOR, $path) as $dir)
  467. {
  468. if (is_file($file = $dir.DIRECTORY_SEPARATOR.$phpCli.$suffix) && is_executable($file))
  469. {
  470. return $file;
  471. }
  472. }
  473. }
  474. }
  475. throw new sfException('Unable to find PHP executable.');
  476. }
  477. /**
  478. * Converts strings to UTF-8 via iconv. NB, the result may not by UTF-8 if the conversion failed.
  479. *
  480. * This file comes from Prado (BSD License)
  481. *
  482. * @param string $string string to convert to UTF-8
  483. * @param string $from current encoding
  484. *
  485. * @return string UTF-8 encoded string, original string if iconv failed.
  486. */
  487. static public function I18N_toUTF8($string, $from)
  488. {
  489. $from = strtoupper($from);
  490. if ($from != 'UTF-8')
  491. {
  492. $s = iconv($from,'UTF-8',$string); // to UTF-8
  493. return $s !== false ? $s : $string; // it could return false
  494. }
  495. return $string;
  496. }
  497. /**
  498. * Converts UTF-8 strings to a different encoding. NB. The result may not have been encoded if iconv fails.
  499. *
  500. * This file comes from Prado (BSD License)
  501. *
  502. * @param string $string the UTF-8 string for conversion
  503. * @param string $to new encoding
  504. *
  505. * @return string encoded string.
  506. */
  507. static public function I18N_toEncoding($string, $to)
  508. {
  509. $to = strtoupper($to);
  510. if ($to != 'UTF-8')
  511. {
  512. $s = iconv('UTF-8', $to, $string);
  513. return $s !== false ? $s : $string;
  514. }
  515. return $string;
  516. }
  517. /**
  518. * Adds a path to the PHP include_path setting.
  519. *
  520. * @param mixed $path Single string path or an array of paths
  521. * @param string $position Either 'front' or 'back'
  522. *
  523. * @return string The old include path
  524. */
  525. static public function addIncludePath($path, $position = 'front')
  526. {
  527. if (is_array($path))
  528. {
  529. foreach ('front' == $position ? array_reverse($path) : $path as $p)
  530. {
  531. self::addIncludePath($p, $position);
  532. }
  533. return;
  534. }
  535. $paths = explode(PATH_SEPARATOR, get_include_path());
  536. // remove what's already in the include_path
  537. if (false !== $key = array_search(realpath($path), array_map('realpath', $paths)))
  538. {
  539. unset($paths[$key]);
  540. }
  541. switch ($position)
  542. {
  543. case 'front':
  544. array_unshift($paths, $path);
  545. break;
  546. case 'back':
  547. $paths[] = $path;
  548. break;
  549. default:
  550. throw new InvalidArgumentException(sprintf('Unrecognized position: "%s"', $position));
  551. }
  552. return set_include_path(join(PATH_SEPARATOR, $paths));
  553. }
  554. }