Created
May 26, 2016 00:27
-
-
Save polidog/c52691a189b37e88ee4f3157cbf17398 to your computer and use it in GitHub Desktop.
vich uploader bundleのassetの改良版
This file contains hidden or 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
services: | |
app_bundle.twig.vich_uri_extension: | |
class: AppBundle\Twig\Excetsion\VichUriExtension | |
arguments: | |
- "@vich_uploader.templating.helper.uploader_helper" | |
calls: | |
- [setRequest, ["@request_stack"]] | |
tags: | |
- { name: twig.extension } |
This file contains hidden or 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
<?php | |
/** | |
* Created by PhpStorm. | |
* User: polidog | |
* Date: 2016/05/26 | |
*/ | |
namespace AppBundle\Twig\Excetsion; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
use Vich\UploaderBundle\Templating\Helper\UploaderHelper; | |
/** | |
* Class VichUriExtension | |
*/ | |
class VichUriExtension extends \Twig_Extension | |
{ | |
/** | |
* @var UploaderHelper $helper | |
*/ | |
private $helper; | |
/** | |
* @var Request | |
*/ | |
private $request; | |
/** | |
* Constructs a new instance of UploaderExtension. | |
* | |
* @param UploaderHelper $helper | |
*/ | |
public function __construct(UploaderHelper $helper) | |
{ | |
$this->helper = $helper; | |
} | |
/** | |
* @param RequestStack $requestStack | |
* @return $this | |
*/ | |
public function setRequest(RequestStack $requestStack) | |
{ | |
$this->request = $requestStack->getCurrentRequest(); | |
return $this; | |
} | |
public function getName() | |
{ | |
return 'vich_uri'; | |
} | |
/** | |
* Returns a list of twig functions. | |
* | |
* @return array An array | |
*/ | |
public function getFunctions() | |
{ | |
$names = array( | |
'vich_uri' => 'asset' | |
); | |
$funcs = array(); | |
foreach ($names as $twig => $local) { | |
$funcs[$twig] = new \Twig_SimpleFunction($twig,[$this, $local]); | |
} | |
return $funcs; | |
} | |
public function asset($obj, $fieldName, $className = null) | |
{ | |
return $this->request->getUriForPath($this->helper->asset($obj, $fieldName, $className)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment