[ 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 * sfValidatorConfigHandler allows you to register validators with the system. 14 * 15 * @package symfony 16 * @subpackage config 17 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 18 * @author Sean Kerr <skerr@mojavi.org> 19 * @version SVN: $Id: sfValidatorConfigHandler.class.php 3410 2007-02-06 08:11:38Z fabien $ 20 */ 21 class sfValidatorConfigHandler extends sfYamlConfigHandler 22 { 23 /** 24 * Executes this configuration handler. 25 * 26 * @param array An array of absolute filesystem path to a configuration file 27 * 28 * @return string Data to be written to a cache file 29 * 30 * @throws sfConfigurationException If a requested configuration file does not exist or is not readable 31 * @throws sfParseException If a requested configuration file is improperly formatted 32 */ 33 public function execute($configFiles) 34 { 35 // parse the yaml 36 $config = $this->parseYamls($configFiles); 37 38 // alternate format? 39 if (isset($config['fields'])) 40 { 41 $this->convertAlternate2Standard($config); 42 } 43 44 foreach (array('methods', 'names') as $category) 45 { 46 if (!isset($config[$category])) 47 { 48 throw new sfParseException(sprintf('Configuration file "%s" is missing "%s" category', $configFiles[0], $category)); 49 } 50 } 51 52 // init our data, includes, methods, names and validators arrays 53 $data = array(); 54 $includes = array(); 55 $methods = array(); 56 $names = array(); 57 $validators = array(); 58 59 // get a list of methods and their registered files/parameters 60 foreach ($config['methods'] as $method => $list) 61 { 62 $method = strtoupper($method); 63 64 if (!isset($methods[$method])) 65 { 66 // make sure that this method is GET or POST 67 if ($method != 'GET' && $method != 'POST') 68 { 69 // unsupported request method 70 $error = sprintf('Configuration file "%s" specifies unsupported request method "%s"', $configFiles[0], $method); 71 72 throw new sfParseException($error); 73 } 74 75 // create our method 76 $methods[$method] = array(); 77 } 78 79 if (!count($list)) 80 { 81 // we have an empty list of names 82 continue; 83 } 84 85 // load name list 86 $this->loadNames($configFiles, $method, $methods, $names, $config, $list); 87 } 88 89 // load attribute list 90 $this->loadAttributes($configFiles, $methods, $names, $validators, $config, $list); 91 92 // fill-in filter configuration 93 $fillin = var_export(isset($config['fillin']) ? $config['fillin'] : array(), true); 94 95 // generate GET file/parameter data 96 97 $data[] = "if (\$_SERVER['REQUEST_METHOD'] == 'GET')"; 98 $data[] = "{"; 99 100 $ret = $this->generateRegistration('GET', $data, $methods, $names, $validators); 101 102 if ($ret) 103 { 104 $data[] = sprintf(" \$context->getRequest()->setAttribute('fillin', %s, 'symfony/filter');", $fillin); 105 } 106 107 // generate POST file/parameter data 108 109 $data[] = "}"; 110 $data[] = "else if (\$_SERVER['REQUEST_METHOD'] == 'POST')"; 111 $data[] = "{"; 112 113 $ret = $this->generateRegistration('POST', $data, $methods, $names, $validators); 114 115 if ($ret) 116 { 117 $data[] = sprintf(" \$context->getRequest()->setAttribute('fillin', %s, 'symfony/filter');", $fillin); 118 } 119 120 $data[] = "}"; 121 122 // compile data 123 $retval = sprintf("<?php\n". 124 "// auto-generated by sfValidatorConfigHandler\n". 125 "// date: %s\n%s\n%s\n", date('Y/m/d H:i:s'), 126 implode("\n", $includes), implode("\n", $data)); 127 128 return $retval; 129 } 130 131 /** 132 * Generates raw cache data. 133 * 134 * @param string A request method 135 * @param array The data array where our cache code will be appended 136 * @param array An associative array of request method data 137 * @param array An associative array of file/parameter data 138 * @param array A validators array 139 * 140 * @return boolean Returns true if there is some validators for this file/parameter 141 */ 142 protected function generateRegistration($method, &$data, &$methods, &$names, &$validators) 143 { 144 // setup validator array 145 $data[] = " \$validators = array();"; 146 147 if (!isset($methods[$method])) 148 { 149 $methods[$method] = array(); 150 } 151 152 // determine which validators we need to create for this request method 153 foreach ($methods[$method] as $name) 154 { 155 if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match)) 156 { 157 // this file/parameter has a parent 158 $subname = $match[2]; 159 $parent = $match[1]; 160 161 $valList = $names[$parent][$subname]['validators']; 162 } 163 else 164 { 165 // no parent 166 $valList = $names[$name]['validators']; 167 } 168 169 if ($valList == null) 170 { 171 // no validator list for this file/parameter 172 continue; 173 } 174 175 foreach ($valList as $valName) 176 { 177 if (isset($validators[$valName]) && !isset($validators[$valName][$method])) 178 { 179 // retrieve this validator's info 180 $validator =& $validators[$valName]; 181 182 $data[] = sprintf(" \$validators['%s'] = new %s();\n". 183 " \$validators['%s']->initialize(%s, %s);", 184 $valName, $validator['class'], $valName, '$context', $validator['parameters']); 185 186 // mark this validator as created for this request method 187 $validators[$valName][$method] = true; 188 } 189 } 190 } 191 192 foreach ($methods[$method] as $name) 193 { 194 if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match)) 195 { 196 // this file/parameter has a parent 197 $subname = $match[2]; 198 $parent = $match[1]; 199 $name = $match[2]; 200 201 $attributes = $names[$parent][$subname]; 202 } 203 else 204 { 205 // no parent 206 $attributes = $names[$name]; 207 } 208 209 // register file/parameter 210 $data[] = sprintf(" \$validatorManager->registerName('%s', %s, %s, %s, %s, %s);", 211 $name, $attributes['required'] ? 1 : 0, 212 isset($attributes['required_msg']) ? $attributes['required_msg'] : "''", 213 $attributes['parent'], $attributes['group'], 214 $attributes['file']); 215 216 // register validators for this file/parameter 217 foreach ($attributes['validators'] as &$validator) 218 { 219 $data[] = sprintf(" \$validatorManager->registerValidator('%s', %s, %s);", $name, 220 "\$validators['$validator']", 221 $attributes['parent']); 222 } 223 } 224 225 return count($methods[$method]) ? true : false; 226 } 227 228 /** 229 * Loads the linear list of attributes from the [names] category. 230 * 231 * @param string The configuration file name (for exception usage) 232 * @param array An associative array of request method data 233 * @param array An associative array of file/parameter names in which to store loaded information 234 * @param array An associative array of validator data 235 * @param array The loaded ini configuration that we'll use for verification purposes 236 * @param string A comma delimited list of file/parameter names 237 */ 238 protected function loadAttributes(&$configFiles, &$methods, &$names, &$validators, &$config, &$list) 239 { 240 foreach ($config['names'] as $name => $attributes) 241 { 242 // get a reference to the name entry 243 if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match)) 244 { 245 // this name entry has a parent 246 $subname = $match[2]; 247 $parent = $match[1]; 248 249 if (!isset($names[$parent][$subname])) 250 { 251 // unknown parent or subname 252 $error = sprintf('Configuration file "%s" specifies unregistered parent "%s" or subname "%s"', $configFiles[0], $parent, $subname); 253 throw new sfParseException($error); 254 } 255 256 $entry =& $names[$parent][$subname]; 257 } 258 else 259 { 260 // no parent 261 if (!isset($names[$name])) 262 { 263 // unknown name 264 $error = sprintf('Configuration file "%s" specifies unregistered name "%s"', $configFiles[0], $name); 265 throw new sfParseException($error); 266 } 267 268 $entry =& $names[$name]; 269 } 270 271 foreach ($attributes as $attribute => $value) 272 { 273 if ($attribute == 'validators') 274 { 275 // load validators for this file/parameter name 276 $this->loadValidators($configFiles, $validators, $config, $value, $entry); 277 } 278 else if ($attribute == 'type') 279 { 280 // name type 281 $lvalue = strtolower($value); 282 $entry['file'] = ($lvalue == 'file' ? 'true' : 'false'); 283 } 284 else 285 { 286 // just a normal attribute 287 $entry[$attribute] = sfToolkit::literalize($value, true); 288 } 289 } 290 } 291 } 292 293 /** 294 * Loads all request methods and the file/parameter names that will be 295 * validated from the [methods] category. 296 * 297 * @param string The configuration file name (for exception usage) 298 * @param string A request method 299 * @param array An associative array of request method data 300 * @param array An associative array of file/parameter names in which to store loaded information 301 * @param array The loaded ini configuration that we'll use for verification purposes 302 * @param string A comma delimited list of file/parameter names 303 */ 304 protected function loadNames(&$configFiles, &$method, &$methods, &$names, &$config, &$list) 305 { 306 // explode the list of names 307 $array = $list; 308 309 // loop through the names 310 foreach ($array as $name) 311 { 312 // make sure we have the required status of this file or parameter 313 if (!isset($config['names'][$name]['required'])) 314 { 315 // missing 'required' attribute 316 $error = sprintf('Configuration file "%s" specifies file or parameter "%s", but it is missing the "required" attribute', $configFiles[0], $name); 317 throw new sfParseException($error); 318 } 319 320 // determine parent status 321 if (preg_match('/^([a-z0-9_-]+)\{([a-z0-9\s_-]+)\}$/i', $name, $match)) 322 { 323 // this name has a parent 324 $subname = $match[2]; 325 $parent = $match[1]; 326 327 if (!isset($names[$parent]) || !isset($names[$parent][$name])) 328 { 329 if (!isset($names[$parent])) 330 { 331 // create our parent 332 $names[$parent] = array('_is_parent' => true); 333 } 334 335 // create our new name entry 336 $entry = array(); 337 $entry['file'] = 'false'; 338 $entry['group'] = 'null'; 339 $entry['parent'] = "'$parent'"; 340 $entry['required'] = 'true'; 341 $entry['required_msg'] = "'Required'"; 342 $entry['validators'] = array(); 343 344 // add our name entry 345 $names[$parent][$subname] = $entry; 346 } 347 } 348 else if (strpos($name, '{') !== false || strpos($name, '}') !== false) 349 { 350 // name contains an invalid character 351 // this is most likely a typo where the user forgot to add a brace 352 $error = sprintf('Configuration file "%s" specifies method "%s" with invalid file/parameter name "%s"', $configFiles[0], $method, $name); 353 throw new sfParseException($error); 354 } 355 else 356 { 357 // no parent 358 if (!isset($names[$name])) 359 { 360 // create our new name entry 361 $entry = array(); 362 $entry['file'] = 'false'; 363 $entry['group'] = 'null'; 364 $entry['parent'] = 'null'; 365 $entry['required'] = 'true'; 366 $entry['required_msg'] = "'Required'"; 367 $entry['type'] = 'parameter'; 368 $entry['validators'] = array(); 369 370 // add our name entry 371 $names[$name] = $entry; 372 } 373 } 374 375 // add this name to the current request method 376 $methods[$method][] = $name; 377 } 378 } 379 380 /** 381 * Loads a list of validators. 382 * 383 * @param string The configuration file name (for exception usage) 384 * @param array An associative array of validator data 385 * @param array The loaded ini configuration that we'll use for verification purposes 386 * @param string A comma delimited list of validator names 387 * @param array A file/parameter name entry 388 */ 389 protected function loadValidators(&$configFiles, &$validators, &$config, &$list, &$entry) 390 { 391 // create our empty entry validator array 392 $entry['validators'] = array(); 393 394 if (!$list || (!is_array($list) && trim($list) == '')) 395 { 396 // skip the empty list 397 return; 398 } 399 400 // get our validator array 401 $array = is_array($list) ? $list : explode(',', $list); 402 403 foreach ($array as $validator) 404 { 405 $validator = trim($validator); 406 407 // add this validator name to our entry 408 $entry['validators'][] = $validator; 409 410 // make sure the specified validator exists 411 if (!isset($config[$validator])) 412 { 413 // validator hasn't been registered 414 $error = sprintf('Configuration file "%s" specifies unregistered validator "%s"', $configFiles[0], $validator); 415 throw new sfParseException($error); 416 } 417 418 // has it already been registered? 419 if (isset($validators[$validator])) 420 { 421 continue; 422 } 423 424 if (!isset($config[$validator]['class'])) 425 { 426 // missing class key 427 $error = sprintf('Configuration file "%s" specifies category "%s" with missing class key', $configFiles[0], $validator); 428 throw new sfParseException($error); 429 } 430 431 // create our validator 432 $validators[$validator] = array(); 433 $validators[$validator]['class'] = $config[$validator]['class']; 434 $validators[$validator]['file'] = null; 435 $validators[$validator]['parameters'] = null; 436 437 if (isset($config[$validator]['file'])) 438 { 439 // we have a file for this validator 440 $file = $config[$validator]['file']; 441 442 // keyword replacement 443 $file = $this->replaceConstants($file); 444 $file = $this->replacePath($file); 445 446 if (!is_readable($file)) 447 { 448 // file doesn't exist 449 $error = sprintf('Configuration file "%s" specifies category "%s" with nonexistent or unreadable file "%s"', $configFiles[0], $validator, $file); 450 throw new sfParseException($error); 451 } 452 453 $validators[$validator]['file'] = $file; 454 } 455 456 // parse parameters 457 $parameters = (isset($config[$validator]['param']) ? var_export($config[$validator]['param'], true) : 'null'); 458 459 $validators[$validator]['parameters'] = $parameters; 460 } 461 } 462 463 /** 464 * Converts alternate format to standard format. 465 * 466 * @param array Configuration data 467 */ 468 protected function convertAlternate2Standard(&$config) 469 { 470 $defaultMethods = isset($config['methods']) ? $config['methods'] : array('post'); 471 $config['methods'] = array(); 472 473 // validators 474 if (isset($config['validators'])) 475 { 476 foreach ((array) $config['validators'] as $validator => $params) 477 { 478 $config[$validator] = $params; 479 } 480 481 unset($config['validators']); 482 } 483 484 // names 485 $config['names'] = $config['fields']; 486 unset($config['fields']); 487 488 foreach ($config['names'] as $name => $values) 489 { 490 // validators 491 $validators = array(); 492 foreach ($values as $validator => $params) 493 { 494 if (in_array($validator, array('required', 'group', 'group_msg', 'parent', 'file', 'methods'))) 495 { 496 continue; 497 } 498 499 // class or validator 500 if (!isset($config[$validator])) 501 { 502 $config[$validator] = array('class' => $validator); 503 } 504 505 $validatorName = $validator; 506 if ($params) 507 { 508 // create a new validator 509 $validatorName = $validator.'_'.$name; 510 $config[$validatorName] = $config[$validator]; 511 $config[$validatorName]['param'] = array_merge(isset($config[$validator]['param']) ? (array) $config[$validator]['param'] : array(), $params); 512 } 513 514 $validators[] = $validatorName; 515 516 unset($values[$validator]); 517 } 518 $values['validators'] = $validators; 519 520 // group 521 if (isset($values['group']) && isset($values['group_msg'])) 522 { 523 $values['required_msg'] = $values['group_msg']; 524 } 525 526 // required 527 if (isset($values['required'])) 528 { 529 $values['required_msg'] = $values['required']['msg']; 530 $values['required'] = true; 531 } 532 else 533 { 534 $values['required'] = false; 535 } 536 537 // methods 538 if (isset($values['methods'])) 539 { 540 $methods = (array) $values['methods']; 541 unset($values['methods']); 542 } 543 else 544 { 545 $methods = $defaultMethods; 546 } 547 foreach ($methods as $method) 548 { 549 $config['methods'][$method][] = $name; 550 } 551 552 $config['names'][$name] = $values; 553 } 554 } 555 }
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 |