-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStorybookCacheWarmer.php
More file actions
64 lines (52 loc) · 1.83 KB
/
StorybookCacheWarmer.php
File metadata and controls
64 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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));
}
}