Last active
September 6, 2016 00:31
-
-
Save deviprsd/5bd4bc827ee7991640c4 to your computer and use it in GitHub Desktop.
Getting file path like in OneupUploaderBundle
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 | |
| namespace AdminBundle\Twig\Extension; | |
| use AdminBundle\Service\Oneup\Templating\Helper\UploaderHelper; | |
| class OneupFilePathExtention extends \Twig_Extension { | |
| protected $helper; | |
| public function __construct(UploaderHelper $helper) { | |
| $this->helper = $helper; | |
| } | |
| public function getName() { | |
| return 'oneup'; | |
| } | |
| public function getFunctions() { | |
| return array( | |
| new \Twig_SimpleFunction('oneup_uploader_file_path', array($this, 'filePath')) | |
| ); | |
| } | |
| public function filePath($name, $key) { | |
| return $this->helper->filePath($name, $key); | |
| } | |
| } |
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
| oneup_uploader.templating.uploader_helper: | |
| class: AdminBundle\Service\Oneup\Templating\Helper\UploaderHelper | |
| arguments: ["@router", %oneup_uploader.maxsize%, "@service_container"] | |
| tags: | |
| - { name: "templating.helper", alias: "oneup_uploader"} | |
| oneup_uploader.twig.extension.filepath: | |
| class: AdminBundle\Twig\Extension\OneupFilePathExtention | |
| arguments: ["@oneup_uploader.templating.uploader_helper"] | |
| 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 | |
| namespace AdminBundle\Service\Oneup\Templating\Helper; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| use Symfony\Component\Routing\RouterInterface; | |
| use Oneup\UploaderBundle\Templating\Helper\UploaderHelper as Helper; | |
| class UploaderHelper extends Helper { | |
| /** | |
| * Configs of oneup/uploader-bundle | |
| */ | |
| protected $container; | |
| public function __construct(RouterInterface $router, array $maxsize, ContainerInterface $container) { | |
| parent::__construct($router, $maxsize); | |
| $this->container = $container; | |
| } | |
| public function filePath($name, $key) { | |
| $storage = $this->container->getParameter('oneup_uploader.config')["mappings"][$key]["storage"]; | |
| if ($storage["type"] == "gaufrette") { | |
| $adapter = $this->container->get($storage['filesystem'])->getAdapter(); | |
| $reflection = new \ReflectionClass($adapter); | |
| $directory = $reflection->getProperty('directory'); | |
| $directory->setAccessible(true); | |
| $path = $directory->getValue($adapter); | |
| array($path, $this->container->getParameter('kernel.root_dir')); | |
| } else { | |
| $path = $storage['filesystem']; | |
| } | |
| return substr($path, strpos($path, '/web/') + 5) . "/$name"; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To people who are new to these stuff, the namespace is changeable, so put it in the proper Bundle you want to use in. And accordingly change in the
service.ymlorservice.xml. Rest should work as expected. Theoneup_uploader.templating.uploader_helperservice will override the old one with the same name. The path it returns is a relative one, so in twig you can useasset,absolute_urlfunction to get the proper path. You can also use it with'templating.helper.assets'service in controllers.In twig