Skip to content

Commit

Permalink
Merge pull request #1382 from garak/backport-twig-enhancement
Browse files Browse the repository at this point in the history
backport Twig extension enhancement to 1.x
  • Loading branch information
garak committed Apr 10, 2023
2 parents 8655c36 + b9a46ee commit af35997
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 70 deletions.
4 changes: 4 additions & 0 deletions config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

<service id="Vich\UploaderBundle\Twig\Extension\UploaderExtension" public="false">
<tag name="twig.extension" />
</service>

<service id="Vich\UploaderBundle\Twig\Extension\UploaderExtensionRuntime" public="false">
<argument type="service" id="Vich\UploaderBundle\Templating\Helper\UploaderHelper" />
<tag name="twig.runtime" />
</service>

</services>
Expand Down
46 changes: 1 addition & 45 deletions src/Twig/Extension/UploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,16 @@

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
use Vich\UploaderBundle\Templating\Helper\UploaderHelperInterface;

/**
* UploaderExtension.
*
* @author Dustin Dobervich <ddobervich@gmail.com>
*/
final class UploaderExtension extends AbstractExtension
{
/**
* @var UploaderHelperInterface
*/
private $helper;

public function __construct(UploaderHelperInterface $helper)
{
$this->helper = $helper;
}

public function getFunctions(): array
{
return [
new TwigFunction('vich_uploader_asset', [$this, 'asset']),
new TwigFunction('vich_uploader_asset', [UploaderExtensionRuntime::class, 'asset']),
];
}

/**
* Gets the public path for the file associated with the uploadable object.
*
* @param object $object The object
* @param string|null $fieldName The field name
* @param string|null $className The object's class. Mandatory if $obj can't be used to determine it
*
* @return string|null The public path or null if file not stored
*/
public function asset($object, ?string $fieldName = null, ?string $className = null): ?string
{
if (!$this->helper instanceof UploaderHelper) {
if (!\is_object($object)) {
$msg = 'Not passing an object option is deprecated and will be removed in version 2.';
@\trigger_error($msg, \E_USER_DEPRECATED);
}
if (\func_num_args() > 2) {
$msg = 'Passing a classname is deprecated and will be removed in version 2.';
@\trigger_error($msg, \E_USER_DEPRECATED);
}
}

if (null === $className) {
return $this->helper->asset($object, $fieldName);
}

// @phpstan-ignore-next-line
return $this->helper->asset($object, $fieldName, $className);
}
}
37 changes: 37 additions & 0 deletions src/Twig/Extension/UploaderExtensionRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Vich\UploaderBundle\Twig\Extension;

use Twig\Extension\RuntimeExtensionInterface;
use Vich\UploaderBundle\Templating\Helper\UploaderHelperInterface;

/**
* @author Massimiliano Arione <garakkio@gmail.com>
*/
final class UploaderExtensionRuntime implements RuntimeExtensionInterface
{
/**
* @var UploaderHelperInterface
*/
private $helper;

public function __construct(UploaderHelperInterface $helper)
{
$this->helper = $helper;
}

/**
* Gets the public path for the file associated with the uploadable object.
*
* @param object|array $object The object or array
* @param string|null $fieldName The field name
* @param string|null $className The class name with the uploadable field. Mandatory if $object is an array
*
* @return string|null The public path or null if file not stored
*/
public function asset($object, ?string $fieldName = null, ?string $className = null): ?string
{
// @phpstan-ignore-next-line
return $this->helper->asset($object, $fieldName, $className);
}
}
35 changes: 10 additions & 25 deletions tests/Twig/Extension/UploaderExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,34 @@
namespace Vich\UploaderBundle\Tests\Twig\Extension;

use PHPUnit\Framework\TestCase;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
use Vich\UploaderBundle\Templating\Helper\UploaderHelperInterface;
use Vich\UploaderBundle\Twig\Extension\UploaderExtension;
use Vich\UploaderBundle\Twig\Extension\UploaderExtensionRuntime;

/**
* UploaderExtensionTest.
*
* @author Kévin Gomez <contact@kevingomez.fr>
*/
final class UploaderExtensionTest extends TestCase
{
/**
* @var \PHPUnit\Framework\MockObject\MockObject|UploaderHelper
*/
protected $helper;

/**
* @var UploaderExtension
*/
protected $extension;

protected function setUp(): void
{
$this->helper = $this->getMockBuilder(UploaderHelper::class)->disableOriginalConstructor()->getMock();
$this->extension = new UploaderExtension($this->helper);
}

public function testAssetIsRegistered(): void
{
$functions = $this->extension->getFunctions();
$extension = new UploaderExtension();
$functions = $extension->getFunctions();

self::assertCount(1, $functions);
self::assertSame('vich_uploader_asset', $functions[0]->getName());
}

public function testAssetForwardsCallsToTheHelper(): void
{
$obj = new \stdClass();
$helper = $this->createMock(UploaderHelperInterface::class);
$extension = new UploaderExtensionRuntime($helper);
$object = new \stdClass();

$this->helper
$helper
->expects(self::once())
->method('asset')
->with($obj, 'file');

$this->extension->asset($obj, 'file');
->with($object, 'file');
$extension->asset($object, 'file');
}
}

0 comments on commit af35997

Please sign in to comment.