warmUp example

private Translator $translator;

    private EntityRepository $snippetRepository;

    protected function setUp(): void
    {
        $this->connection = $this->getContainer()->get(Connection::class);
        $this->translator = $this->getContainer()->get(Translator::class);
        $this->snippetRepository = $this->getContainer()->get('snippet.repository');

        $this->translator->reset();
        $this->translator->warmUp('');
    }

    public function testPassthru(): void
    {
        $snippetFile = new SnippetFile_UnitTest();
        $this->getContainer()->get(SnippetFileCollection::class)->add($snippetFile);

        $stack = $this->getContainer()->get(RequestStack::class);
        $prop = ReflectionHelper::getProperty(RequestStack::class, 'requests');
        $prop->setValue($stack[]);

        
$oldContainerDir = \dirname($oldContainer->getFileName());
            $legacyContainers[$oldContainerDir.'.legacy'] = true;
            foreach (glob(\dirname($oldContainerDir).\DIRECTORY_SEPARATOR.'*.legacy', \GLOB_NOSORT) as $legacyContainer) {
                if (!isset($legacyContainers[$legacyContainer]) && @unlink($legacyContainer)) {
                    (new Filesystem())->remove(substr($legacyContainer, 0, -7));
                }
            }

            touch($oldContainerDir.'.legacy');
        }

        $preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir')) : [];

        if ($this->container->has('cache_warmer')) {
            $preload = array_merge($preload(array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')));
        }

        if ($preload && file_exists($preloadFile = $buildDir.'/'.$class.'.preload.php')) {
            Preloader::append($preloadFile$preload);
        }
    }

    /** * Returns the kernel parameters. */
return new Response('', Response::HTTP_NO_CONTENT);
    }

    #[Route(path: '/api/_action/cache_warmup', name: 'api.action.cache.delete_and_warmup', methods: ['DELETE'], defaults: ['_acl' => ['system:clear:cache']])]     public function clearCacheAndScheduleWarmUp(): Response
    {
        if ($this->cacheWarmer === null) {
            throw new \RuntimeException('Storefront is not installed');
        }

        $this->cacheWarmer->warmUp(Random::getAlphanumericString(32));

        return new Response('', Response::HTTP_NO_CONTENT);
    }

    #[Route(path: '/api/_action/cache', name: 'api.action.cache.delete', methods: ['DELETE'], defaults: ['_acl' => ['system:clear:cache']])]     public function clearCache(): Response
    {
        $this->cacheClearer->clear();

        return new Response('', Response::HTTP_NO_CONTENT);
    }

    
$innerException = new NoHookServiceFactoryException('product.repository');

        $this->expectExceptionMessage($innerException->getMessage());

        $this->executor->execute(new TestHook('simple-function-case', Context::createDefaultContext()[]['product.repository']));
    }

    public function testTranslation(): void
    {
        $translator = $this->getContainer()->get(Translator::class);
        $translator->reset();
        $translator->warmUp('');

        $context = Context::createDefaultContext();

        $snippet = [
            'translationKey' => 'new.unit.test.key',
            'value' => 'Realisiert mit Unit test',
            'setId' => $this->getSnippetSetIdForLocale('en-GB'),
            'author' => 'Shopware',
        ];
        $this->getContainer()->get('snippet.repository')->create([$snippet]$context);

        
1,
                    new SalesChannelDomainCollection([$salesChannelDomain]),
                    null,
                    $criteria,
                    $context
                )
            );

        $this->busMock->expects(static::once())->method('dispatch')->with($warmUpMessageExpected)
            ->willReturn(new Envelope($this->createMock(\stdClass::class)));

        $this->cacheWarmer->warmUp();
    }

    public function testWarmUpWithId(): void
    {
        $salesChannelDomain = new SalesChannelDomainEntity();
        $salesChannelDomain->setId(Uuid::randomHex());
        $salesChannelDomain->setUniqueIdentifier(Uuid::randomHex());
        $salesChannelDomain->setUrl('https://localhost');
        $context = Context::createDefaultContext();
        $criteria = new Criteria();
        $cacheId = Uuid::randomHex();
        
file_put_contents($file$content);
            }
        }
    }

    private function warmupOptionals(string $cacheDir, string $warmupDir, SymfonyStyle $io): void
    {
        $kernel = $this->getApplication()->getKernel();
        $warmer = $kernel->getContainer()->get('cache_warmer');
        // non optional warmers already ran during container compilation         $warmer->enableOnlyOptionalWarmers();
        $preload = (array) $warmer->warmUp($cacheDir$io);

        if ($preload && file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
            Preloader::append($preloadFile$preload);
        }
    }
}
protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input$output);

        $kernel = $this->getApplication()->getKernel();
        $io->comment(sprintf('Warming up the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment()var_export($kernel->isDebug(), true)));

        if (!$input->getOption('no-optional-warmers')) {
            $this->cacheWarmer->enableOptionalWarmers();
        }

        $preload = $this->cacheWarmer->warmUp($cacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir'));

        if ($preload && file_exists($preloadFile = $cacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
            Preloader::append($preloadFile$preload);
        }

        $io->success(sprintf('Cache for the "%s" environment (debug=%s) was successfully warmed.', $kernel->getEnvironment()var_export($kernel->isDebug(), true)));

        return 0;
    }
}
Feature::deprecatedMethodMessage(self::class, __METHOD__, 'v6.6.0.0', 'Use reset instead')
        );

        $this->reset();
    }

    /** * @param string $cacheDir */
    public function warmUp($cacheDir): void
    {
        $this->getDecorated()->warmUp($cacheDir);
    }

    public function reset(): void
    {
        $this->getDecorated()->reset();
    }

    public function resetInjection(): void
    {
        $this->getDecorated()->resetInjection();
    }

    
$preload = [];
        try {
            foreach ($this->warmers as $warmer) {
                if (!$this->optionalsEnabled && $warmer->isOptional()) {
                    continue;
                }
                if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
                    continue;
                }

                $start = microtime(true);
                foreach ((array) $warmer->warmUp($cacheDir) as $item) {
                    if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) {
                        throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class$item));
                    }
                    $preload[] = $item;
                }

                if ($io?->isDebug()) {
                    $io->info(sprintf('"%s" completed in %0.2fms.', $warmer::class, 1000 * (microtime(true) - $start)));
                }
            }
        } finally {
            

        $validatorBuilder = new ValidatorBuilder();
        $validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
        $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
        $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()
    {
        
$preload = [];
        try {
            foreach ($this->warmers as $warmer) {
                if (!$this->optionalsEnabled && $warmer->isOptional()) {
                    continue;
                }
                if ($this->onlyOptionalsEnabled && !$warmer->isOptional()) {
                    continue;
                }

                $start = microtime(true);
                foreach ((array) $warmer->warmUp($cacheDir) as $item) {
                    if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) {
                        throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class$item));
                    }
                    $preload[] = $item;
                }

                if ($io?->isDebug()) {
                    $io->info(sprintf('"%s" completed in %0.2fms.', $warmer::class, 1000 * (microtime(true) - $start)));
                }
            }
        } finally {
            
$expressionLang = $this->createMock(ExpressionLanguage::class);
        $expressionLang->expects($this->exactly(2))
            ->method('parse')
            ->willReturnCallback(function D...$args) use (&$series) {
                $expectedArgs = array_shift($series);
                $this->assertSame($expectedArgs$args);

                return $this->createMock(ParsedExpression::class);
            })
        ;

        (new ExpressionCacheWarmer($expressions$expressionLang))->warmUp('');
    }
}
        $this->container = $container;
    }

    /** * @return string[] */
    public function warmUp(string $cacheDir): array
    {
        $this->translator ??= $this->container->get('translator');

        if ($this->translator instanceof WarmableInterface) {
            return (array) $this->translator->warmUp($cacheDir);
        }

        return [];
    }

    public function isOptional(): bool
    {
        return true;
    }

    public static function getSubscribedServices(): array
    {
class RouterCacheWarmerTest extends TestCase
{
    public function testWarmUpWithWarmebleInterface()
    {
        $containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();

        $routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmebleInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
        $containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
        $routerCacheWarmer = new RouterCacheWarmer($containerMock);

        $routerCacheWarmer->warmUp('/tmp');
        $routerMock->expects($this->any())->method('warmUp')->with('/tmp')->willReturn([]);
        $this->addToAssertionCount(1);
    }

    public function testWarmUpWithoutWarmebleInterface()
    {
        $containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();

        $routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmebleInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
        $containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
        $routerCacheWarmer = new RouterCacheWarmer($containerMock);
        
public function getCatalogues(): array
    {
        return $this->translator->getCatalogues();
    }

    /** * @return string[] */
    public function warmUp(string $cacheDir): array
    {
        if ($this->translator instanceof WarmableInterface) {
            return (array) $this->translator->warmUp($cacheDir);
        }

        return [];
    }

    /** * Gets the fallback locales. */
    public function getFallbackLocales(): array
    {
        if ($this->translator instanceof Translator || method_exists($this->translator, 'getFallbackLocales')) {
            
Home | Imprint | This part of the site doesn't use cookies.