[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /** 4 * @package pake 5 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 6 * @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com> 7 * @license see the LICENSE file included in the distribution 8 * @version SVN: $Id: pakeFunction.php 3263 2007-01-13 14:20:52Z fabien $ 9 */ 10 11 require_once dirname(__FILE__).'/pakeException.class.php'; 12 require_once dirname(__FILE__).'/pakeYaml.class.php'; 13 require_once dirname(__FILE__).'/pakeGetopt.class.php'; 14 require_once dirname(__FILE__).'/pakeFinder.class.php'; 15 require_once dirname(__FILE__).'/pakeTask.class.php'; 16 require_once dirname(__FILE__).'/pakeFileTask.class.php'; 17 require_once dirname(__FILE__).'/pakeColor.class.php'; 18 require_once dirname(__FILE__).'/pakeApp.class.php'; 19 20 function pake_import($name, $import_default_tasks = true) 21 { 22 $class_name = 'pake'.ucfirst(strtolower($name)).'Task'; 23 24 if (!class_exists($class_name)) 25 { 26 // plugin available? 27 $plugin_path = ''; 28 foreach (pakeApp::get_plugin_dirs() as $dir) 29 { 30 if (file_exists($dir.DIRECTORY_SEPARATOR.$class_name.'.class.php')) 31 { 32 $plugin_path = $dir.DIRECTORY_SEPARATOR.$class_name.'.class.php'; 33 break; 34 } 35 } 36 37 if ($plugin_path) 38 { 39 require_once $plugin_path; 40 } 41 else 42 { 43 throw new pakeException(sprintf('Plugin "%s" does not exist.', $name)); 44 } 45 } 46 47 if ($import_default_tasks && is_callable($class_name, 'import_default_tasks')) 48 { 49 call_user_func(array($class_name, 'import_default_tasks')); 50 } 51 } 52 53 function pake_task($name) 54 { 55 $args = func_get_args(); 56 array_shift($args); 57 pakeTask::define_task($name, $args); 58 59 return $name; 60 } 61 62 function pake_alias($alias, $name) 63 { 64 pakeTask::define_alias($alias, $name); 65 66 return $alias; 67 } 68 69 function pake_desc($comment) 70 { 71 pakeTask::define_comment($comment); 72 } 73 74 function pake_properties($property_file) 75 { 76 $file = $property_file; 77 if (!pakeFinder::isPathAbsolute($file)) 78 { 79 $file = getcwd().DIRECTORY_SEPARATOR.$property_file; 80 } 81 82 if (file_exists($file)) 83 { 84 pakeApp::get_instance()->set_properties(parse_ini_file($file, true)); 85 } 86 else 87 { 88 throw new pakeException('Properties file does not exist.'); 89 } 90 } 91 92 function pake_file($name) 93 { 94 $args = func_get_args(); 95 array_shift($args); 96 pakeFileTask::define_task($name, $args); 97 98 return $name; 99 } 100 101 function pake_mkdirs($path, $mode = 0777) 102 { 103 if (is_dir($path)) 104 { 105 return true; 106 } 107 108 pake_echo_action('dir+', $path); 109 110 return @mkdir($path, $mode, true); 111 } 112 113 /* 114 override => boolean 115 */ 116 function pake_copy($origin_file, $target_file, $options = array()) 117 { 118 if (!array_key_exists('override', $options)) 119 { 120 $options['override'] = false; 121 } 122 123 // we create target_dir if needed 124 if (!is_dir(dirname($target_file))) 125 { 126 pake_mkdirs(dirname($target_file)); 127 } 128 129 $most_recent = false; 130 if (file_exists($target_file)) 131 { 132 $stat_target = stat($target_file); 133 $stat_origin = stat($origin_file); 134 $most_recent = ($stat_origin['mtime'] > $stat_target['mtime']) ? true : false; 135 } 136 137 if ($options['override'] || !file_exists($target_file) || $most_recent) 138 { 139 pake_echo_action('file+', $target_file); 140 copy($origin_file, $target_file); 141 } 142 } 143 144 function pake_rename($origin, $target, $options = array()) 145 { 146 // we check that target does not exist 147 if (is_readable($target)) 148 { 149 throw new pakeException(sprintf('Cannot rename because the target "%" already exist.', $target)); 150 } 151 152 pake_echo_action('rename', $origin.' > '.$target); 153 rename($origin, $target); 154 } 155 156 function pake_mirror($arg, $origin_dir, $target_dir, $options = array()) 157 { 158 $files = pakeApp::get_files_from_argument($arg, $origin_dir, true); 159 160 foreach ($files as $file) 161 { 162 if (is_dir($origin_dir.DIRECTORY_SEPARATOR.$file)) 163 { 164 pake_mkdirs($target_dir.DIRECTORY_SEPARATOR.$file); 165 } 166 else if (is_file($origin_dir.DIRECTORY_SEPARATOR.$file)) 167 { 168 pake_copy($origin_dir.DIRECTORY_SEPARATOR.$file, $target_dir.DIRECTORY_SEPARATOR.$file, $options); 169 } 170 else if (is_link($origin_dir.DIRECTORY_SEPARATOR.$file)) 171 { 172 pake_symlink($origin_dir.DIRECTORY_SEPARATOR.$file, $target_dir.DIRECTORY_SEPARATOR.$file); 173 } 174 else 175 { 176 throw new pakeException(sprintf('Unable to determine "%s" type', $file)); 177 } 178 } 179 } 180 181 function pake_remove($arg, $target_dir) 182 { 183 $files = array_reverse(pakeApp::get_files_from_argument($arg, $target_dir)); 184 185 foreach ($files as $file) 186 { 187 if (is_dir($file) && !is_link($file)) 188 { 189 pake_echo_action('dir-', $file); 190 191 rmdir($file); 192 } 193 else 194 { 195 pake_echo_action(is_link($file) ? 'link-' : 'file-', $file); 196 197 unlink($file); 198 } 199 } 200 } 201 202 function pake_touch($arg, $target_dir) 203 { 204 $files = pakeApp::get_files_from_argument($arg, $target_dir); 205 206 foreach ($files as $file) 207 { 208 pake_echo_action('file+', $file); 209 210 touch($file); 211 } 212 } 213 214 function pake_replace_tokens($arg, $target_dir, $begin_token, $end_token, $tokens) 215 { 216 $files = pakeApp::get_files_from_argument($arg, $target_dir, true); 217 218 foreach ($files as $file) 219 { 220 $replaced = false; 221 $content = file_get_contents($target_dir.DIRECTORY_SEPARATOR.$file); 222 foreach ($tokens as $key => $value) 223 { 224 $content = str_replace($begin_token.$key.$end_token, $value, $content, $count); 225 if ($count) $replaced = true; 226 } 227 228 pake_echo_action('tokens', $target_dir.DIRECTORY_SEPARATOR.$file); 229 230 file_put_contents($target_dir.DIRECTORY_SEPARATOR.$file, $content); 231 } 232 } 233 234 function pake_symlink($origin_dir, $target_dir, $copy_on_windows = false) 235 { 236 if (!function_exists('symlink') && $copy_on_windows) 237 { 238 $finder = pakeFinder::type('any')->ignore_version_control(); 239 pake_mirror($finder, $origin_dir, $target_dir); 240 return; 241 } 242 243 $ok = false; 244 if (is_link($target_dir)) 245 { 246 if (readlink($target_dir) != $origin_dir) 247 { 248 unlink($target_dir); 249 } 250 else 251 { 252 $ok = true; 253 } 254 } 255 256 if (!$ok) 257 { 258 pake_echo_action('link+', $target_dir); 259 symlink($origin_dir, $target_dir); 260 } 261 } 262 263 function pake_chmod($arg, $target_dir, $mode, $umask = 0000) 264 { 265 $current_umask = umask(); 266 umask($umask); 267 268 $files = pakeApp::get_files_from_argument($arg, $target_dir, true); 269 270 foreach ($files as $file) 271 { 272 pake_echo_action(sprintf('chmod %o', $mode), $target_dir.DIRECTORY_SEPARATOR.$file); 273 chmod($target_dir.DIRECTORY_SEPARATOR.$file, $mode); 274 } 275 276 umask($current_umask); 277 } 278 279 function pake_sh($cmd) 280 { 281 $verbose = pakeApp::get_instance()->get_verbose(); 282 pake_echo_action('exec ', $cmd); 283 284 ob_start(); 285 passthru($cmd.' 2>&1', $return); 286 $content = ob_get_contents(); 287 ob_end_clean(); 288 289 if ($return > 0) 290 { 291 throw new pakeException(sprintf('Problem executing command %s', $verbose ? "\n".$content : '')); 292 } 293 294 return $content; 295 } 296 297 function pake_strip_php_comments($arg) 298 { 299 /* T_ML_COMMENT does not exist in PHP 5. 300 * The following three lines define it in order to 301 * preserve backwards compatibility. 302 * 303 * The next two lines define the PHP 5-only T_DOC_COMMENT, 304 * which we will mask as T_ML_COMMENT for PHP 4. 305 */ 306 if (!defined('T_ML_COMMENT')) 307 { 308 define('T_ML_COMMENT', T_COMMENT); 309 } 310 else 311 { 312 if (!defined('T_DOC_COMMENT')) define('T_DOC_COMMENT', T_ML_COMMENT); 313 } 314 315 $files = pakeApp::get_files_from_argument($arg); 316 317 foreach ($files as $file) 318 { 319 if (!is_file($file)) continue; 320 321 $source = file_get_contents($file); 322 $output = ''; 323 324 $tokens = token_get_all($source); 325 foreach ($tokens as $token) 326 { 327 if (is_string($token)) 328 { 329 // simple 1-character token 330 $output .= $token; 331 } 332 else 333 { 334 // token array 335 list($id, $text) = $token; 336 switch ($id) 337 { 338 case T_COMMENT: 339 case T_ML_COMMENT: // we've defined this 340 case T_DOC_COMMENT: // and this 341 // no action on comments 342 break; 343 default: 344 // anything else -> output "as is" 345 $output .= $text; 346 break; 347 } 348 } 349 } 350 351 file_put_contents($file, $output); 352 } 353 } 354 355 function pake_format_action($section, $text, $size = null) 356 { 357 if (pakeApp::get_instance()->get_verbose()) 358 { 359 $width = 9 + strlen(pakeColor::colorize('', 'INFO')); 360 return sprintf('>> %-'.$width.'s %s', pakeColor::colorize($section, 'INFO'), pakeApp::excerpt($text, $size))."\n"; 361 } 362 } 363 364 function pake_echo_action($section, $text) 365 { 366 echo pake_format_action($section, $text); 367 } 368 369 function pake_excerpt($text) 370 { 371 if (pakeApp::get_instance()->get_verbose()) 372 { 373 echo pakeApp::excerpt($text)."\n"; 374 } 375 } 376 377 function pake_echo($text) 378 { 379 if (pakeApp::get_instance()->get_verbose()) 380 { 381 echo $text."\n"; 382 } 383 } 384 385 function pake_echo_comment($text) 386 { 387 if (pakeApp::get_instance()->get_verbose()) 388 { 389 echo sprintf(pakeColor::colorize(' # %s', 'COMMENT'), $text)."\n"; 390 } 391 } 392 393 // register our default exception handler 394 function pake_exception_default_handler($exception) 395 { 396 $e = new pakeException(); 397 $e->render($exception); 398 exit(1); 399 } 400 set_exception_handler('pake_exception_default_handler'); 401 402 // fix php behavior if using cgi php 403 // from http://www.sitepoint.com/article/php-command-line-1/3 404 if (false !== strpos(PHP_SAPI, 'cgi')) 405 { 406 // handle output buffering 407 @ob_end_flush(); 408 ob_implicit_flush(true); 409 410 // PHP ini settings 411 set_time_limit(0); 412 ini_set('track_errors', true); 413 ini_set('html_errors', false); 414 ini_set('magic_quotes_runtime', false); 415 416 // define stream constants 417 define('STDIN', fopen('php://stdin', 'r')); 418 define('STDOUT', fopen('php://stdout', 'w')); 419 define('STDERR', fopen('php://stderr', 'w')); 420 421 // change directory 422 if (isset($_SERVER['PWD'])) 423 { 424 chdir($_SERVER['PWD']); 425 } 426 427 // close the streams on script termination 428 register_shutdown_function(create_function('', 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;')); 429 }
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 |