Skip to content

Commit d9c2491

Browse files
committed
adding autowireable alias for EntrypointLookupInterface
1 parent 61cf058 commit d9c2491

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,23 @@ rendered. For example, in a controller:
112112
```php
113113
// src/Controller/SomeController.php
114114

115-
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
115+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
116116

117117
class SomeController
118118
{
119-
public function index(EntrypointLookupCollectionInterface $entrypointLookups)
119+
public function index(EntrypointLookupInterface $entrypointLookup)
120120
{
121-
$entrypointLookups->getEntrypointLookup()->reset();
121+
$entrypointLookup->reset();
122122
// render a template
123123

124-
$entrypointLookups->getEntrypointLookup()->reset();
124+
$entrypointLookup->reset();
125125
// render another template
126126

127127
// ...
128128
}
129129
}
130130
```
131+
132+
If you have multiple builds, you can also autowire
133+
`Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface`
134+
and use it to get the `EntrypointLookupInterface` object for any build.

src/DependencyInjection/WebpackEncoreExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
namespace Symfony\WebpackEncoreBundle\DependencyInjection;
1111

1212
use Symfony\Component\Config\FileLocator;
13+
use Symfony\Component\DependencyInjection\Alias;
1314
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Definition;
1617
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1718
use Symfony\Component\DependencyInjection\Reference;
1819
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1920
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
21+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
2022

2123
final class WebpackEncoreExtension extends Extension
2224
{
@@ -46,14 +48,20 @@ public function load(array $configs, ContainerBuilder $container)
4648

4749
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
4850
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
51+
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
4952
}
5053

5154
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference
5255
{
53-
$id = sprintf('webpack_encore.entrypoint_lookup[%s]', $name);
56+
$id = $this->getEntrypointServiceId($name);
5457
$arguments = [$path.'/'.self::ENTRYPOINTS_FILE_NAME, $cacheEnabled ? new Reference('webpack_encore.cache') : null, $name];
5558
$container->setDefinition($id, new Definition(EntrypointLookup::class, $arguments));
5659

5760
return new Reference($id);
5861
}
62+
63+
private function getEntrypointServiceId(string $name): string
64+
{
65+
return sprintf('webpack_encore.entrypoint_lookup[%s]', $name);
66+
}
5967
}

tests/IntegrationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ public function testCacheWarmer()
9696
// check for both build keys
9797
$this->assertEquals(['_default' => 0, 'different_build' => 1], $data[0]);
9898
}
99+
100+
public function testAutowireableInterfaces()
101+
{
102+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
103+
$kernel->boot();
104+
$container = $kernel->getContainer();
105+
$this->assertInstanceOf(WebpackEncoreAutowireTestService::class, $container->get(WebpackEncoreAutowireTestService::class));
106+
}
99107
}
100108

101109
class WebpackEncoreIntegrationTestKernel extends Kernel
@@ -145,6 +153,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
145153
$container->register(WebpackEncoreCacheWarmerTester::class)
146154
->addArgument(new Reference('webpack_encore.entrypoint_lookup.cache_warmer'))
147155
->setPublic(true);
156+
157+
$container->autowire(WebpackEncoreAutowireTestService::class)
158+
->setPublic(true);
148159
});
149160
}
150161

@@ -173,3 +184,10 @@ public function warmCache(string $cacheDir)
173184
$this->entrypointCacheWarmer->warmUp($cacheDir);
174185
}
175186
}
187+
188+
class WebpackEncoreAutowireTestService
189+
{
190+
public function __construct(EntrypointLookupInterface $entrypointLookup, EntrypointLookupCollectionInterface $entrypointLookupCollection)
191+
{
192+
}
193+
}

0 commit comments

Comments
 (0)