Drupal investigation

AbstractLoader.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Mapping\Loader;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Exception\MappingException;
  13. /**
  14. * Base loader for validation metadata.
  15. *
  16. * This loader supports the loading of constraints from Symfony's default
  17. * namespace (see {@link DEFAULT_NAMESPACE}) using the short class names of
  18. * those constraints. Constraints can also be loaded using their fully
  19. * qualified class names. At last, namespace aliases can be defined to load
  20. * constraints with the syntax "alias:ShortName".
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. */
  24. abstract class AbstractLoader implements LoaderInterface
  25. {
  26. /**
  27. * The namespace to load constraints from by default.
  28. */
  29. const DEFAULT_NAMESPACE = '\\Symfony\\Component\\Validator\\Constraints\\';
  30. /**
  31. * @var array
  32. */
  33. protected $namespaces = array();
  34. /**
  35. * Adds a namespace alias.
  36. *
  37. * The namespace alias can be used to reference constraints from specific
  38. * namespaces in {@link newConstraint()}:
  39. *
  40. * $this->addNamespaceAlias('mynamespace', '\\Acme\\Package\\Constraints\\');
  41. *
  42. * $constraint = $this->newConstraint('mynamespace:NotNull');
  43. *
  44. * @param string $alias The alias
  45. * @param string $namespace The PHP namespace
  46. */
  47. protected function addNamespaceAlias($alias, $namespace)
  48. {
  49. $this->namespaces[$alias] = $namespace;
  50. }
  51. /**
  52. * Creates a new constraint instance for the given constraint name.
  53. *
  54. * @param string $name The constraint name. Either a constraint relative
  55. * to the default constraint namespace, or a fully
  56. * qualified class name. Alternatively, the constraint
  57. * may be preceded by a namespace alias and a colon.
  58. * The namespace alias must have been defined using
  59. * {@link addNamespaceAlias()}.
  60. * @param mixed $options The constraint options
  61. *
  62. * @return Constraint
  63. *
  64. * @throws MappingException If the namespace prefix is undefined
  65. */
  66. protected function newConstraint($name, $options = null)
  67. {
  68. if (strpos($name, '\\') !== false && class_exists($name)) {
  69. $className = (string) $name;
  70. } elseif (strpos($name, ':') !== false) {
  71. list($prefix, $className) = explode(':', $name, 2);
  72. if (!isset($this->namespaces[$prefix])) {
  73. throw new MappingException(sprintf('Undefined namespace prefix "%s"', $prefix));
  74. }
  75. $className = $this->namespaces[$prefix].$className;
  76. } else {
  77. $className = self::DEFAULT_NAMESPACE.$name;
  78. }
  79. return new $className($options);
  80. }
  81. }