Skip to content

Commit bbfadee

Browse files
committed
adding autowireable alias for EntrypointLookupInterface
1 parent ca02c85 commit bbfadee

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

README.md

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

111-
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
111+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
112112

113113
class SomeController
114114
{
115-
public function index(EntrypointLookupCollectionInterface $entrypointLookups)
115+
public function index(EntrypointLookupInterface $entrypointLookup)
116116
{
117-
$entrypointLookups->getEntrypointLookup()->reset();
117+
$entrypointLookup->reset();
118118
// render a template
119119

120-
$entrypointLookups->getEntrypointLookup()->reset();
120+
$entrypointLookup->reset();
121121
// render another template
122122

123123
// ...
124124
}
125125
}
126126
```
127+
128+
If you have multiple builds, you can also autowire
129+
`Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface`
130+
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
{
@@ -44,14 +46,20 @@ public function load(array $configs, ContainerBuilder $container)
4446

4547
$container->getDefinition('webpack_encore.entrypoint_lookup_collection')
4648
->replaceArgument(0, ServiceLocatorTagPass::register($container, $factories));
49+
$container->setAlias(EntrypointLookupInterface::class, new Alias($this->getEntrypointServiceId('_default')));
4750
}
4851

4952
private function entrypointFactory(ContainerBuilder $container, string $name, string $path, bool $cacheEnabled): Reference
5053
{
51-
$id = sprintf('webpack_encore.entrypoint_lookup[%s]', $name);
54+
$id = $this->getEntrypointServiceId($name);
5255
$arguments = [$path.'/entrypoints.json', $cacheEnabled ? new Reference('webpack_encore.cache') : null, $name];
5356
$container->setDefinition($id, new Definition(EntrypointLookup::class, $arguments));
5457

5558
return new Reference($id);
5659
}
60+
61+
private function getEntrypointServiceId(string $name): string
62+
{
63+
return sprintf('webpack_encore.entrypoint_lookup[%s]', $name);
64+
}
5765
}

tests/IntegrationTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Symfony\WebpackEncoreBundle\Tests;
44

5+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
6+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
57
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle;
68
use PHPUnit\Framework\TestCase;
79
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
@@ -77,6 +79,14 @@ public function testEntriesAreNotRepeteadWhenAlreadyOutputIntegration()
7779
$html2
7880
);
7981
}
82+
83+
public function testAutowireableInterfaces()
84+
{
85+
$kernel = new WebpackEncoreIntegrationTestKernel(true);
86+
$kernel->boot();
87+
$container = $kernel->getContainer();
88+
$this->assertInstanceOf(WebpackEncoreAutowireTestService::class, $container->get(WebpackEncoreAutowireTestService::class));
89+
}
8090
}
8191

8292
class WebpackEncoreIntegrationTestKernel extends Kernel
@@ -122,6 +132,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
122132
'different_build' => __DIR__.'/fixtures/different_build'
123133
]
124134
]);
135+
136+
$container->autowire(WebpackEncoreAutowireTestService::class)
137+
->setPublic(true);
125138
});
126139
}
127140

@@ -135,3 +148,11 @@ public function getLogDir()
135148
return sys_get_temp_dir().'/logs'.spl_object_hash($this);
136149
}
137150
}
151+
152+
class WebpackEncoreAutowireTestService
153+
{
154+
public function __construct(EntrypointLookupInterface $entrypointLookup, EntrypointLookupCollectionInterface $entrypointLookupCollection)
155+
{
156+
157+
}
158+
}

0 commit comments

Comments
 (0)