LoaderChain.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Serializer\Mapping\Loader;
  11. use Symfony\Component\Serializer\Exception\MappingException;
  12. use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
  13. /**
  14. * Calls multiple {@link LoaderInterface} instances in a chain.
  15. *
  16. * This class accepts multiple instances of LoaderInterface to be passed to the
  17. * constructor. When {@link loadClassMetadata()} is called, the same method is called
  18. * in <em>all</em> of these loaders, regardless of whether any of them was
  19. * successful or not.
  20. *
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. * @author Kévin Dunglas <dunglas@gmail.com>
  23. */
  24. class LoaderChain implements LoaderInterface
  25. {
  26. /**
  27. * @var LoaderInterface[]
  28. */
  29. private $loaders;
  30. /**
  31. * Accepts a list of LoaderInterface instances.
  32. *
  33. * @param LoaderInterface[] $loaders An array of LoaderInterface instances
  34. *
  35. * @throws MappingException If any of the loaders does not implement LoaderInterface
  36. */
  37. public function __construct(array $loaders)
  38. {
  39. foreach ($loaders as $loader) {
  40. if (!$loader instanceof LoaderInterface) {
  41. throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', get_class($loader)));
  42. }
  43. }
  44. $this->loaders = $loaders;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function loadClassMetadata(ClassMetadataInterface $metadata)
  50. {
  51. $success = false;
  52. foreach ($this->loaders as $loader) {
  53. $success = $loader->loadClassMetadata($metadata) || $success;
  54. }
  55. return $success;
  56. }
  57. }