Skip to content

Commit c5f6c49

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

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Symfony\WebpackEncoreBundle\Tests;
44

55
use Symfony\Component\DependencyInjection\Reference;
6+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
7+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
68
use Symfony\WebpackEncoreBundle\CacheWarmer\EntrypointCacheWarmer;
79
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
810
use PHPUnit\Framework\TestCase;
@@ -96,6 +98,14 @@ public function testCacheWarmer()
9698
// check for both build keys
9799
$this->assertEquals(['_default' => 0, 'different_build' => 1], $data[0]);
98100
}
101+
102+
public function testAutowireableInterfaces()
103+
{
104+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
105+
$kernel->boot();
106+
$container = $kernel->getContainer();
107+
$this->assertInstanceOf(WebpackEncoreAutowireTestService::class, $container->get(WebpackEncoreAutowireTestService::class));
108+
}
99109
}
100110

101111
class WebpackEncoreIntegrationTestKernel extends Kernel
@@ -145,6 +155,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
145155
$container->register(WebpackEncoreCacheWarmerTester::class)
146156
->addArgument(new Reference('webpack_encore.entrypoint_lookup.cache_warmer'))
147157
->setPublic(true);
158+
159+
$container->autowire(WebpackEncoreAutowireTestService::class)
160+
->setPublic(true);
148161
});
149162
}
150163

@@ -173,3 +186,10 @@ public function warmCache(string $cacheDir)
173186
$this->entrypointCacheWarmer->warmUp($cacheDir);
174187
}
175188
}
189+
190+
class WebpackEncoreAutowireTestService
191+
{
192+
public function __construct(EntrypointLookupInterface $entrypointLookup, EntrypointLookupCollectionInterface $entrypointLookupCollection)
193+
{
194+
}
195+
}

0 commit comments

Comments
 (0)