Symfony construct()必须实现接口Symfony \ Component \ DependencyInjection \ ContainerInterface,没有给出

时间:2017-10-12 09:15:02

标签: php dependency-injection symfony-2.8

我无法理解这个错误。我尝试了所有的东西,但无法解决这个问题。有人可以告诉我,我的错在哪里。

错误:

  

MemcachedCacheProvider.php第14行中的FatalThrowableError:输入错误:   争论1传递给   的appbundle \ DATA \的CacheProvider \ MemcachedCacheProvider :: __结构()   必须实现接口   Symfony \ Component \ DependencyInjection \ ContainerInterface,没有给出,   在第373行调用... app \ cache \ dev \ appDevDebugProjectContainer.php

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;
    }

和services.yml;

ayon_app.cacheprovider:
        class: AppBundle\Data\CacheProvider\MemcachedCacheProvider
        arguments: ["@service_container"]

1 个答案:

答案 0 :(得分:0)

首先,将完整的Container注入服务是非常糟糕的做法,这是因为您还将获得您没有要求/需要的所有服务。因此,不要使用类型提示ContainerInterface,而是只提示您在代码中主动使用的服务,并相应地更新服务定义。

此外,在其他类中使用此缓存提供程序时。您必须输入提示MemcachedCacheProvider,而不是使用new关键字来创建它的新实例。

提示:当您不确定service.yml中的任何类型提示服务名称时,您只需在服务中设置以下参数即可自动解决它们:autowire: true