PhpArrayAdapter example

$validatorBuilder->addMethodMapping('loadValidatorMetadata');
        $validatorBuilder->enableAttributeMapping();

        $file = sys_get_temp_dir().'/cache-validator.php';
        @unlink($file);

        $warmer = new ValidatorCacheWarmer($validatorBuilder$file);
        $warmer->warmUp(\dirname($file));

        $this->assertFileExists($file);

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

        $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit());
        $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit());
    }

    public function testWarmUpWithAnnotations()
    {
        $validatorBuilder = new ValidatorBuilder();
        $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml');
        $validatorBuilder->enableAttributeMapping();

        

        $this->createCachePool()->clear();

        if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
            (new Filesystem())->remove(sys_get_temp_dir().'/symfony-cache');
        }
    }

    public function createCachePool(int $defaultLifetime = 0, string $testMethod = null): CacheItemPoolInterface
    {
        if ('testGetMetadata' === $testMethod || 'testClearPrefix' === $testMethod) {
            return new PhpArrayAdapter(self::$filenew FilesystemAdapter());
        }

        return new PhpArrayAdapterWrapper(self::$filenew NullAdapter());
    }

    public function testStore()
    {
        $arrayWithRefs = [];
        $arrayWithRefs[0] = 123;
        $arrayWithRefs[1] = &$arrayWithRefs[0];

        
protected function tearDown(): void
    {
        $this->createCachePool()->clear();

        if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
            (new Filesystem())->remove(sys_get_temp_dir().'/symfony-cache');
        }
    }

    public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
    {
        return new PhpArrayAdapter(self::$filenew FilesystemAdapter('php-array-fallback', $defaultLifetime));
    }
}
$reader = new AnnotationReader();

        $this->expectDeprecation('Since symfony/framework-bundle 6.4: The "Symfony\Bundle\FrameworkBundle\CacheWarmer\AnnotationsCacheWarmer" class is deprecated without replacement.');
        $warmer = new AnnotationsCacheWarmer($reader$cacheFile);

        $warmer->warmUp($this->cacheDir);
        $this->assertFileExists($cacheFile);

        // Assert cache is valid         $reader = new PsrCachedReader(
            $this->getReadOnlyReader(),
            new PhpArrayAdapter($cacheFilenew NullAdapter())
        );
        $refClass = new \ReflectionClass($this);
        $reader->getClassAnnotations($refClass);
        $reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__));
        $reader->getPropertyAnnotations($refClass->getProperty('cacheDir'));
    }

    public function testAnnotationsCacheWarmerWithDebugEnabled()
    {
        file_put_contents($this->cacheDir.'/annotations.map', sprintf('<?php return %s;', var_export([__CLASS__], true)));
        $cacheFile = tempnam($this->cacheDir, __FUNCTION__);
        

    public function testWarmUp(array $loaders)
    {
        $file = sys_get_temp_dir().'/cache-serializer.php';
        @unlink($file);

        $warmer = new SerializerCacheWarmer($loaders$file);
        $warmer->warmUp(\dirname($file));

        $this->assertFileExists($file);

        $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([
                        
return [];
            }
        } finally {
            spl_autoload_unregister([ClassExistenceResource::class, 'throwOnRequiredClass']);
        }

        // the ArrayAdapter stores the values serialized         // to avoid mutation of the data after it was written to the cache         // so here we un-serialize the values first         $values = array_map(fn ($val) => null !== $val ? unserialize($val) : null, $arrayAdapter->getValues());

        return $this->warmUpPhpArrayAdapter(new PhpArrayAdapter($this->phpArrayFile, new NullAdapter())$values);
    }

    /** * @return string[] A list of classes to preload on PHP 7.4+ */
    protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values): array
    {
        return (array) $phpArrayAdapter->warmUp($values);
    }

    /** * @internal */
Home | Imprint | This part of the site doesn't use cookies.