I want to intercept a call to a Service method and execute some code before it.
For example, someone wrote:
$this->container->get('file_locator')->isAbsolutePath($path);
I want to intercept the call to Symfony\\Component\\Config\\FileLocator::isAbsolutePath() and execute some code before it.
Is that possible?
Thanks
I'm not aware of a way to achieve exactly what you are looking for, but I can think of two methods that you should be able to use to achieve the same effect:
And then you have two options:
You could override the service with your own service which extends default one and overrides isAbsolutePath() method.
Override default FileLocator
class with your own:
class MyCustomFileLocator extends FileLocator
{
public function isAbsolutePath()
{
// Your awesome code here...
return parent::isAbsolutePath();
}
}
And override service definition:
# services.yml
# Just create a new definition with the same name
services:
file_locator:
class: MyCustomFileLocator
arguments: [...]
# Or alternatively, if you're overriding one of symfony/third bundle services,
# they often provide class parameter which can be overriden
parameters:
file_locator.class: MyCustomFileLocator
You can use service decoration techinque , which will yield the same effect, but you will still have reference to original service as file_locator.inner
if you need that for some reason.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.