Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/CacheWarmer/StorybookCacheWarmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Storybook\CacheWarmer;

use Symfony\Component\Config\ConfigCacheFactory;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Config\ConfigCacheInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

class StorybookCacheWarmer implements CacheWarmerInterface
{
private ConfigCacheFactory $configCacheFactory;

public function __construct(
private readonly ?string $cacheDir,
private readonly bool $debug,
private readonly string $projectDir,
private readonly array $storybookConfig,
private readonly array $twigConfig,
private readonly array $twigComponentConfig,
) {
}

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

public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
$this->getConfigCacheFactory()->cache(
$this->cacheDir.'/symfony_parameters.json',
function (ConfigCacheInterface $cache) {
$this->generateSymfonyParameters($cache);
}
);

return [];
}

/**
* Provides the ConfigCache factory implementation, falling back to a
* default implementation if necessary.
*/
private function getConfigCacheFactory(): ConfigCacheFactoryInterface
{
$this->configCacheFactory ??= new ConfigCacheFactory($this->debug);

return $this->configCacheFactory;
}

private function generateSymfonyParameters(ConfigCacheInterface $cache): void
{
$parameters = [
'kernel_project_dir' => $this->projectDir,
'storybook_config' => $this->storybookConfig,
'twig_config' => $this->twigConfig,
'twig_component_config' => $this->twigComponentConfig,
];

$cache->write(json_encode($parameters, JSON_PRETTY_PRINT));
}
}
51 changes: 51 additions & 0 deletions src/DependencyInjection/Compiler/CacheWarmerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Storybook\DependencyInjection\Compiler;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

class CacheWarmerPass implements CompilerPassInterface
{
private ?array $extensionConfig = null;

public function process(ContainerBuilder $container): void
{
$cacheWarmerDefinition = $container->getDefinition('storybook.cache_warmer');

$cacheWarmerDefinition
->setArgument(3, $this->getConfig($container, $container->getExtension('storybook')))
->setArgument(4, $this->getConfig($container, $container->getExtension('twig')))
->setArgument(5, $this->getConfig($container, $container->getExtension('twig_component')))
;
}

private function getConfigForExtension(ExtensionInterface $extension, ContainerBuilder $container)
{
$extensionAlias = $extension->getAlias();

if (isset($this->extensionConfig[$extensionAlias])) {
return $this->extensionConfig[$extensionAlias];
}

$configs = $container->getExtensionConfig($extensionAlias);

$configuration = $extension instanceof ConfigurationInterface ? $extension : $extension->getConfiguration($configs, $container);

return $this->extensionConfig[$extensionAlias] = (new Processor())->processConfiguration($configuration, $configs);
}

private function getConfig(ContainerBuilder $container, ExtensionInterface $extension): array
{
return $container->resolveEnvPlaceholders(
$container->getParameterBag()->resolveValue(
$this->getConfigForExtension($extension, $container)
), true
);
}
Comment on lines +27 to +49
Copy link
Author

@jon-ht jon-ht Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This process is based on debug:config command.

I'm not really sure about the implementation. It gives me expected output, but there could be other ways to achieve this. Let me know if it could be improved

}
10 changes: 10 additions & 0 deletions src/DependencyInjection/StorybookExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Storybook\ArgsProcessor\StorybookArgsProcessor;
use Storybook\Attributes\AsArgsProcessor;
use Storybook\Attributes\AsComponentMock;
use Storybook\CacheWarmer\StorybookCacheWarmer;
use Storybook\Command\GeneratePreviewCommand;
use Storybook\Command\StorybookInitCommand;
use Storybook\Controller\StorybookController;
use Storybook\DependencyInjection\Compiler\CacheWarmerPass;
use Storybook\DependencyInjection\Compiler\ComponentMockPass;
use Storybook\EventListener\ProxyRequestListener;
use Storybook\Exception\UnauthorizedStoryException;
Expand Down Expand Up @@ -159,6 +161,14 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
->setArgument(0, new Reference('request_stack'))
->setArgument(1, new Reference('event_dispatcher'))
->addTag('kernel.event_subscriber');

$container->register('storybook.cache_warmer', StorybookCacheWarmer::class)
->setArgument(0, $container->getParameter('kernel.cache_dir').'/storybook')
->setArgument(1, $container->getParameter('kernel.debug'))
->setArgument(2, $container->getParameter('kernel.project_dir'))
->setArgument(3, new AbstractArgument(\sprintf('Provided in "%s".', CacheWarmerPass::class)))
->setArgument(4, new AbstractArgument(\sprintf('Provided in "%s".', CacheWarmerPass::class)))
->addTag('kernel.cache_warmer');
}

public function getConfigTreeBuilder(): TreeBuilder
Expand Down
3 changes: 3 additions & 0 deletions src/StorybookBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Storybook;

use Storybook\DependencyInjection\Compiler\ArgsProcessorPass;
use Storybook\DependencyInjection\Compiler\CacheWarmerPass;
use Storybook\DependencyInjection\Compiler\ComponentMockPass;
use Storybook\DependencyInjection\StorybookExtension;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -18,6 +20,7 @@ public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new ArgsProcessorPass());
$container->addCompilerPass(new ComponentMockPass());
$container->addCompilerPass(new CacheWarmerPass());
}

public function getContainerExtension(): ?ExtensionInterface
Expand Down