1313use Storybook \Exception \UnauthorizedStoryException ;
1414use Storybook \Mock \ComponentProxyFactory ;
1515use Storybook \StoryRenderer ;
16- use Storybook \Twig \StorybookEnvironment ;
1716use Storybook \Twig \StorybookEnvironmentConfigurator ;
17+ use Storybook \Twig \StorybookRuntimeLoader ;
1818use Storybook \Twig \StoryExtension ;
1919use Storybook \Twig \TwigComponentSubscriber ;
2020use Symfony \Component \Config \Definition \Builder \ArrayNodeDefinition ;
@@ -70,6 +70,8 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
7070
7171 $ config = (new Processor ())->processConfiguration ($ this , $ configs );
7272
73+ $ this ->configureStorybookTwigEnvironment ($ container , $ config );
74+
7375 // Proxy listener
7476 $ container ->register ('storybook.listener.proxy_request ' , ProxyRequestListener::class)
7577 ->addTag ('kernel.event_subscriber ' );
@@ -82,6 +84,39 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
8284 ;
8385
8486 // Story renderer
87+ $ container ->register ('storybook.story_renderer ' , StoryRenderer::class)
88+ ->setArgument (0 , new Reference ('storybook.twig ' ))
89+ ;
90+
91+ // Args processors
92+ $ container ->register ('storybook.args_processor ' , StorybookArgsProcessor::class);
93+
94+ // Proxy factory
95+ $ container ->register ('storybook.component_proxy_factory ' , ComponentProxyFactory::class)
96+ ->setArgument (0 , new AbstractArgument (\sprintf ('Provided in "%s". ' , ComponentMockPass::class)));
97+
98+ // Internal commands
99+ $ container ->register ('storybook.generate_preview_command ' , GeneratePreviewCommand::class)
100+ ->setArgument (0 , new Reference ('twig ' ))
101+ ->setArgument (1 , new Reference ('event_dispatcher ' ))
102+ ->addTag ('console.command ' , ['name ' => 'storybook:generate-preview ' ])
103+ ;
104+
105+ // Init command
106+ $ container ->register ('storybook.init_command ' , StorybookInitCommand::class)
107+ ->setArgument (0 , $ container ->getParameter ('kernel.project_dir ' ))
108+ ->addTag ('console.command ' , ['name ' => 'storybook:init ' ]);
109+
110+ // Component subscriber
111+ $ container ->register ('storybook.twig.on_pre_render_listener ' , TwigComponentSubscriber::class)
112+ ->setArgument (0 , new Reference ('request_stack ' ))
113+ ->setArgument (1 , new Reference ('event_dispatcher ' ))
114+ ->addTag ('kernel.event_subscriber ' );
115+ }
116+
117+ private function configureStorybookTwigEnvironment (ContainerBuilder $ container , array $ config ): void
118+ {
119+ // Sandbox
85120 $ defaultSandboxConfig = [
86121 'allowedTags ' => ['component ' ],
87122 'allowedFunctions ' => ['component ' ],
@@ -90,7 +125,7 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
90125 'allowedProperties ' => [],
91126 ];
92127
93- $ sandboxConfig = array_merge_recursive ($ defaultSandboxConfig , $ config ['sandbox ' ]);
128+ $ sandboxConfig = \ array_merge_recursive ($ defaultSandboxConfig , $ config ['sandbox ' ]);
94129
95130 $ container ->register ('storybook.twig.security_policy ' , SecurityPolicy::class)
96131 ->setArgument (0 , $ sandboxConfig ['allowedTags ' ])
@@ -100,13 +135,15 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
100135 ->setArgument (4 , $ sandboxConfig ['allowedFunctions ' ])
101136 ;
102137
103- // Storybook Twig extensions
138+ // Storybook Twig environment
104139 $ container ->setDefinition ('storybook.twig ' , new ChildDefinition ('twig ' ))
105- ->setClass (StorybookEnvironment::class)
106- ->addMethodCall ('setComponentRuntime ' , [new Reference ('storybook.twig.component_runtime ' )])
107140 ->setConfigurator ([new Reference ('storybook.twig.environment_configurator ' ), 'configure ' ])
108141 ;
109142
143+ $ container ->register ('storybook.twig.runtime_loader ' , StorybookRuntimeLoader::class)
144+ ->addMethodCall ('addRuntime ' , [new Reference ('storybook.twig.component_runtime ' )])
145+ ;
146+
110147 $ container ->register ('storybook.twig.extension.sandbox ' , SandboxExtension::class)
111148 ->setArgument (0 , new Reference ('storybook.twig.security_policy ' ))
112149 ->addTag ('storybook.twig.extension ' )
@@ -123,42 +160,13 @@ static function (ChildDefinition $definition, AsComponentMock $attributeInstance
123160 ->setArgument (2 , $ config ['cache ' ] ?? false )
124161 ;
125162
126- $ container ->setDefinition ('storybook.twig.component_runtime ' , new ChildDefinition ('. ux.twig_component.twig.component_runtime ' ))
163+ $ container ->setDefinition ('storybook.twig.component_runtime ' , new ChildDefinition ('ux.twig_component.twig.component_runtime ' ))
127164 ->replaceArgument (0 , new Reference ('storybook.twig.component_renderer ' ))
128165 ;
129166
130167 $ container ->setDefinition ('storybook.twig.component_renderer ' , new ChildDefinition ('ux.twig_component.component_renderer ' ))
131168 ->replaceArgument (0 , new Reference ('storybook.twig ' ))
132169 ;
133-
134- $ container ->register ('storybook.story_renderer ' , StoryRenderer::class)
135- ->setArgument (0 , new Reference ('storybook.twig ' ))
136- ;
137-
138- // Args processors
139- $ container ->register ('storybook.args_processor ' , StorybookArgsProcessor::class);
140-
141- // Proxy factory
142- $ container ->register ('storybook.component_proxy_factory ' , ComponentProxyFactory::class)
143- ->setArgument (0 , new AbstractArgument (\sprintf ('Provided in "%s". ' , ComponentMockPass::class)));
144-
145- // Internal commands
146- $ container ->register ('storybook.generate_preview_command ' , GeneratePreviewCommand::class)
147- ->setArgument (0 , new Reference ('twig ' ))
148- ->setArgument (1 , new Reference ('event_dispatcher ' ))
149- ->addTag ('console.command ' , ['name ' => 'storybook:generate-preview ' ])
150- ;
151-
152- // Init command
153- $ container ->register ('storybook.init_command ' , StorybookInitCommand::class)
154- ->setArgument (0 , $ container ->getParameter ('kernel.project_dir ' ))
155- ->addTag ('console.command ' , ['name ' => 'storybook:init ' ]);
156-
157- // Component subscriber
158- $ container ->register ('storybook.twig.on_pre_render_listener ' , TwigComponentSubscriber::class)
159- ->setArgument (0 , new Reference ('request_stack ' ))
160- ->setArgument (1 , new Reference ('event_dispatcher ' ))
161- ->addTag ('kernel.event_subscriber ' );
162170 }
163171
164172 public function getConfigTreeBuilder (): TreeBuilder
0 commit comments