Skip to content

Commit 5868c1e

Browse files
committed
Making an autowireable alias and documenting reset
1 parent a17f090 commit 5868c1e

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,39 @@ and `build/entry1.js`, then `encore_entry_script_tags()` is equivalent to:
8888
If you want more control, you can use the `encore_entry_js_files()` and
8989
`encore_entry_css_files()` methods to get the list of files needed, then
9090
loop and create the `script` and `link` tags manually.
91+
92+
## Rendering Multiple Times in a Request (e.g. to Generate a PDF)
93+
94+
When you render your script or link tags, the bundle is smart enough
95+
not to repeat the same JavaScript or CSS file within the same request.
96+
This prevents you from having duplicate `<link>` or `<script>` tags
97+
if you render multiple entries that both rely on the same file.
98+
99+
In some cases, however, you may want to render the script & link
100+
tags for the same entry multiple times in a request. For example,
101+
if you render multiple Twig templates to create multiple PDF files
102+
during a single request.
103+
104+
In that case, before each render, you'll need to "reset" the internal
105+
cache so that the bundle re-renders CSS or JS files that it previously
106+
rendered. For example, in a controller:
107+
108+
```php
109+
// src/Controller/SomeController.php
110+
111+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
112+
113+
class SomeController
114+
{
115+
public function index(EntrypointLookupCollectionInterface $entrypointLookups)
116+
{
117+
$entrypointLookups->getEntrypointLookup()->reset();
118+
// render a template
119+
120+
$entrypointLookups->getEntrypointLookup()->reset();
121+
// render another template
122+
123+
// ...
124+
}
125+
}
126+
```

src/Asset/EntrypointLookupCollection.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,30 @@
1919
*
2020
* @final
2121
*/
22-
class EntrypointLookupCollection
22+
class EntrypointLookupCollection implements EntrypointLookupCollectionInterface
2323
{
2424
private $buildEntrypoints;
2525

26-
public function __construct(ContainerInterface $buildEntrypoints)
26+
private $defaultBuildName;
27+
28+
public function __construct(ContainerInterface $buildEntrypoints, string $defaultBuildName = null)
2729
{
2830
$this->buildEntrypoints = $buildEntrypoints;
31+
$this->defaultBuildName = $defaultBuildName;
2932
}
3033

31-
public function getEntrypointLookup(string $buildName): EntrypointLookupInterface
34+
public function getEntrypointLookup(string $buildName = null): EntrypointLookupInterface
3235
{
36+
if (null === $buildName) {
37+
if (null === $this->defaultBuildName) {
38+
throw new UndefinedBuildException('There is no default build configured: please pass an argument to getEntrypointLookup().');
39+
}
40+
41+
$buildName = $this->defaultBuildName;
42+
}
43+
3344
if (!$this->buildEntrypoints->has($buildName)) {
34-
throw new UndefinedBuildException(sprintf('Given entry point "%s" is not configured', $buildName));
45+
throw new UndefinedBuildException(sprintf('The build "%s" is not configured', $buildName));
3546
}
3647

3748
return $this->buildEntrypoints->get($buildName);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony WebpackEncoreBundle package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\WebpackEncoreBundle\Asset;
11+
12+
use Symfony\WebpackEncoreBundle\Exception\UndefinedBuildException;
13+
14+
interface EntrypointLookupCollectionInterface
15+
{
16+
/**
17+
* Retrieve the EntrypointLookupInterface for the given build.
18+
*
19+
* @throws UndefinedBuildException If the build does not exist.
20+
*/
21+
public function getEntrypointLookup(string $buildName = null): EntrypointLookupInterface;
22+
}

src/Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<argument /> <!-- build list of entrypoints locator -->
1212
</service>
1313

14+
<service id="Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface" alias="webpack_encore.entrypoint_lookup_collection" />
15+
1416
<service id="webpack_encore.tag_renderer" class="Symfony\WebpackEncoreBundle\Asset\TagRenderer">
1517
<argument type="service" id="webpack_encore.entrypoint_lookup_collection" />
1618
<argument type="service" id="assets.packages" />

tests/Asset/EntrypointLookupCollectionTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,35 @@
55
use Symfony\Component\DependencyInjection\ServiceLocator;
66
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
77
use PHPUnit\Framework\TestCase;
8+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
89

910
class EntrypointLookupCollectionTest extends TestCase
1011
{
1112
/**
1213
* @expectedException Symfony\WebpackEncoreBundle\Exception\UndefinedBuildException
13-
* @expectedExceptionMessage Given entry point "something" is not configured
14+
* @expectedExceptionMessage The build "something" is not configured
1415
*/
1516
public function testExceptionOnMissingEntry()
1617
{
1718
$collection = new EntrypointLookupCollection(new ServiceLocator([]));
1819
$collection->getEntrypointLookup('something');
1920
}
21+
22+
/**
23+
* @expectedException Symfony\WebpackEncoreBundle\Exception\UndefinedBuildException
24+
* @expectedExceptionMessage There is no default build configured: please pass an argument to getEntrypointLookup().
25+
*/
26+
public function testExceptionOnMissingDefaultBuildEntry()
27+
{
28+
$collection = new EntrypointLookupCollection(new ServiceLocator([]));
29+
$collection->getEntrypointLookup();
30+
}
31+
32+
public function testDefaultBuildIsReturned()
33+
{
34+
$lookup = $this->createMock(EntrypointLookupInterface::class);
35+
$collection = new EntrypointLookupCollection(new ServiceLocator(['the_default' => function() use ($lookup) { return $lookup; }]), 'the_default');
36+
37+
$this->assertSame($lookup, $collection->getEntrypointLookup());
38+
}
2039
}

0 commit comments

Comments
 (0)