LoaderChain.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. private $loaders;
  27. /**
  28. * Accepts a list of LoaderInterface instances.
  29. *
  30. * @param LoaderInterface[] $loaders An array of LoaderInterface instances
  31. *
  32. * @throws MappingException If any of the loaders does not implement LoaderInterface
  33. */
  34. public function __construct(array $loaders)
  35. {
  36. foreach ($loaders as $loader) {
  37. if (!$loader instanceof LoaderInterface) {
  38. throw new MappingException(sprintf('Class "%s" is expected to implement LoaderInterface.', get_debug_type($loader)));
  39. }
  40. }
  41. $this->loaders = $loaders;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function loadClassMetadata(ClassMetadataInterface $metadata)
  47. {
  48. $success = false;
  49. foreach ($this->loaders as $loader) {
  50. $success = $loader->loadClassMetadata($metadata) || $success;
  51. }
  52. return $success;
  53. }
  54. /**
  55. * @return LoaderInterface[]
  56. */
  57. public function getLoaders()
  58. {
  59. return $this->loaders;
  60. }
  61. }