How can I inject callable code into the Symfon2 Dependency Injection Container container service
Given the following class definition
class CacheSubscriber implements SubscriberInterface
{
public function __construct(
CacheStorageInterface $cache,
callable $canCache
) {
$this->storage = $cache;
$this->canCache = $canCache;
}
I want to define this class as a service in Symfony2 DIC.
Although it is relatively clear what to do with the first argument
<service id="nsp_generic.guzzle.subscriber.cache" class="GuzzleHttp\Subscriber\Cache\CacheSubscriber">
<argument type="service" id="nsp_generic.redis.cache"/>
<!-- ??? -->
</service>
How do I define and introduce the second argument?
UPDATE
User meckhardt did it in the right direction.
Helper class
<?php
namespace Nsp\GenericBundle\Service\Cache;
class CacheConfig {
public static function canCache() {
return true;
}
}
Denial of service
<service id="nsp_generic.guzzle.subscriber.cache" class="GuzzleHttp\Subscriber\Cache\CacheSubscriber">
<argument type="service" id="nsp_generic.guzzle.subscriber.cache.storage"/>
<argument type="collection">
<argument>Nsp\GenericBundle\Service\Cache\CacheConfig</argument>
<argument>canCache</argument>
</argument>
</service>
Thanks Martin!
+3
source to share