There is a better way to do this:
$this->container->get('kernel')->locateResource('@AcmeDemoBundle')
will give an absolute path to AcmeDemoBundle
$this->container->get('kernel')->locateResource('@AcmeDemoBundle/Resource')
will give the path to the dir resource inside AcmeDemoBundle and so on ...
An InvalidArgumentException will be thrown if such a file / file does not exist.
In addition, in the container definition, you can use:
my_service: class: AppBundle\Services\Config arguments: ["@=service('kernel').locateResource('@AppBundle/Resources/customers')"]
EDIT
Your services should not be kernel dependent. There is a default Symfony service that you can use: file_locator . It uses Kernel :: locateResource internally, but in your tests it is easier to double / layout.
service definition
my_service: class: AppBundle\Service arguments: ['@file_locator']
the class
namespace AppBundle; use Symfony\Component\HttpKernel\Config\FileLocator; class Service { private $fileLocator; public function __construct(FileLocator $fileLocator) { $this->fileLocator = $fileLocator; } public function doSth() { $resourcePath = $this->fileLocator->locate('@AppBundle/Resources/some_resource'); } }
source share