-
Notifications
You must be signed in to change notification settings - Fork 17
feat: use cache file to replace Symfony commands #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
4c5fb18
1c9fe90
4e70c16
68b057f
46c537c
78eb6c2
a48e09a
9b852dc
75a688e
3d098cb
9158fc9
50ed82a
f7bb6ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?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 = [ | ||
| 'storybook_bundle_config' => $this->storybookConfig, | ||
| 'twig_config' => $this->twigConfig, | ||
| 'twig_component_config' => $this->twigComponentConfig, | ||
| ]; | ||
|
|
||
| $cache->write(json_encode($parameters, JSON_PRETTY_PRINT)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace Storybook\Controller; | ||
|
|
||
| use Storybook\Event\GeneratePreviewEvent; | ||
| use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Twig\Environment; | ||
|
|
||
| final class StorybookPreviewController | ||
| { | ||
| public function __construct( | ||
| private readonly Environment $twig, | ||
| private readonly EventDispatcherInterface $eventDispatcher | ||
| ) { | ||
| } | ||
|
|
||
| public function __invoke(): Response | ||
| { | ||
| $this->eventDispatcher->dispatch(new GeneratePreviewEvent()); | ||
|
|
||
| $content = $this->twig->render('@Storybook/preview.html.twig'); | ||
|
|
||
| return new Response($content); | ||
| } | ||
| } | ||
| 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This process is based on 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 |
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same process as
storybook:generate-preview. This command should be removed before merging