[ 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: pakeTask.class.php 1794 2006-08-24 05:37:10Z fabien $ 9 */ 10 11 /** 12 * 13 * . 14 * 15 * . 16 * 17 * @package pake 18 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 19 * @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com> 20 * @license see the LICENSE file included in the distribution 21 * @version SVN: $Id: pakeTask.class.php 1794 2006-08-24 05:37:10Z fabien $ 22 */ 23 class pakeTask 24 { 25 protected static $TASKS = array(); 26 protected static $ALIAS = array(); 27 protected static $last_comment = ''; 28 protected $prerequisites = array(); 29 protected $name = ''; 30 protected $comment = ''; 31 protected $already_invoked = false; 32 protected $trace = null; 33 protected $verbose = null; 34 protected $dryrun = null; 35 protected $alias = ''; 36 37 public function __construct($task_name) 38 { 39 $this->name = $task_name; 40 $this->comment = ''; 41 $this->prerequisites = array(); 42 $this->already_invoked = false; 43 $pake = pakeApp::get_instance(); 44 $this->trace = $pake->get_trace(); 45 $this->dryrun = $pake->get_dryrun(); 46 $this->verbose = $pake->get_verbose(); 47 } 48 49 public function is_verbose() 50 { 51 return $this->verbose; 52 } 53 54 public function enhance($deps = null) 55 { 56 if (!$deps) return; 57 58 if (is_array($deps)) 59 { 60 $this->prerequisites = array_merge($this->prerequisites, $deps); 61 } 62 else 63 { 64 $this->prerequisites[] = $deps; 65 } 66 } 67 68 public static function get_tasks() 69 { 70 $tasks = pakeTask::$TASKS; 71 // we merge tasks and aliases 72 foreach (pakeTask::$ALIAS as $alias => $name) 73 { 74 if (!array_key_exists($name, $tasks)) 75 { 76 throw new pakeException(sprintf('Task "%s" cannot be cloned to "%s" because it does not exist.', $name, $alias)); 77 } 78 79 $alias_task = clone $tasks[$name]; 80 $alias_task->alias = $name; 81 $alias_task->name = $alias; 82 $tasks[$alias] = $alias_task; 83 } 84 85 return $tasks; 86 } 87 88 public function get_property($name, $section = null) 89 { 90 $properties = pakeApp::get_instance()->get_properties(); 91 92 if ($section) 93 { 94 if (!array_key_exists($section, $properties) || !array_key_exists($name, $properties[$section])) 95 { 96 throw new pakeException(sprintf('Property "%s/%s" does not exist.', $section, $name)); 97 } 98 else 99 { 100 return $properties[$section][$name]; 101 } 102 } 103 else 104 { 105 if (!array_key_exists($name, $properties)) 106 { 107 throw new pakeException(sprintf('Property "%s" does not exist.', $name)); 108 } 109 else 110 { 111 return $properties[$name]; 112 } 113 } 114 } 115 116 public function get_alias() 117 { 118 return $this->alias; 119 } 120 121 public function get_prerequisites() 122 { 123 return $this->prerequisites; 124 } 125 126 public function get_name() 127 { 128 return $this->name; 129 } 130 131 public function get_comment() 132 { 133 return $this->comment; 134 } 135 136 // Format the trace flags for display. 137 private function format_trace_flags() 138 { 139 $flags = array(); 140 if (!$this->already_invoked) 141 { 142 $flags[] = 'first_time'; 143 } 144 if (!$this->is_needed()) 145 { 146 $flags[] = 'not_needed'; 147 } 148 149 return (count($flags)) ? '('.join(', ', $flags).')' : ''; 150 } 151 152 public function invoke($args) 153 { 154 if ($this->trace) 155 { 156 pake_echo_action('invoke', $this->name.' '.$this->format_trace_flags()); 157 } 158 159 // return if already invoked 160 if ($this->already_invoked) return; 161 $this->already_invoked = true; 162 163 // run prerequisites 164 $tasks = self::get_tasks(); 165 foreach ($this->prerequisites as $prerequisite) 166 { 167 $real_prerequisite = self::get_full_task_name($prerequisite); 168 if (array_key_exists($real_prerequisite, $tasks)) 169 { 170 $tasks[$real_prerequisite]->invoke($args); 171 } 172 else 173 { 174 throw new pakeException(sprintf('Prerequisite "%s" does not exist.', $prerequisite)); 175 } 176 } 177 178 // only run if needed 179 if ($this->is_needed()) 180 { 181 return $this->execute($args); 182 } 183 } 184 185 public function execute($args) 186 { 187 if ($this->dryrun) 188 { 189 pake_echo_action('execute', '(dry run) '.$this->name); 190 return; 191 } 192 193 if ($this->trace) 194 { 195 pake_echo_action('execute', $this->name); 196 } 197 198 // action to run 199 $function = ($this->get_alias() ? $this->get_alias() : $this->get_name()); 200 if ($pos = strpos($function, '::')) 201 { 202 $function = array(substr($function, 0, $pos), preg_replace('/\-/', '_', 'run_'.strtolower(substr($function, $pos + 2)))); 203 if (!is_callable($function)) 204 { 205 throw new pakeException(sprintf('Task "%s" is defined but with no action defined.', $function[1])); 206 } 207 } 208 else 209 { 210 $function = preg_replace('/\-/', '_', 'run_'.strtolower($function)); 211 if (!function_exists($function)) 212 { 213 throw new pakeException(sprintf('Task "%s" is defined but with no action defined.', $this->name)); 214 } 215 } 216 217 // execute action 218 return call_user_func_array($function, array($this, $args)); 219 } 220 221 public function is_needed() 222 { 223 return true; 224 } 225 226 public function timestamp() 227 { 228 $max = 0; 229 foreach ($this->prerequisites as $prerequisite) 230 { 231 $t = pakeTask::get($prerequisite)->timestamp(); 232 if ($t > $max) $max = $t; 233 } 234 235 return ($max ? $max : time()); 236 } 237 238 public static function define_task($name, $deps = null) 239 { 240 $task = pakeTask::lookup($name, 'pakeTask'); 241 $task->add_comment(); 242 $task->enhance($deps); 243 } 244 245 public static function define_alias($alias, $name) 246 { 247 self::$ALIAS[$alias] = $name; 248 } 249 250 public static function lookup($task_name, $class = 'pakeTask') 251 { 252 $tasks = self::get_tasks(); 253 $task_name = self::get_full_task_name($task_name); 254 if (!array_key_exists($task_name, $tasks)) 255 { 256 pakeTask::$TASKS[$task_name] = new $class($task_name); 257 } 258 259 return pakeTask::$TASKS[$task_name]; 260 } 261 262 public static function get($task_name) 263 { 264 $tasks = self::get_tasks(); 265 $task_name = self::get_full_task_name($task_name); 266 if (!array_key_exists($task_name, $tasks)) 267 { 268 throw new pakeException(sprintf('Task "%s" is not defined.', $task_name)); 269 } 270 271 return $tasks[$task_name]; 272 } 273 274 public static function get_full_task_name($task_name) 275 { 276 foreach (self::get_tasks() as $task) 277 { 278 $mini_task_name = self::get_mini_task_name($task->get_name()); 279 if ($mini_task_name == $task_name) 280 { 281 return $task->get_name(); 282 } 283 } 284 285 return $task_name; 286 } 287 288 public static function get_mini_task_name($task_name) 289 { 290 $is_method_task = strpos($task_name, '::'); 291 return ($is_method_task ? substr($task_name, $is_method_task + 2) : $task_name); 292 } 293 294 public static function define_comment($comment) 295 { 296 pakeTask::$last_comment = $comment; 297 } 298 299 public function add_comment() 300 { 301 if (!pakeTask::$last_comment) return; 302 if ($this->comment) 303 { 304 $this->comment .= ' / '; 305 } 306 307 $this->comment .= pakeTask::$last_comment; 308 pakeTask::$last_comment = ''; 309 } 310 }
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 |