0
votes

I can't understand this error. I tried all things but can't solve this. Can someone tell me where is my fault please .

The error :

FatalThrowableError in MemcachedCacheProvider.php line 14: Type error: Argument 1 passed to AppBundle\Data\CacheProvider\MemcachedCacheProvider::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in ...app\cache\dev\appDevDebugProjectContainer.php on line 373

MemcachedCacheProvider.php ;

    <?php

    namespace AppBundle\Data\CacheProvider;

    use AppBundle\Data\Interfaces\ICacheProvider;
    use AppBundle\Data\Interfaces\ICacheItem;
    use Symfony\Component\DependencyInjection\ContainerInterface;

    class MemcachedCacheProvider implements ICacheProvider
    {
        /** @var ContainerInterface */
        protected $_container;

        public function __construct(ContainerInterface $container) {
            $this->_container = $container;
        }
public function GetItem($cacheKey)
    {
        $result = $this->_container->get('beryllium_cache.client')->get($cacheKey);
        if($result === null
            || !$result)
            return null;

        return $result;
    }

    public function GetItems(array $cacheKeys)
    {
        $cachedItems = array();
        $cache = $this->_container->get('beryllium_cache.client');

        foreach($cacheKeys as $key) {
            $item = $cache->get($key);

            if($item === null || !$item)
                return null;

            $cachedItems[] = $item;
        }

        if(count($cachedItems) == 0)
            return null;

        return $cachedItems;
    }

    public function RemoveItem($cacheKey)
    {
        $this->_container->get('beryllium_cache.client')->delete($cacheKey);
    }

    public function RemovePrefix($namespaceKey)
    {
        //Used namespaces for memcache, so incrementing namespace value will now work as removing prefix
        $this->_container->get('beryllium_cache.client')->increment($namespaceKey);

        //TODO: No solution for removing items by prefix in memcached. Flushing cache!
        //$this->_container->get('beryllium_cache.client')->flush();
    }

    public function SetItem(ICacheItem $cacheItem)
    {
        if($cacheItem->GetItem() === null)
            return;

        $this->_container->get('beryllium_cache.client')->set($cacheItem->GetKey(), $cacheItem, ($cacheItem->GetExpireTimeStamp() == 0 ? 0 : ($cacheItem->GetExpireTimeStamp()-time())));
    }

    public function SetItems(array $items, $expireTimeStamp)
    {
        $cache = $this->_container->get('beryllium_cache.client');

        foreach($items as &$item) {
            $cache->set($item->GetKey(), $item, ($expireTimeStamp == 0 ? 0 : ($expireTimeStamp-time())));
        }
    }

    public function GetNamespace($namespaceKey)
    {
        $ns = $this->GetItem($namespaceKey);

        if(empty($ns)) {
            //generate a random value to use as namespace and store it
            $ns = mt_rand();

            $this->_container->get('beryllium_cache.client')->set($namespaceKey, $ns, 0);
        }

        return $ns;
    }

And the services.yml ;

ayon_app.cacheprovider:
        class: AppBundle\Data\CacheProvider\MemcachedCacheProvider
        arguments: ["@service_container"]
1
Where is that service used/injected? You don't, by chance, create it somewhere by hand? - Yoshi
This service using in AppBundle and the services.yml in same bundle. But the constructor give me that error. I can't see my fault. - Ismail
Please add the exact code where you access or inject the service. - Yoshi
@Yoshi If I don't understand you please correct me. That is exact code just I think you don't need my all codes cause that problem. My problem about dependecy injection and just sent related codes. - Ismail
As it stands, the provider and the service definition is correct. But the service is not created properly. So the problem probably lies where you inject/use the service. For example, in a controller you might use $this->get(...), but if by any chance you do $x = new MemcachedCacheProvider(...) then that would be a problem. So I don't need all your code, just the context where it is used/created/injected. - Yoshi

1 Answers

0
votes

First off, it is extremely bad practice to inject the complete Container into a service, this because you will also get all the services you did not ask for / need. So instead of type-hinting ContainerInterface, type-hint only the services you actively use inside of the code and update the service definition appropriately.

Furthermore, when using this cache provider in other classes. You will have to type-hint MemcachedCacheProvider instead of using the new keyword to create a new instance of it.

Tip: When you are not sure about any type-hinted service names within the service.yml, you can simply set the following parameter within your service to automagically resolve them: autowire: true