[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * (c) 2004-2006 Sean Kerr. 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 /** 13 * sfToolkit provides basic utility methods. 14 * 15 * @package symfony 16 * @subpackage util 17 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 18 * @author Sean Kerr <skerr@mojavi.org> 19 * @version SVN: $Id: sfToolkit.class.php 3251 2007-01-12 20:49:29Z fabien $ 20 */ 21 class sfToolkit 22 { 23 /** 24 * Extract the class or interface name from filename. 25 * 26 * @param string A filename. 27 * 28 * @return string A class or interface name, if one can be extracted, otherwise null. 29 */ 30 public static function extractClassName($filename) 31 { 32 $retval = null; 33 34 if (self::isPathAbsolute($filename)) 35 { 36 $filename = basename($filename); 37 } 38 39 $pattern = '/(.*?)\.(class|interface)\.php/i'; 40 41 if (preg_match($pattern, $filename, $match)) 42 { 43 $retval = $match[1]; 44 } 45 46 return $retval; 47 } 48 49 /** 50 * Clear all files in a given directory. 51 * 52 * @param string An absolute filesystem path to a directory. 53 * 54 * @return void 55 */ 56 public static function clearDirectory($directory) 57 { 58 if (!is_dir($directory)) 59 { 60 return; 61 } 62 63 // open a file point to the cache dir 64 $fp = opendir($directory); 65 66 // ignore names 67 $ignore = array('.', '..', 'CVS', '.svn'); 68 69 while (($file = readdir($fp)) !== false) 70 { 71 if (!in_array($file, $ignore)) 72 { 73 if (is_link($directory.'/'.$file)) 74 { 75 // delete symlink 76 unlink($directory.'/'.$file); 77 } 78 else if (is_dir($directory.'/'.$file)) 79 { 80 // recurse through directory 81 self::clearDirectory($directory.'/'.$file); 82 83 // delete the directory 84 rmdir($directory.'/'.$file); 85 } 86 else 87 { 88 // delete the file 89 unlink($directory.'/'.$file); 90 } 91 } 92 } 93 94 // close file pointer 95 fclose($fp); 96 } 97 98 /** 99 * Clear all files and directories corresponding to a glob pattern. 100 * 101 * @param string An absolute filesystem pattern. 102 * 103 * @return void 104 */ 105 public static function clearGlob($pattern) 106 { 107 $files = glob($pattern); 108 109 // order is important when removing directories 110 sort($files); 111 112 foreach ($files as $file) 113 { 114 if (is_dir($file)) 115 { 116 // delete directory 117 self::clearDirectory($file); 118 } 119 else 120 { 121 // delete file 122 unlink($file); 123 } 124 } 125 } 126 127 /** 128 * Determine if a filesystem path is absolute. 129 * 130 * @param path A filesystem path. 131 * 132 * @return bool true, if the path is absolute, otherwise false. 133 */ 134 public static function isPathAbsolute($path) 135 { 136 if ($path[0] == '/' || $path[0] == '\\' || 137 (strlen($path) > 3 && ctype_alpha($path[0]) && 138 $path[1] == ':' && 139 ($path[2] == '\\' || $path[2] == '/') 140 ) 141 ) 142 { 143 return true; 144 } 145 146 return false; 147 } 148 149 /** 150 * Determine if a lock file is present. 151 * 152 * @param integer A max amount of life time for the lock file. 153 * 154 * @return bool true, if the lock file is present, otherwise false. 155 */ 156 public static function hasLockFile($lockFile, $maxLockFileLifeTime = 0) 157 { 158 $isLocked = false; 159 if (is_readable($lockFile) && ($last_access = fileatime($lockFile))) 160 { 161 $now = time(); 162 $timeDiff = $now - $last_access; 163 164 if (!$maxLockFileLifeTime || $timeDiff < $maxLockFileLifeTime) 165 { 166 $isLocked = true; 167 } 168 else 169 { 170 unlink($lockFile); 171 } 172 } 173 174 return $isLocked; 175 } 176 177 public static function stripComments($source) 178 { 179 if (!sfConfig::get('sf_strip_comments', true)) 180 { 181 return $source; 182 } 183 184 // tokenizer available? 185 if (!function_exists('token_get_all')) 186 { 187 $source = sfToolkit::pregtr($source, array('#/\*((?!\*/)[\d\D\s])*\*/#' => '', // remove /* ... */ 188 '#^\s*//.*$#m' => '')); // remove // ... 189 190 return $source; 191 } 192 193 $output = ''; 194 195 $tokens = token_get_all($source); 196 foreach ($tokens as $token) 197 { 198 if (is_string($token)) 199 { 200 // simple 1-character token 201 $output .= $token; 202 } 203 else 204 { 205 // token array 206 list($id, $text) = $token; 207 208 switch ($id) 209 { 210 case T_COMMENT: 211 case T_DOC_COMMENT: 212 // no action on comments 213 break; 214 default: 215 // anything else -> output "as is" 216 $output .= $text; 217 break; 218 } 219 } 220 } 221 222 return $output; 223 } 224 225 public static function stripslashesDeep($value) 226 { 227 return is_array($value) ? array_map(array('sfToolkit', 'stripslashesDeep'), $value) : stripslashes($value); 228 } 229 230 // code from php at moechofe dot com (array_merge comment on php.net) 231 /* 232 * array arrayDeepMerge ( array array1 [, array array2 [, array ...]] ) 233 * 234 * Like array_merge 235 * 236 * arrayDeepMerge() merges the elements of one or more arrays together so 237 * that the values of one are appended to the end of the previous one. It 238 * returns the resulting array. 239 * If the input arrays have the same string keys, then the later value for 240 * that key will overwrite the previous one. If, however, the arrays contain 241 * numeric keys, the later value will not overwrite the original value, but 242 * will be appended. 243 * If only one array is given and the array is numerically indexed, the keys 244 * get reindexed in a continuous way. 245 * 246 * Different from array_merge 247 * If string keys have arrays for values, these arrays will merge recursively. 248 */ 249 public static function arrayDeepMerge() 250 { 251 switch (func_num_args()) 252 { 253 case 0: 254 return false; 255 case 1: 256 return func_get_arg(0); 257 case 2: 258 $args = func_get_args(); 259 $args[2] = array(); 260 if (is_array($args[0]) && is_array($args[1])) 261 { 262 foreach (array_unique(array_merge(array_keys($args[0]),array_keys($args[1]))) as $key) 263 { 264 $isKey0 = array_key_exists($key, $args[0]); 265 $isKey1 = array_key_exists($key, $args[1]); 266 if ($isKey0 && $isKey1 && is_array($args[0][$key]) && is_array($args[1][$key])) 267 { 268 $args[2][$key] = self::arrayDeepMerge($args[0][$key], $args[1][$key]); 269 } 270 else if ($isKey0 && $isKey1) 271 { 272 $args[2][$key] = $args[1][$key]; 273 } 274 else if (!$isKey1) 275 { 276 $args[2][$key] = $args[0][$key]; 277 } 278 else if (!$isKey0) 279 { 280 $args[2][$key] = $args[1][$key]; 281 } 282 } 283 return $args[2]; 284 } 285 else 286 { 287 return $args[1]; 288 } 289 default : 290 $args = func_get_args(); 291 $args[1] = sfToolkit::arrayDeepMerge($args[0], $args[1]); 292 array_shift($args); 293 return call_user_func_array(array('sfToolkit', 'arrayDeepMerge'), $args); 294 break; 295 } 296 } 297 298 public static function stringToArray($string) 299 { 300 preg_match_all('/ 301 \s*(\w+) # key \\1 302 \s*=\s* # = 303 (\'|")? # values may be included in \' or " \\2 304 (.*?) # value \\3 305 (?(2) \\2) # matching \' or " if needed \\4 306 \s*(?: 307 (?=\w+\s*=) | \s*$ # followed by another key= or the end of the string 308 ) 309 /x', $string, $matches, PREG_SET_ORDER); 310 311 $attributes = array(); 312 foreach ($matches as $val) 313 { 314 $attributes[$val[1]] = self::literalize($val[3]); 315 } 316 317 return $attributes; 318 } 319 320 /** 321 * Finds the type of the passed value, returns the value as the new type. 322 * 323 * @param string 324 * @return mixed 325 */ 326 public static function literalize($value, $quoted = false) 327 { 328 // lowercase our value for comparison 329 $value = trim($value); 330 $lvalue = strtolower($value); 331 332 if (in_array($lvalue, array('null', '~', ''))) 333 { 334 $value = null; 335 } 336 else if (in_array($lvalue, array('true', 'on', '+', 'yes'))) 337 { 338 $value = true; 339 } 340 else if (in_array($lvalue, array('false', 'off', '-', 'no'))) 341 { 342 $value = false; 343 } 344 else if (ctype_digit($value)) 345 { 346 $value = (int) $value; 347 } 348 else if (is_numeric($value)) 349 { 350 $value = (float) $value; 351 } 352 else 353 { 354 $value = self::replaceConstants($value); 355 if ($quoted) 356 { 357 $value = '\''.str_replace('\'', '\\\'', $value).'\''; 358 } 359 } 360 361 return $value; 362 } 363 364 /** 365 * Replaces constant identifiers in a scalar value. 366 * 367 * @param string the value to perform the replacement on 368 * @return string the value with substitutions made 369 */ 370 public static function replaceConstants($value) 371 { 372 return is_string($value) ? preg_replace('/%(.+?)%/e', 'sfConfig::has(strtolower("\\1")) ? sfConfig::get(strtolower("\\1")) : "%\\1%"', $value) : $value; 373 } 374 375 /** 376 * Returns subject replaced with regular expression matchs 377 * 378 * @param mixed subject to search 379 * @param array array of search => replace pairs 380 */ 381 public static function pregtr($search, $replacePairs) 382 { 383 return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search); 384 } 385 386 public static function isArrayValuesEmpty($array) 387 { 388 static $isEmpty = true; 389 foreach ($array as $value) 390 { 391 $isEmpty = (is_array($value)) ? self::isArrayValuesEmpty($value) : (strlen($value) == 0); 392 if (!$isEmpty) 393 { 394 break; 395 } 396 } 397 398 return $isEmpty; 399 } 400 401 /** 402 * Check if a string is an utf8 using a W3C regular expression 403 * http://fr3.php.net/manual/en/function.mb-detect-encoding.php#50087 404 * 405 * @param string 406 * 407 * @return bool true if $string is valid UTF-8 and false otherwise. 408 */ 409 public static function isUTF8($string) 410 { 411 // from http://w3.org/International/questions/qa-forms-utf-8.html 412 return preg_match('%^(?: 413 [\x09\x0A\x0D\x20-\x7E] # ASCII 414 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 415 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 416 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 417 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 418 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 419 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 420 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 421 )*$%xs', $string); 422 } 423 424 public static function getArrayValueForPath($values, $name, $default = null) 425 { 426 if (false !== ($offset = strpos($name, '['))) 427 { 428 if (isset($values[substr($name, 0, $offset)])) 429 { 430 $array = $values[substr($name, 0, $offset)]; 431 432 while ($pos = strpos($name, '[', $offset)) 433 { 434 $end = strpos($name, ']', $pos); 435 if ($end == $pos + 1) 436 { 437 // reached a [] 438 break; 439 } 440 else if (!isset($array[substr($name, $pos + 1, $end - $pos - 1)])) 441 { 442 return $default; 443 } 444 $array = $array[substr($name, $pos + 1, $end - $pos - 1)]; 445 $offset = $end; 446 } 447 448 return $array; 449 } 450 } 451 452 return $default; 453 } 454 455 public static function getPhpCli() 456 { 457 $path = getenv('PATH') ? getenv('PATH') : getenv('Path'); 458 $suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array(''); 459 foreach (array('php5', 'php') as $phpCli) 460 { 461 foreach ($suffixes as $suffix) 462 { 463 foreach (explode(PATH_SEPARATOR, $path) as $dir) 464 { 465 $file = $dir.DIRECTORY_SEPARATOR.$phpCli.$suffix; 466 if (is_executable($file)) 467 { 468 return $file; 469 } 470 } 471 } 472 } 473 474 throw new sfException('Unable to find PHP executable'); 475 } 476 477 /** 478 * From PEAR System.php 479 * 480 * LICENSE: This source file is subject to version 3.0 of the PHP license 481 * that is available through the world-wide-web at the following URI: 482 * http://www.php.net/license/3_0.txt. If you did not receive a copy of 483 * the PHP License and are unable to obtain it through the web, please 484 * send a note to license@php.net so we can mail you a copy immediately. 485 * 486 * @author Tomas V.V.Cox <cox@idecnet.com> 487 * @copyright 1997-2006 The PHP Group 488 * @license http://www.php.net/license/3_0.txt PHP License 3.0 489 */ 490 public static function getTmpDir() 491 { 492 if (DIRECTORY_SEPARATOR == '\\') 493 { 494 if ($var = isset($_ENV['TEMP']) ? $_ENV['TEMP'] : getenv('TEMP')) 495 { 496 return $var; 497 } 498 if ($var = isset($_ENV['TMP']) ? $_ENV['TMP'] : getenv('TMP')) 499 { 500 return $var; 501 } 502 if ($var = isset($_ENV['windir']) ? $_ENV['windir'] : getenv('windir')) 503 { 504 return $var; 505 } 506 507 return getenv('SystemRoot').'\temp'; 508 } 509 510 if ($var = isset($_ENV['TMPDIR']) ? $_ENV['TMPDIR'] : getenv('TMPDIR')) 511 { 512 return $var; 513 } 514 515 return '/tmp'; 516 } 517 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |