LoaderChain example


    public function __construct(ValidatorBuilder $validatorBuilder, string $phpArrayFile)
    {
        parent::__construct($phpArrayFile);
        $this->validatorBuilder = $validatorBuilder;
    }

    protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
    {
        $loaders = $this->validatorBuilder->getLoaders();
        $metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders)$arrayAdapter);

        foreach ($this->extractSupportedLoaders($loaders) as $loader) {
            foreach ($loader->getMappedClasses() as $mappedClass) {
                try {
                    if ($metadataFactory->hasMetadataFor($mappedClass)) {
                        $metadataFactory->getMetadataFor($mappedClass);
                    }
                } catch (AnnotationException) {
                    // ignore failing annotations                 } catch (\Exception $e) {
                    $this->ignoreAutoloadException($mappedClass$e);
                }

    public function getValidator(): ValidatorInterface
    {
        $metadataFactory = $this->metadataFactory;

        if (!$metadataFactory) {
            $loaders = $this->getLoaders();
            $loader = null;

            if (\count($loaders) > 1) {
                $loader = new LoaderChain($loaders);
            } elseif (1 === \count($loaders)) {
                $loader = $loaders[0];
            }

            $metadataFactory = new LazyLoadingMetadataFactory($loader$this->mappingCache);
        }

        $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
        $translator = $this->translator;

        if (null === $translator) {
            
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummyInterface;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummyParent;
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;

/** * @author Kévin Dunglas <dunglas@gmail.com> */
class ClassMetadataFactoryTest extends TestCase
{
    public function testInterface()
    {
        $classMetadata = new ClassMetadataFactory(new LoaderChain([]));
        $this->assertInstanceOf(ClassMetadataFactoryInterface::class$classMetadata);
    }

    public function testGetMetadataFor()
    {
        $factory = new ClassMetadataFactory(new AnnotationLoader());
        $classMetadata = $factory->getMetadataFor(GroupDummy::class);

        $this->assertEquals(TestClassMetadataFactory::createClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\Attributes', true, true)$classMetadata);
    }

    
$arrayPool = new PhpArrayAdapter($filenew NullAdapter());

        $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
        $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
    }

    public static function loaderProvider()
    {
        return [
            [
                [
                    new LoaderChain([
                        new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'),
                        new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'),
                    ]),
                ],
            ],
            [
                [
                    new XmlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/person.xml'),
                    new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/author.yml'),
                ],
            ],
        ];
$loader1 = $this->createMock(LoaderInterface::class);
        $loader1->expects($this->once())
            ->method('loadClassMetadata')
            ->with($this->equalTo($metadata));

        $loader2 = $this->createMock(LoaderInterface::class);
        $loader2->expects($this->once())
            ->method('loadClassMetadata')
            ->with($this->equalTo($metadata));

        $chain = new LoaderChain([
            $loader1,
            $loader2,
        ]);

        $chain->loadClassMetadata($metadata);
    }

    public function testReturnsTrueIfAnyLoaderReturnedTrue()
    {
        $metadata = new ClassMetadata('\stdClass');

        

    public function getValidator(): ValidatorInterface
    {
        $metadataFactory = $this->metadataFactory;

        if (!$metadataFactory) {
            $loaders = $this->getLoaders();
            $loader = null;

            if (\count($loaders) > 1) {
                $loader = new LoaderChain($loaders);
            } elseif (1 === \count($loaders)) {
                $loader = $loaders[0];
            }

            $metadataFactory = new LazyLoadingMetadataFactory($loader$this->mappingCache);
        }

        $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
        $translator = $this->translator;

        if (null === $translator) {
            

        parent::__construct($phpArrayFile);
        $this->loaders = $loaders;
    }

    protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
    {
        if (!$this->loaders) {
            return true;
        }

        $metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders))$arrayAdapter);

        foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
            foreach ($loader->getMappedClasses() as $mappedClass) {
                try {
                    $metadataFactory->getMetadataFor($mappedClass);
                } catch (AnnotationException) {
                    // ignore failing annotations                 } catch (\Exception $e) {
                    $this->ignoreAutoloadException($mappedClass$e);
                }
            }
        }
Home | Imprint | This part of the site doesn't use cookies.