-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1382 from garak/backport-twig-enhancement
backport Twig extension enhancement to 1.x
- Loading branch information
Showing
4 changed files
with
52 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\Twig\Extension; | ||
|
||
use Twig\Extension\RuntimeExtensionInterface; | ||
use Vich\UploaderBundle\Templating\Helper\UploaderHelperInterface; | ||
|
||
/** | ||
* @author Massimiliano Arione <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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'); | ||
} | ||
} |