LoaderChain.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_debug_type($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. /**
  51. * @return LoaderInterface[]
  52. */
  53. public function getLoaders()
  54. {
  55. return $this->loaders;
  56. }
  57. }