Drupal investigation

StaticMethodLoader.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\Exception\MappingException;
  12. use Symfony\Component\Validator\Mapping\ClassMetadata;
  13. /**
  14. * Loads validation metadata by calling a static method on the loaded class.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class StaticMethodLoader implements LoaderInterface
  19. {
  20. /**
  21. * The name of the method to call.
  22. *
  23. * @var string
  24. */
  25. protected $methodName;
  26. /**
  27. * Creates a new loader.
  28. *
  29. * @param string $methodName The name of the static method to call
  30. */
  31. public function __construct($methodName = 'loadValidatorMetadata')
  32. {
  33. $this->methodName = $methodName;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function loadClassMetadata(ClassMetadata $metadata)
  39. {
  40. /** @var \ReflectionClass $reflClass */
  41. $reflClass = $metadata->getReflectionClass();
  42. if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) {
  43. $reflMethod = $reflClass->getMethod($this->methodName);
  44. if ($reflMethod->isAbstract()) {
  45. return false;
  46. }
  47. if (!$reflMethod->isStatic()) {
  48. throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->name, $this->methodName));
  49. }
  50. if ($reflMethod->getDeclaringClass()->name != $reflClass->name) {
  51. return false;
  52. }
  53. $reflMethod->invoke(null, $metadata);
  54. return true;
  55. }
  56. return false;
  57. }
  58. }