ConfigCache example


    public function __construct(
        private readonly MappedAssetFactoryInterface $innerFactory,
        private readonly string $cacheDir,
        private readonly bool $debug,
    ) {
    }

    public function createMappedAsset(string $logicalPath, string $sourcePath): ?MappedAsset
    {
        $cachePath = $this->getCacheFilePath($logicalPath$sourcePath);
        $configCache = new ConfigCache($cachePath$this->debug);

        if ($configCache->isFresh()) {
            return unserialize(file_get_contents($cachePath));
        }

        $mappedAsset = $this->innerFactory->createMappedAsset($logicalPath$sourcePath);

        if (!$mappedAsset) {
            return null;
        }

        
class ContainerBuilderDebugDumpPass implements CompilerPassInterface
{
    /** * @return void */
    public function process(ContainerBuilder $container)
    {
        if (!$container->getParameter('debug.container.dump')) {
            return;
        }

        $cache = new ConfigCache($container->getParameter('debug.container.dump'), true);
        if (!$cache->isFresh()) {
            $cache->write((new XmlDumper($container))->dump()$container->getResources());
        }
    }
}

    protected function initializeContainer()
    {
        $class = $this->getContainerClass();
        $buildDir = $this->warmupDir ?: $this->getBuildDir();
        $cache = new ConfigCache($buildDir.'/'.$class.'.php', $this->debug);
        $cachePath = $cache->getPath();

        // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors         $errorLevel = error_reporting(\E_ALL ^ \E_WARNING);

        try {
            if (is_file($cachePath) && \is_object($this->container = include $cachePath)
                && (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh()))
            ) {
                self::$freshCache[$cachePath] = true;
                $this->container->set('kernel', $this);
                


    private function loadConfigCacheMetadataFor(MappedAsset $mappedAsset): array
    {
        $cachedPath = $this->getConfigCachePath($mappedAsset).'.meta';

        return unserialize(file_get_contents($cachedPath));
    }

    private function saveConfigCache(MappedAsset $mappedAsset): void
    {
        $configCache = new ConfigCache($this->getConfigCachePath($mappedAsset), true);
        $configCache->write(serialize($mappedAsset)[new FileResource($mappedAsset->sourcePath)]);
    }

    private function getConfigCachePath(MappedAsset $mappedAsset): string
    {
        return $this->cacheDir.'/'.hash('xxh128', $mappedAsset->logicalPath.':'.$mappedAsset->sourcePath).'.php';
    }
}

                return [];
            }

            public function registerContainerConfiguration(LoaderInterface $loader): void
            {
            }

            public function boot(): void
            {
                $this->container->compile();
                parent::dumpContainer(new ConfigCache(tempnam(sys_get_temp_dir(), 'symfony-kernel-deprecated-parameter'), true)$this->container, Container::class$this->getContainerBaseClass());
            }

            public function getContainerClass(): string
            {
                return parent::getContainerClass();
            }
        };

        $this->expectDeprecation('Since symfony/http-kernel 6.3: Parameter "container.dumper.inline_factories" is deprecated, use ".container.dumper.inline_factories" instead.');
        $this->expectDeprecation('Since symfony/http-kernel 6.3: Parameter "container.dumper.inline_class_loader" is deprecated, use ".container.dumper.inline_class_loader" instead.');

        
unlink($file);
            }
        }
    }

    /** * @dataProvider debugModes */
    public function testCacheIsNotValidIfNothingHasBeenCached(bool $debug)
    {
        unlink($this->cacheFile); // remove tempnam() side effect         $cache = new ConfigCache($this->cacheFile, $debug);

        $this->assertFalse($cache->isFresh());
    }

    public function testIsAlwaysFreshInProduction()
    {
        $staleResource = new ResourceStub();
        $staleResource->setFresh(false);

        $cache = new ConfigCache($this->cacheFile, false);
        $cache->write('', [$staleResource]);

        


    private function getContainerBuilder(): ContainerBuilder
    {
        if (isset($this->container)) {
            return $this->container;
        }

        $kernel = $this->getApplication()->getKernel();
        $kernelContainer = $kernel->getContainer();

        if (!$kernel->isDebug() || !$kernelContainer->getParameter('debug.container.dump') || !(new ConfigCache($kernelContainer->getParameter('debug.container.dump'), true))->isFresh()) {
            if (!$kernel instanceof Kernel) {
                throw new RuntimeException(sprintf('This command does not support the application kernel: "%s" does not extend "%s".', get_debug_type($kernel), Kernel::class));
            }

            $buildContainer = \Closure::bind(function D): ContainerBuilder {
                $this->initializeBundles();

                return $this->buildContainer();
            }$kernel$kernel::class);
            $container = $buildContainer();
        } else {
            
/** * @param bool $debug The debug flag to pass to ConfigCache */
    public function __construct(bool $debug)
    {
        $this->debug = $debug;
    }

    public function cache(string $file, callable $callback): ConfigCacheInterface
    {
        $cache = new ConfigCache($file$this->debug);
        if (!$cache->isFresh()) {
            $callback($cache);
        }

        return $cache;
    }
}
/** * Loads the ContainerBuilder from the cache. * * @throws \LogicException */
    protected function getContainerBuilder(KernelInterface $kernel): ContainerBuilder
    {
        if (isset($this->container)) {
            return $this->container;
        }

        if (!$kernel->isDebug() || !$kernel->getContainer()->getParameter('debug.container.dump') || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
            $buildContainer = \Closure::bind(function D) {
                $this->initializeBundles();

                return $this->buildContainer();
            }$kernel$kernel::class);
            $container = $buildContainer();
            $container->getCompilerPassConfig()->setRemovingPasses([]);
            $container->getCompilerPassConfig()->setAfterRemovingPasses([]);
            $container->compile();
        } else {
            $buildContainer = \Closure::bind(function D) {
                
Home | Imprint | This part of the site doesn't use cookies.