-
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
Open
jon-ht
wants to merge
13
commits into
sensiolabs:main
Choose a base branch
from
jon-ht:feat-use-cache-file-to-replace-commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4c5fb18
feat: use cache file to replace Symfony commands
jon-ht 1c9fe90
fix: remove Symfony commands for configuration parameters
jon-ht 4e70c16
fix: add template path aliases for Dockerized apps
jon-ht 68b057f
fix: unit tests
jon-ht 46c537c
fix: remove storybook:generate-preview command
jon-ht 78eb6c2
docs: update configuration reference
jon-ht a48e09a
fix: add mandatory options to init command
jon-ht 9b852dc
fix: rename "templatePathAliases" to "projectPathAliases" + handle ad…
jon-ht 75a688e
chore: CS fixes
jon-ht 3d098cb
fix: Twig component path alias when using component in component
jon-ht 9158fc9
fix: default namespace resolution for twig components
jon-ht 50ed82a
fix: use relative paths when importing components
jon-ht f7bb6ec
chore: code and doc cleanup
jon-ht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This process is based on
debug:configcommand.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