Development

/branches/1.1/lib/plugins/sfCompat10Plugin/lib/config/sfValidatorConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfCompat10Plugin/lib/config/sfValidatorConfigHandler.class.php

Revision 10270, 16.8 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

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