Skip to content

[Twig] add computed properties system #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- Add `ExposeInTemplate` attribute to make non-public properties available in component
templates directly.

- Add _Computed Properties_ system.

## 2.0.0

- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`
Expand Down
3 changes: 3 additions & 0 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function render(MountedComponent $mounted): string
// add the component as "this"
['this' => $component],

// add computed properties proxy
['computed' => new ComputedPropertiesProxy($component)],

// add attributes
['attributes' => $mounted->getAttributes()],

Expand Down
69 changes: 69 additions & 0 deletions src/TwigComponent/src/ComputedPropertiesProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\TwigComponent;

/**
* @author Kevin Bond <[email protected]>
*
* @experimental
*/
final class ComputedPropertiesProxy
{
private array $cache = [];

public function __construct(private object $component)
{
}

public function __call(string $name, array $arguments): mixed
{
if ($arguments) {
throw new \InvalidArgumentException('Passing arguments to computed methods is not supported.');
}

if (isset($this->component->$name)) {
// try property
return $this->component->$name;
}

if ($this->component instanceof \ArrayAccess && isset($this->component[$name])) {
return $this->component[$name];
}

$method = $this->normalizeMethod($name);

if (isset($this->cache[$method])) {
return $this->cache[$method];
}

if ((new \ReflectionMethod($this->component, $method))->getNumberOfRequiredParameters()) {
throw new \LogicException('Cannot use computed methods for methods with required parameters.');
}

return $this->cache[$method] = $this->component->$method();
}

private function normalizeMethod(string $name): string
{
if (method_exists($this->component, $name)) {
return $name;
}

foreach (['get', 'is', 'has'] as $prefix) {
if (method_exists($this->component, $method = sprintf('%s%s', $prefix, ucfirst($name)))) {
return $method;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm guessing there wasn't a nice way to "steal" and re-use the Twig native functionality?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was not...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at using twig_get_attribute but it requires injecting the twig environment and Source.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, Source, that's a new class to me :)


throw new \InvalidArgumentException(sprintf('Component "%s" does not have a "%s" method.', $this->component::class, $name));
}
}
45 changes: 23 additions & 22 deletions src/TwigComponent/src/Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ need to populate, you can render it with:
Computed Properties
~~~~~~~~~~~~~~~~~~~

.. versionadded:: 2.1

Computed Properties were added in TwigComponents 2.1.

In the previous example, instead of querying for the featured products
immediately (e.g. in ``__construct()``), we created a ``getProducts()``
method and called that from the template via ``this.products``.
Expand All @@ -432,35 +436,32 @@ But there's no magic with the ``getProducts()`` method: if you call
``this.products`` multiple times in your template, the query would be
executed multiple times.

To make your ``getProducts()`` method act like a true computed property
(where its value is only evaluated the first time you call the method),
you can store its result on a private property:
To make your ``getProducts()`` method act like a true computed property,
call ``computed.products`` in your template. ``computed`` is a proxy
that wraps your component and caches the return of methods. If they
are called additional times, the cached value is used.

.. code-block:: diff
.. code-block:: twig

// src/Components/FeaturedProductsComponent.php
namespace App\Components;
// ...
{# templates/components/featured_products.html.twig #}

#[AsTwigComponent('featured_products')]
class FeaturedProductsComponent
{
private ProductRepository $productRepository;
<div>
<h3>Featured Products</h3>

+ private ?array $products = null;
{% for product in computed.products %}
...
{% endfor %}

// ...
...
{% for product in computed.products %} {# use cache, does not result in a second query #}
...
{% endfor %}
</div>

public function getProducts(): array
{
+ if ($this->products === null) {
+ $this->products = $this->productRepository->findFeatured();
+ }
.. note::

- return $this->productRepository->findFeatured();
+ return $this->products;
}
}
Computed methods only work for component methods with no required
arguments.

Component Attributes
--------------------
Expand Down
26 changes: 26 additions & 0 deletions src/TwigComponent/tests/Fixtures/Component/ComputedComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\TwigComponent\Tests\Fixtures\Component;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent('computed_component')]
final class ComputedComponent
{
public $prop = 'value';
private $count = 0;

public function getCount()
{
return ++$this->count;
}
}
2 changes: 2 additions & 0 deletions src/TwigComponent/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentA;
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentB;
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentC;
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComputedComponent;
use Symfony\UX\TwigComponent\Tests\Fixtures\Component\WithAttributes;
use Symfony\UX\TwigComponent\Tests\Fixtures\Service\ServiceA;
use Symfony\UX\TwigComponent\TwigComponentBundle;
Expand Down Expand Up @@ -61,6 +62,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'template' => 'components/custom2.html.twig',
]);
$c->register(WithExposedVariables::class)->setAutoconfigured(true)->setAutowired(true);
$c->register(ComputedComponent::class)->setAutoconfigured(true)->setAutowired(true);

if ('missing_key' === $this->environment) {
$c->register('missing_key', ComponentB::class)->setAutowired(true)->addTag('twig.component');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
countDirect1: {{ this.getCount }}
countDirect2: {{ this.count }}
countComputed1: {{ computed.getCount }}
countComputed2: {{ computed.count }}
countComputed3: {{ computed.count }}
propDirect: {{ this.prop }}
propComputed: {{ computed.prop }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{ component('component_a', { propA: 'prop a value', propB: 'prop b value' }) }}
{{ component('with_attributes', { prop: 'prop value 1', class: 'bar', style: 'color:red;', value: '', autofocus: null }) }}
{{ component('with_attributes', { prop: 'prop value 2', attributes: { class: 'baz' }, type: 'submit', style: 'color:red;' }) }}
{{ component('computed_component') }}
13 changes: 13 additions & 0 deletions src/TwigComponent/tests/Integration/ComponentExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ public function testRenderComponentWithExposedVariables(): void
$this->assertStringContainsString('Prop2: prop2 value', $output);
$this->assertStringContainsString('Prop3: prop3 value', $output);
}

public function testCanUseComputedMethods(): void
{
$output = self::getContainer()->get(Environment::class)->render('template_a.html.twig');

$this->assertStringContainsString('countDirect1: 1', $output);
$this->assertStringContainsString('countDirect2: 2', $output);
$this->assertStringContainsString('countComputed1: 3', $output);
$this->assertStringContainsString('countComputed2: 3', $output);
$this->assertStringContainsString('countComputed3: 3', $output);
$this->assertStringContainsString('propDirect: value', $output);
$this->assertStringContainsString('propComputed: value', $output);
}
}
128 changes: 128 additions & 0 deletions src/TwigComponent/tests/Unit/ComputedPropertiesProxyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Symfony\UX\TwigComponent\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Symfony\UX\TwigComponent\ComputedPropertiesProxy;

/**
* @author Kevin Bond <[email protected]>
*/
final class ComputedPropertiesProxyTest extends TestCase
{
public function testProxyCachesGetMethodReturns(): void
{
$proxy = new ComputedPropertiesProxy(new class() {
private int $count = 0;

public function getCount(): int
{
return ++$this->count;
}
});

$this->assertSame(1, $proxy->getCount());
$this->assertSame(1, $proxy->getCount());
$this->assertSame(1, $proxy->count());
}

public function testProxyCachesIsMethodReturns(): void
{
$proxy = new ComputedPropertiesProxy(new class() {
private int $count = 0;

public function isCount(): int
{
return ++$this->count;
}
});

$this->assertSame(1, $proxy->isCount());
$this->assertSame(1, $proxy->isCount());
$this->assertSame(1, $proxy->count());
}

public function testProxyCachesHasMethodReturns(): void
{
$proxy = new ComputedPropertiesProxy(new class() {
private int $count = 0;

public function hasCount(): int
{
return ++$this->count;
}
});

$this->assertSame(1, $proxy->hasCount());
$this->assertSame(1, $proxy->hasCount());
$this->assertSame(1, $proxy->count());
}

public function testCanProxyPublicProperties(): void
{
$proxy = new ComputedPropertiesProxy(new class() {
public $foo = 'bar';
});

$this->assertSame('bar', $proxy->foo());
}

public function testCanProxyArrayAccess(): void
{
$proxy = new ComputedPropertiesProxy(new class() implements \ArrayAccess {
private $array = ['foo' => 'bar'];

public function offsetExists(mixed $offset): bool
{
return isset($this->array[$offset]);
}

public function offsetGet(mixed $offset): mixed
{
return $this->array[$offset];
}

public function offsetSet(mixed $offset, mixed $value): void
{
}

public function offsetUnset(mixed $offset): void
{
}
});

$this->assertSame('bar', $proxy->foo());
}

public function testCannotProxyMethodsThatDoNotExist(): void
{
$proxy = new ComputedPropertiesProxy(new class() {});

$this->expectException(\InvalidArgumentException::class);

$proxy->getSomething();
}

public function testCannotPassArgumentsToProxiedMethods(): void
{
$proxy = new ComputedPropertiesProxy(new class() {});

$this->expectException(\InvalidArgumentException::class);

$proxy->getSomething('foo');
}

public function testCannotProxyMethodsWithRequiredArguments(): void
{
$proxy = new ComputedPropertiesProxy(new class() {
public function getValue(int $value): int
{
return $value;
}
});

$this->expectException(\LogicException::class);

$proxy->getValue();
}
}