/** * Bind the installation paths to the application. * * @param array $paths * @return void */ public function bindInstallPaths(array $paths) { $this->instance('path', realpath($paths['app'])); // Here we will bind the install paths into the container as strings that can be // accessed from any point in the system. Each path key is prefixed with path // so that they have the consistent naming convention inside the container. foreach (BoxUtil::array_except($paths, array('app')) as $key => $value) { $this->instance("path.{$key}", realpath($value)); } }
public function queryStruct($queryStruct = array(), $option = array(), $vendorInstance = null) { $option += array('routeKey' => $this->getRouteKey(), 'dbName' => $this->getDbName(), 'seqName' => null, 'collectionName' => null, 'options' => array(), 'explain' => false, 'cacheCount' => false); empty($option['dbName']) && ($option['dbName'] = lcfirst($option['routeKey'])); empty($option['collectionName']) && ($option['collectionName'] = $option['dbName']); empty($option['seqName']) && ($option['seqName'] = lcfirst($option['routeKey'])); list($vendorInstance, $option) = $this->getVendorInstanceSet($option, $vendorInstance, $this->buildRouteAttrCallbackByQueryStruct($queryStruct, $option)); $collection = $vendorInstance->selectCollection($option['dbName'], $option['collectionName']); // 拼接 查询字段 $queryField = array(); !array_key_exists('queryField', $queryStruct) && ($queryStruct['queryField'] = array('*')); if (count($queryStruct['queryField']) == 1 && $queryStruct['queryField'][0] == '*') { $queryField = array(); } else { $queryFiled = array(); foreach ($queryStruct['queryField'] as $field) { $queryFiled[$field] = TRUE; } } // 查询条件 $queryCondition = $this->compileCondition($queryStruct); // 排序条件 $querySort = array(); if (isset($queryStruct['orderSet']) && !empty($queryStruct['orderSet'])) { foreach ($queryStruct['orderSet'] as $row => $set) { foreach ($set as $field => $direction) { $direction = strtoupper(trim($direction)); !in_array($direction, array('ASC', 'DESC')) && ($direction = 'ASC'); $querySort[$field] = $direction == 'ASC' ? MongoCollection::ASCENDING : MongoCollection::DESCENDING; } } } // Limit 分页条件 $limit = null; $offset = null; if (isset($queryStruct['limitOffset']) && !empty($queryStruct['limitOffset'])) { if (isset($queryStruct['limitOffset']['offset']) && isset($queryStruct['limitOffset']['limit'])) { $limit = $queryStruct['limitOffset']['limit']; $offset = $queryStruct['limitOffset']['offset']; } elseif (!isset($queryStruct['limitOffset']['offset']) && isset($queryStruct['limitOffset']['limit'])) { $limit = $queryStruct['limitOffset']['limit']; } } // 结果集游标 $resCur = $collection->find($queryCondition, $queryField); //var_export($resCur);exit; if (!empty($querySort)) { $resCur = $resCur->sort($querySort); } if (!is_null($offset)) { $resCur = $resCur->skip($offset); } if (!is_null($limit)) { $resCur = $resCur->limit($limit); } if ($option['explain'] == true) { return $resCur->explain(); } if (isset($queryStruct['returnType'])) { if ($queryStruct['returnType'] == self::RETURNTYPE_CURSOR) { return $resCur; } } $resCount = null; if ($option['cacheCount'] == true) { !isset($conditionHash) && ($conditionHash = BoxUtil::getDataHashKey(array($option['dbName'], $option['collection'], $queryCondition))); $resCount = $this->currentQuerycount($option['collection'], $conditionHash); } if (is_null($resCount)) { $resCount = $resCur->count(); } if ($option['cacheCount'] == true) { !isset($conditionHash) && ($conditionHash = BoxUtil::getDataHashKey(array($option['dbName'], $option['collection'], $queryCondition))); $this->setQueryCount($option['collection'], $conditionHash, $resCount); } $resAssoc = array(); if ($resCount > 0) { foreach ($resCur as $rowObject) { $resAssoc[] = $rowObject; } } $retStruct = array('count' => $resCount, 'assoc' => $resAssoc); return $retStruct; }
/** * Set a given configuration value. * * @param string $key * @param mixed $value * @return void */ public function set($key, $value) { list($group, $item) = $this->parseKey($key); $this->load($group); if (is_null($item)) { $this->items[$group] = $value; } else { BoxUtil::array_set($this->items[$group], $item, $value); } }
/** * 根据数据类型标识、对象ID更新对应对象数据 * @param string $objectLabel * @param int $requestId * @param array $reqData * @param array $option * @return array|mixed|null */ public function _setStructDataById($objectLabel = '', $requestId = 0, $reqData = array(), $option = array()) { $returnData = null; $option += array('idLabel' => 'id', 'dbInstance' => $this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_DB), 'dbPrefix' => static::$prefixDb, 'dbOption' => array(), 'cacheInstance' => $this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_CACHE), 'setCache' => null, 'cacheOption' => array(), 'cachePrefix' => static::$prefixCache, 'cacheTtl' => static::$ttlCache); // mongo对类型敏感,假设所有的ID是数字类型时,强制类型转换 is_numeric($requestId) && ($requestId = intval($requestId)); $storeData = $tplRowData = $this->getBoxObjectTemplateByLabel($objectLabel); $upData = array(); $gotServiceSyntax = false; foreach ($reqData as $reqKey => $reqValue) { if ($option['dbInstance'] == BoxConstants::INSTANCE_DB_DRIVER_MONGODB && MongoBoxRouteServiceDriver::isServiceSyntax($reqKey)) { $gotServiceSyntax = true; break; } elseif (in_array($option['dbInstance'], array(BoxConstants::INSTANCE_DB_DRIVER_MYSQL, BoxConstants::INSTANCE_DB_DRIVER_MSSQL)) && DbBoxRouteServiceDriver::hasOperator($reqKey)) { $gotServiceSyntax = true; break; } else { if (array_key_exists($reqKey, $tplRowData)) { $upData[$reqKey] = $reqValue; } } } if ($gotServiceSyntax == true) { $upData = $reqData; } else { $storeData = array_merge($storeData, $upData); } $optionDb = array('routeKey' => $objectLabel); !empty($option['dbOption']) && ($optionDb = array_merge($optionDb, $option['dbOption'])); switch ($option['dbInstance']) { case BoxConstants::INSTANCE_DB_DRIVER_MYSQL: case BoxConstants::INSTANCE_DB_DRIVER_MSSQL: if (in_array($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_DB), array(BoxConstants::INSTANCE_DB_DRIVER_MYSQL, BoxConstants::INSTANCE_DB_DRIVER_MSSQL))) { !isset($dbDriverInstance) && ($dbDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_DB)); } else { !isset($dbDriverInstance) && ($dbDriverInstance = self::initDriverInstanceByName($option['dbInstance'])); //$dbDriverInstance->setRouteKey($this->getObjectName()); $dbDriverInstance->setRouteKey($objectLabel); } if ($option['dbInstance'] == BoxConstants::INSTANCE_DB_DRIVER_MSSQL) { $dbType = DbBoxRouteServiceDriver::DBTYPE_MSSQL; } elseif ($option['dbInstance'] == BoxConstants::INSTANCE_DB_DRIVER_MYSQL) { $dbType = DbBoxRouteServiceDriver::DBTYPE_MYSQL; } if ($gotServiceSyntax == TRUE) { $queryFieldStruct = array(); foreach ($upData as $key => $val) { if (DbBoxRouteServiceDriver::hasOperator($key)) { $queryFieldStruct[] = $key . $val; } else { $queryFieldStruct[] = $key . ' = ' . DbBoxRouteServiceDriver::dbEscape($val, $dbType); } } } else { $queryFieldStruct = array(); foreach ($upData as $key => $val) { $queryFieldStruct[] = $key . ' = ' . DbBoxRouteServiceDriver::dbEscape($val, $dbType); } } $queryStruct = array('queryField' => $queryFieldStruct, 'querySchema' => array($objectLabel), 'conditionKey' => array()); $queryStruct['conditionKey'][$option['idLabel']] = $requestId; $returnData = $dbDriverInstance->queryUpdate($queryStruct, $optionDb); break; case BoxConstants::INSTANCE_DB_DRIVER_REDIS: if ($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_DB) == BoxConstants::INSTANCE_DB_DRIVER_REDIS) { !isset($dbDriverInstance) && ($dbDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_DB)); } else { !isset($dbDriverInstance) && ($dbDriverInstance = self::initDriverInstanceByName(BoxConstants::INSTANCE_DB_DRIVER_REDIS)); //$dbDriverInstance->setRouteKey($this->getObjectName()); $dbDriverInstance->setRouteKey($objectLabel); } $objectDbKey = $option['dbPrefix'] . $objectLabel . BoxConstants::KEYSEP . $requestId; $returnData = $dbDriverInstance->set($objectDbKey, BoxUtil::datapack($reqData), $optionDb); break; case BoxConstants::INSTANCE_DB_DRIVER_MONGODB: default: $dbName = isset($optionDb['dbName']) && !empty($optionDb['dbName']) ? $optionDb['dbName'] : lcfirst($objectLabel); $collectionName = isset($optionDb['collectionName']) && !empty($optionDb['collectionName']) ? $optionDb['collectionName'] : lcfirst($objectLabel); if ($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_DB) == BoxConstants::INSTANCE_DB_DRIVER_MONGODB) { !isset($dbDriverInstance) && ($dbDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_DB)); } else { !isset($dbDriverInstance) && ($dbDriverInstance = self::initDriverInstanceByName(BoxConstants::INSTANCE_DB_DRIVER_MONGODB)); $dbDriverInstance->__init(array('routeKey' => $objectLabel, 'dbName' => $dbName)); } $dbOptions = isset($optionDb['options']) && !empty($optionDb['options']) ? $optionDb['options'] : array(); empty($dbOptions) && ($dbOptions = array('safe' => true)); $optionReq = $optionDb; $optionReq['idLabel'] = $option['idLabel']; $queryIdCond = array($option['idLabel'] => $requestId); $dbData = $dbDriverInstance->read($queryIdCond, $optionReq); if (empty($dbData)) { // 如果未找到数据时直接返回 return false; } $storeData = array_merge($storeData, $dbData); $setData = array(); $gotServiceSyntax = false; foreach ($reqData as $reqKey => $reqValue) { if (MongoBoxRouteServiceDriver::isServiceSyntax($reqKey)) { $gotServiceSyntax = true; break; } else { $reqKey != $option['idLabel'] && array_key_exists($reqKey, $dbData) && $dbData[$reqKey] !== $reqValue && ($setData[$reqKey] = $reqValue); } } $collectionData = $dbDriverInstance->selectCollection($dbName, $collectionName, $optionDb); if ($gotServiceSyntax == true) { $setData = $reqData; $collectionData->update($queryIdCond, $setData, $dbOptions); } else { if (empty($setData)) { return FALSE; } $collectionData->update($queryIdCond, array('$set' => $setData), $dbOptions); } if ($gotServiceSyntax == false) { $storeData = array_merge($storeData, $upData); } break; } if ($option['setCache'] !== false) { $optionCache = array('routeKey' => $objectLabel); !empty($option['cacheOption']) && ($optionCache = array_merge($optionCache, $option['cacheOption'])); if ($option['setCache'] === true) { // 写入缓存 switch ($option['cacheInstance']) { case BoxConstants::INSTANCE_CACHE_DRIVER_MEMCACHE: if ($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_CACHE) == BoxConstants::INSTANCE_CACHE_DRIVER_MEMCACHE) { !isset($cacheDriverInstance) && ($cacheDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_CACHE)); } else { !isset($cacheDriverInstance) && ($cacheDriverInstance = self::initDriverInstanceByName(BoxConstants::INSTANCE_CACHE_DRIVER_MEMCACHE)); //$driverInstance->setRouteKey($this->getObjectName()); $cacheDriverInstance->setRouteKey($objectLabel); } $objectCacheKey = $option['cachePrefix'] . $objectLabel . BoxConstants::KEYSEP . $requestId; $cacheDriverInstance->set($objectCacheKey, BoxUtil::datapack($storeData), $option['cacheTtl'], $optionCache); break; case BoxConstants::INSTANCE_CACHE_DRIVER_REDIS: default: if ($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_CACHE) == BoxConstants::INSTANCE_CACHE_DRIVER_REDIS) { !isset($cacheDriverInstance) && ($cacheDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_CACHE)); } else { !isset($cacheDriverInstance) && ($cacheDriverInstance = self::initDriverInstanceByName(BoxConstants::INSTANCE_CACHE_DRIVER_REDIS)); //$driverInstance->setRouteKey($this->getObjectName()); $cacheDriverInstance->setRouteKey($objectLabel); } $objectCacheKey = $option['cachePrefix'] . $objectLabel . BoxConstants::KEYSEP . $requestId; $cacheDriverInstance->setex($objectCacheKey, $option['cacheTtl'], BoxUtil::datapack($storeData), $optionCache); break; } } else { //删除cache switch ($option['cacheInstance']) { case BoxConstants::INSTANCE_CACHE_DRIVER_MEMCACHE: if ($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_CACHE) == BoxConstants::INSTANCE_CACHE_DRIVER_MEMCACHE) { !isset($cacheDriverInstance) && ($cacheDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_CACHE)); } else { !isset($cacheDriverInstance) && ($cacheDriverInstance = self::initDriverInstanceByName(BoxConstants::INSTANCE_CACHE_DRIVER_MEMCACHE)); //$driverInstance->setRouteKey($this->getObjectName()); $cacheDriverInstance->setRouteKey($objectLabel); } $objectCacheKey = $option['cachePrefix'] . $objectLabel . BoxConstants::KEYSEP . $requestId; $cacheDriverInstance->delete($objectCacheKey, 0, $optionCache); break; case BoxConstants::INSTANCE_CACHE_DRIVER_REDIS: default: if ($this->getInstanceTypeDriverName(BoxConstants::INSTANCE_TYPE_CACHE) == BoxConstants::INSTANCE_CACHE_DRIVER_REDIS) { !isset($cacheDriverInstance) && ($cacheDriverInstance = $this->getSetInstanceTypeDriverInstance(BoxConstants::INSTANCE_TYPE_CACHE)); } else { !isset($cacheDriverInstance) && ($cacheDriverInstance = self::initDriverInstanceByName(BoxConstants::INSTANCE_CACHE_DRIVER_REDIS)); //$driverInstance->setRouteKey($this->getObjectName()); $cacheDriverInstance->setRouteKey($objectLabel); } $objectCacheKey = $option['cachePrefix'] . $objectLabel . BoxConstants::KEYSEP . $requestId; $cacheDriverInstance->delete($objectCacheKey, $optionCache); break; } } } return $returnData; }
/** * Set a given configuration value. * * @param string $key * @param mixed $value * @return void */ public function setScript($key, $value) { list($driver, $item) = $this->parseConfKey($key); $this->loadConf($driver); if (is_null($item)) { $this->itemsScript[$driver] = $value; } else { BoxUtil::array_set($this->itemsScript[$driver], $item, $value); } }
public function callMultiGetVendorInstance(array $keys, $method = '', $params = array(), $rowPostCallback = null, $option = array(), $vendorInstance = null) { $option += array('driverKey' => $this->getDriverKey(), 'routeMode' => BoxConstants::DATAROUTE_MODE_ATTR, 'routeKey' => $this->getRouteKey(), 'routeAttrIdLabel' => 'id'); $resultDict = array(); if (is_null($vendorInstance)) { $routeAttr = array(); // 根据各个属性分配到的池子中不同的分组 $routeInstSetPool = array(); foreach ($keys as $key) { $routeAttr = array($option['routeAttrIdLabel'] => $key); $routeIdSet = self::$routeInstance->getRouteInstanceRouteIdSet($option['driverKey'], $option['routeKey'], $routeAttr); !isset($routeInstSetPool[$routeIdSet['group']]) && ($routeInstSetPool[$routeIdSet['group']] = array('keys' => array(), 'idset' => $routeIdSet)); $keyHash = is_scalar($key) ? $key : BoxUtil::getDataHashKey($key); !isset($routeInstSetPool[$routeIdSet['group']]['keys'][$keyHash]) && ($routeInstSetPool[$routeIdSet['group']]['keys'][$keyHash] = $key); } $tplParams = $params; // 再分别根据各个分组将执行的结果返回 if (!empty($routeInstSetPool)) { foreach ($routeInstSetPool as $group => $groupSet) { $rowParams = $tplParams; $rowKeys = array_values($groupSet['keys']); $routeIdSet = $groupSet['idset']; $rowVendorInstance = self::$routeInstance->getRouteInstanceByConfSubset($option['driverKey'], $routeIdSet['routeKey'], $routeIdSet['group'])->getInstance(); if (!empty($rowParams)) { array_unshift($rowParams, $rowKeys); } else { $rowParams = array($rowKeys); } if (!is_null($rowPostCallback)) { $midResult = call_user_func_array(array($rowVendorInstance, $method), $rowParams); $result = call_user_func($rowPostCallback, $midResult); } else { $result = call_user_func_array(array($rowVendorInstance, $method), $rowParams); } if (!empty($result)) { $resultDict = array_merge($resultDict, $result); } } } } else { if (!empty($params)) { array_unshift($params, $keys); } else { $params = array($keys); } if (!is_null($rowPostCallback)) { $midResult = call_user_func_array(array($vendorInstance, $method), $params); $resultDict = call_user_func($rowPostCallback, $midResult); } else { $resultDict = call_user_func_array(array($vendorInstance, $method), $params); } } return $resultDict; }