LoaderChain.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 from multiple {@link LoaderInterface} instances.
  15. *
  16. * Pass the loaders when constructing the chain. Once
  17. * {@link loadClassMetadata()} is called, that method will be called on all
  18. * loaders in the chain.
  19. *
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. */
  22. class LoaderChain implements LoaderInterface
  23. {
  24. protected $loaders;
  25. /**
  26. * @param LoaderInterface[] $loaders The metadata loaders to use
  27. *
  28. * @throws MappingException If any of the loaders has an invalid type
  29. */
  30. public function __construct(array $loaders)
  31. {
  32. foreach ($loaders as $loader) {
  33. if (!$loader instanceof LoaderInterface) {
  34. throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', \get_class($loader)));
  35. }
  36. }
  37. $this->loaders = $loaders;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function loadClassMetadata(ClassMetadata $metadata)
  43. {
  44. $success = false;
  45. foreach ($this->loaders as $loader) {
  46. $success = $loader->loadClassMetadata($metadata) || $success;
  47. }
  48. return $success;
  49. }
  50. }