-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TwigComponent] [LiveComponent] Add support for embedded live components
- Loading branch information
1 parent
f05e5f1
commit d9dd3fc
Showing
23 changed files
with
421 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\LiveComponent\Twig; | ||
|
||
use Symfony\Component\Cache\Adapter\NullAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
|
||
/** | ||
* @author Bart Vanderstukken <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class TemplateCacheWarmer implements CacheWarmerInterface | ||
{ | ||
public function __construct(private \IteratorAggregate $templateIterator, private readonly string $cacheFilename) | ||
{ | ||
} | ||
|
||
public function warmUp(string $cacheDir): void | ||
{ | ||
$map = []; | ||
foreach ($this->templateIterator as $item) { | ||
$map[bin2hex(random_bytes(16))] = $item; | ||
} | ||
|
||
(new PhpArrayAdapter($cacheDir.'/'.$this->cacheFilename, new NullAdapter()))->warmUp(['map' => $map]); | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\LiveComponent\Twig; | ||
|
||
use Symfony\Component\Cache\Adapter\NullAdapter; | ||
use Symfony\Component\Cache\Adapter\PhpArrayAdapter; | ||
|
||
/** | ||
* @author Bart Vanderstukken <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class TemplateMap | ||
{ | ||
private readonly array $map; | ||
|
||
public function __construct(string $cacheFile) | ||
{ | ||
$this->map = (new PhpArrayAdapter($cacheFile, new NullAdapter()))->getItem('map')->get(); | ||
} | ||
|
||
public function resolve(string $obscuredName) | ||
{ | ||
return $this->map[$obscuredName] ?? throw new \RuntimeException(sprintf('Cannot find a template matching "%s". Cache may be corrupt.', $obscuredName)); | ||
} | ||
|
||
public function obscuredName(string $templateName): string | ||
{ | ||
$obscuredName = array_search($templateName, $this->map, true); | ||
if (false === $obscuredName) { | ||
throw new \RuntimeException(sprintf('Cannot find a match for template "%s". Cache may be corrupt.', $templateName)); | ||
} | ||
|
||
return $obscuredName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
src/LiveComponent/tests/Fixtures/templates/components/component2.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<div{{ attributes }}> | ||
<div{{ attributes.defaults({class: 'component2'}) }} > | ||
{% block content %} | ||
Count: {{ this.count }} | ||
PreReRenderCalled: {{ this.preReRenderCalled ? 'Yes' : 'No' }} | ||
{% endblock %} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/LiveComponent/tests/Fixtures/templates/render_embedded_with_blocks.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% component component2 %} | ||
{% block content %} | ||
{{ parent() }} | ||
Embedded content with access to context, like count={{ this.count }} | ||
{% endblock %} | ||
{% endcomponent %} |
2 changes: 2 additions & 0 deletions
2
src/LiveComponent/tests/Fixtures/templates/render_multiple_embedded_with_blocks.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<div id="component1">{% component component2 %}{% block content %}Overridden content from component 1{% endblock %}{% endcomponent %}</div><div id="component2">{% component component2 %}{% block content %}Overridden content from component 2 on same line - count: {{ this.count }}{% endblock %}{% endcomponent %}</div> | ||
<div id="component3">Not overriding{% component component2 %}{% endcomponent %}</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.