DI容器找不到类

时间:2017-04-24 13:00:22

标签: yii yii2 yii2-advanced-app

我的现有应用程序包含以下行

\Yii::$container->invoke([$this, 'injections']);

这条线路错误

  

ReflectionException类Redis不存在

我有一个文件Redis.php,其中Redis目录中定义了类/common/components。 但是yii在/common/components/myAPI/

寻找它

А包含一行

的类
\Yii::$container->invoke([$this, 'injections']);

ia位于路径/common/components/myAPI/

我尝试调用Redis的整个班级

abstract class AEntity{

    const API_BE_SOCCER_COUNTER = 'API_BE_SOCCER_COUNTER';
    /**
     * @var Fetcher $_fetcher
     * @var boolean $_isFromCache
     * @var APIConfig $_config
     * @var string $_redisKey
     * @var array $_params
     */
    private $_fetcher,
        $_isFromCache = null,
        $_params = [],
        $_redisKey = null,
        $_mainApi;

    /**
     * @var APIRedisManager $_redis
     */
    private $_redis,
        $_config;

    /**
     * @var Array $_dbTable
     * @var Array $_dbMap
     */
    protected $_dbMap,
              $_dbTable;

    /**
     * 
     * @return array
     */
    protected function setParams(Array $params = []){
        $this->_params = $params;
    }

    protected abstract function keyRelationShip();

    protected abstract function getAttributes();

    protected abstract function jsonArrayMap();

    protected function setRedisKey($redisData){
        $this->_redisKey = $redisData;
    }

    protected function mapArrays(){
        $jsonArrayMap = $this->jsonArrayMap();

        foreach ($jsonArrayMap as $arrayMap){
            $mapPath = $arrayMap['path'] ? explode('.', $arrayMap['path']) : null;
            $key = $arrayMap['key'];
            $arr = $arrayMap['array'];
            $assoc = $arrayMap['assoc'];
            $tmpData = $this->_mainApi;

            if($mapPath){
                foreach($mapPath as $mapper) {
                    if(!isset($tmpData[$mapper])){
                        $this->{$key} = null;

                        return false;
                    }
                    $tmpData = $tmpData[$mapper];
                }
            }
            $this->{$key} = $arr ? $this->jsonArrayMapper($tmpData, $assoc, $mapPath) : $tmpData;
        }
    }

    public function injections(APIConfig $config, Fetcher $fetcher, APIRedisManager $redisManager){
        $this->_config = $config;
        $this->_fetcher = $fetcher;
        $this->_redis = $redisManager;
    }

    protected function initialize(Array $params = []){
        $constant = static::keyRelationShip();
        $redisKey = $this->paramToRedis($params);

        $this->setParams($params);
        $this->setRedisKey($redisKey);

        \Yii::$container->invoke([$this, 'injections']);

        $this->_config->get($constant, $this->_params);
        $this->_redis->setConfig($this->_config);
        $this->_fetcher->setConfig($this->_config);
        $this->_mainApi = $this->getAPIRequest();

        $this->mapArrays();
    }


    /**
     * @return array
     */
    public function getMainApi(){
        return $this->_mainApi;
    }

    /**
     * @return APIRedisManager
     */
    public function getRedis(){
        return $this->_redis;
    }

    /**
     * @return array
     * @throws \Exception
     */
    public function loadYiiData(){
        $arrModel = [];
        if (!$this->_dbTable) new Exception('No Table Specified.');
        if (!$this->_dbMap) new Exception('No DB Map Specified.');

        foreach ($this->_dbMap as $keyApi => $keyDB){
            if(!isset($this->$keyDB)) throw new \Exception("KeyDB: $keyDB, is not Set");
            $arrModel[$this->_dbTable][$keyApi] = $this->$keyDB;
        }

        return $arrModel;
    }

    /**
     * GET API request logic
     *
     * @return array
     */
    public function getAPIRequest(){
        $redisKey = $this->formulateRedisKeyLogic();
        $storedRequest = $this->_redis->getConfig() ? $this->_redis->get($redisKey) : null;
        if(!$storedRequest){
            $this->_isFromCache = false;
            $apiRequestResult = $this->_fetcher->get()->asArray();
            $this->_redis->incrCounter();
            if($apiRequestResult && !$storedRequest){
                $serializedApiRequest = serialize($apiRequestResult);
                $this->_redis->store($serializedApiRequest, $redisKey);
            }
        }else{
            $this->_isFromCache = true;
            $apiRequestResult = unserialize($storedRequest);
        }
        return $apiRequestResult;
    }

    /** @return boolean */
    public function isFromCache(){
        return $this->_isFromCache;
    }

    private function formulateRedisKeyLogic(){
        $config = $this->_redis->getConfig();
        if(isset($config['key']) && strpos($this->_redisKey,'$.')!==false){
            $configKey = $config['key'];
            $redisKey = $configKey . $this->_redisKey;
            $redisKey = str_replace('$.', '', $redisKey);
        }
        else{
            $redisKey = $this->_redisKey;
        }

        return $redisKey;
    }

    protected function paramToRedis($param){
        $className = (new \ReflectionClass($this))->getShortName();
        $buildRedisKey = '$._'.str_replace('=', '_', http_build_query($param, null, ','));
        $paramKey = $buildRedisKey.'_'.$className;

        return $paramKey;
    }
    /**
     * GET API request logic
     *
     * @return array
     */
    protected function jsonArrayMapper(Array $entityItems, $assoc = false, $mapPath= true){

        $aEntityArray = [];
        $attributes = $this->getAttributes();
        $Klass = static::class;
        if($mapPath){
            foreach ($entityItems as $entityItem){
                $aEntity = new $Klass(false);
                foreach ($attributes as $attribute){

                    $aEntity->{$attribute} = $entityItem[$attribute];
                }
                $assoc ? $aEntityArray[$entityItem[$assoc]] = $aEntity : $aEntityArray[] = $aEntity;
            }
        }else{
            $aEntity = new $Klass(false);
            foreach ($attributes as $attribute){
                $aEntity->{$attribute} = $entityItems[$attribute];
            }
           $aEntityArray = $aEntity;
        }

        return $aEntityArray;
    }

    public function __set($key, $value){
        $this->{$key} = $value;
    }

    public function __get($name) {
        return $this->{$key};
    }

}

这是具有此类构造函数的类的超类

public function __construct($fullInitizalization=true, $params = []) {
    if($fullInitizalization){
        $redisParams = $this->paramToRedis($params);
        parent::initialize($params);
    }
}

当DI容器尝试实例化APIRedisConnection类时,它会传递类型为Redis的参数:

/**  @param Redis $redis */
function __construct(Redis $redis){
   $this->_redis = $redis;
}

在项目中找不到类Redis,但我可以在IDE中看到它,这个类是用PHP 7编写的 虽然整个项目都是用PHP 5.6编写的

0 个答案:

没有答案