Chris@0:
Chris@0: *
Chris@0: * For the full copyright and license information, please view the LICENSE
Chris@0: * file that was distributed with this source code.
Chris@0: */
Chris@0:
Chris@0: namespace Symfony\Component\Serializer\Mapping\Loader;
Chris@0:
Chris@0: use Symfony\Component\Serializer\Exception\MappingException;
Chris@0: use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
Chris@0:
Chris@0: /**
Chris@0: * Calls multiple {@link LoaderInterface} instances in a chain.
Chris@0: *
Chris@0: * This class accepts multiple instances of LoaderInterface to be passed to the
Chris@0: * constructor. When {@link loadClassMetadata()} is called, the same method is called
Chris@0: * in all of these loaders, regardless of whether any of them was
Chris@0: * successful or not.
Chris@0: *
Chris@0: * @author Bernhard Schussek
Chris@0: * @author Kévin Dunglas
Chris@0: */
Chris@0: class LoaderChain implements LoaderInterface
Chris@0: {
Chris@0: private $loaders;
Chris@0:
Chris@0: /**
Chris@0: * Accepts a list of LoaderInterface instances.
Chris@0: *
Chris@0: * @param LoaderInterface[] $loaders An array of LoaderInterface instances
Chris@0: *
Chris@0: * @throws MappingException If any of the loaders does not implement LoaderInterface
Chris@0: */
Chris@0: public function __construct(array $loaders)
Chris@0: {
Chris@0: foreach ($loaders as $loader) {
Chris@0: if (!$loader instanceof LoaderInterface) {
Chris@14: throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', \get_class($loader)));
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: $this->loaders = $loaders;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * {@inheritdoc}
Chris@0: */
Chris@0: public function loadClassMetadata(ClassMetadataInterface $metadata)
Chris@0: {
Chris@0: $success = false;
Chris@0:
Chris@0: foreach ($this->loaders as $loader) {
Chris@0: $success = $loader->loadClassMetadata($metadata) || $success;
Chris@0: }
Chris@0:
Chris@0: return $success;
Chris@0: }
Chris@14:
Chris@14: /**
Chris@14: * @return LoaderInterface[]
Chris@14: */
Chris@14: public function getLoaders()
Chris@14: {
Chris@14: return $this->loaders;
Chris@14: }
Chris@0: }