Skip to content

Instantly share code, notes, and snippets.

@deviprsd
Last active September 6, 2016 00:31
Show Gist options
  • Save deviprsd/5bd4bc827ee7991640c4 to your computer and use it in GitHub Desktop.
Save deviprsd/5bd4bc827ee7991640c4 to your computer and use it in GitHub Desktop.
Getting file path like in OneupUploaderBundle
<?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);
}
}
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 }
<?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";
}
}
@deviprsd
Copy link
Author

deviprsd commented Mar 19, 2016

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.yml or service.xml. Rest should work as expected. The oneup_uploader.templating.uploader_helper service will override the old one with the same name. The path it returns is a relative one, so in twig you can use asset, absolute_url function to get the proper path. You can also use it with 'templating.helper.assets' service in controllers.

/**
* In some controller
*/
$assetHelper = $this->get('templating.helper.assets');
$oneupUrl = $this->get('oneup_uploader.templating.uploader_helper')->filePath($name, $key);

//For relative urls
$assetUrl = $assetHelper->getUrl($oneupUrl); 

//For absolute urls http://stackoverflow.com/questions/8811251/how-to-get-the-full-url-for-an-asset-in-controller
// May not work in Development environment
// Sadly this is a problem, avoid it if you can
$absoluteUrl = $this->getRequest()->getUriForPath($oneupUrl);

In twig

{# For relative urls #}
{{ asset(oneup_uploader_file_path(name, key)) }}

{# For absolute urls #}
{{ absolute_url(asset(oneup_uploader_file_path(name, key))) }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment