public function handleMultiRequest() { $listOfRequests = array(); $results = array(); $found = true; $i = 1; while ($found) { $currentService = isset($this->params[$i . ":service"]) ? $this->params[$i . ":service"] : null; $currentAction = isset($this->params[$i . ":action"]) ? $this->params[$i . ":action"] : null; $found = $currentAction && $currentService; if ($found) { $listOfRequests[$i]["service"] = $currentService; $listOfRequests[$i]["action"] = $currentAction; // find all the parameters for this request foreach ($this->params as $key => $val) { // the key "1:myparam" mean that we should input value of this key to request "1", for param "myparam" $keyArray = explode(":", $key); if ($keyArray[0] == $i) { array_shift($keyArray); // remove the request number $requestKey = implode(":", $keyArray); /* remarked by Dor - 13/10/2010 * There is no need to remove service and action from the params in case of multirequest * while they are needed in KalturaResponseCacher if (in_array($requestKey, array("service", "action"))) // don't add service name and action name to the params continue; */ $listOfRequests[$i]["params"][$requestKey] = $val; // store the param } } // clientTag param might be used in KalturaResponseCacher if (isset($this->params['clientTag']) && !isset($listOfRequests[$i]["params"]['clientTag'])) { $listOfRequests[$i]["params"]['clientTag'] = $this->params['clientTag']; } // if ks is not set for a specific request, copy the ks from the top params $currentKs = isset($listOfRequests[$i]["params"]["ks"]) ? $listOfRequests[$i]["params"]["ks"] : null; if (!$currentKs) { $mainKs = isset($this->params["ks"]) ? $this->params["ks"] : null; if ($mainKs) { $listOfRequests[$i]["params"]["ks"] = $mainKs; } } $currentPartner = isset($listOfRequests[$i]["params"]["partnerId"]) ? $listOfRequests[$i]["params"]["partnerId"] : null; if (!$currentPartner) { $mainPartner = isset($this->params["partnerId"]) ? $this->params["partnerId"] : null; if ($mainPartner) { $listOfRequests[$i]["params"]["partnerId"] = $mainPartner; } } $i++; } else { // will break the loop } } $i = 1; foreach ($listOfRequests as $currentRequest) { $currentService = $currentRequest["service"]; $currentAction = $currentRequest["action"]; $currentParams = $currentRequest["params"]; // check if we need to replace params with prev results foreach ($currentParams as $key => &$val) { $matches = array(); // keywords: multirequest, result, depend, pass // figuring out if requested params should be extracted from previous result // example: if you want to use KalturaPlaylist->playlistContent result from the first request // in your second request, the second request will contain the following value: // {1:result:playlistContent} if (preg_match('/\\{([0-9]*)\\:result\\:?(.*)?\\}/', $val, $matches)) { $resultIndex = $matches[1]; $resultKey = $matches[2]; if (count($results) >= $resultIndex) { if (strlen(trim($resultKey)) > 0) { $resultPathArray = explode(":", $resultKey); } else { $resultPathArray = array(); } $val = $this->getValueFromObject($results[$resultIndex], $resultPathArray); } } } try { // cached parameters should be different when the request is part of a multirequest // as part of multirequest - the cached data is a serialized php object // when not part of multirequest - the cached data is the actual response $currentParams['multirequest'] = true; unset($currentParams['format']); $cache = new KalturaResponseCacher($currentParams); if (!isset($currentParams['ks']) && kCurrentContext::$ks) { $cache->setKS(kCurrentContext::$ks); } $cachedResult = $cache->checkCache('X-Kaltura-Part-Of-MultiRequest'); if ($cachedResult) { $currentResult = unserialize($cachedResult); } else { $currentResult = $this->dispatcher->dispatch($currentService, $currentAction, $currentParams); // store serialized resposne in cache $cache->storeCache(serialize($currentResult)); } } catch (Exception $ex) { $currentResult = $this->getExceptionObject($ex); KalturaResponseCacher::disableCache(); } $results[$i] = $currentResult; $i++; } return $results; }
public function handleMultiRequest() { // arrange the parameters by request index $commonParams = array(); $listOfRequests = array(); $requestStartIndex = 1; $requestEndIndex = 1; foreach ($this->params as $paramName => $paramValue) { if (is_numeric($paramName)) { $paramName = intval($paramName); $requestStartIndex = min($requestStartIndex, $paramName); $requestEndIndex = max($requestEndIndex, $paramName); $listOfRequests[$paramName] = $paramValue; continue; } $explodedName = explode(':', $paramName, 2); if (count($explodedName) <= 1 || !is_numeric($explodedName[0])) { $commonParams[$paramName] = $paramValue; continue; } $requestIndex = (int) $explodedName[0]; $requestStartIndex = min($requestStartIndex, $requestIndex); $requestEndIndex = max($requestEndIndex, $requestIndex); $paramName = $explodedName[1]; if (!array_key_exists($requestIndex, $listOfRequests)) { $listOfRequests[$requestIndex] = array(); } $listOfRequests[$requestIndex][$paramName] = $paramValue; } $multiRequestResultsPaths = $this->getMultiRequestResultsPaths($listOfRequests); // process the requests $results = array(); kCurrentContext::$multiRequest_index = 0; for ($i = $requestStartIndex; $i <= $requestEndIndex; $i++) { $currentParams = $listOfRequests[$i]; if (!isset($currentParams["service"]) || !isset($currentParams["action"])) { break; } kCurrentContext::$multiRequest_index++; $currentService = $currentParams["service"]; $currentAction = $currentParams["action"]; // copy derived common params to current params if (isset($commonParams['clientTag']) && !isset($currentParams['clientTag'])) { $currentParams['clientTag'] = $commonParams['clientTag']; } if (isset($commonParams['ks']) && !isset($currentParams['ks'])) { $currentParams['ks'] = $commonParams['ks']; } if (isset($commonParams['partnerId']) && !isset($currentParams['partnerId'])) { $currentParams['partnerId'] = $commonParams['partnerId']; } // cached parameters should be different when the request is part of a multirequest // as part of multirequest - the cached data is a serialized php object // when not part of multirequest - the cached data is the actual response $currentParams['multirequest'] = true; unset($currentParams['format']); $cache = new KalturaResponseCacher($currentParams); $success = true; $errorCode = null; $this->onRequestStart($currentService, $currentAction, $currentParams, kCurrentContext::$multiRequest_index, true); $cachedResult = $cache->checkCache('X-Kaltura-Part-Of-MultiRequest'); if ($cachedResult) { $currentResult = unserialize($cachedResult); } else { if (kCurrentContext::$multiRequest_index != 1) { kMemoryManager::clearMemory(); KalturaCriterion::clearTags(); } try { $currentResult = $this->dispatcher->dispatch($currentService, $currentAction, $currentParams); } catch (Exception $ex) { $success = false; $errorCode = $ex->getCode(); $currentResult = $this->getExceptionObject($ex, $currentService, $currentAction); } $cache->storeCache($currentResult, "", true); } $this->onRequestEnd($success, $errorCode, kCurrentContext::$multiRequest_index); for ($nextMultiRequestIndex = $i + 1; $nextMultiRequestIndex <= count($listOfRequests); $nextMultiRequestIndex++) { if (isset($multiRequestResultsPaths[$nextMultiRequestIndex])) { $listOfRequests[$nextMultiRequestIndex] = $this->replaceMultiRequestResults(kCurrentContext::$multiRequest_index, $multiRequestResultsPaths[$nextMultiRequestIndex], $listOfRequests[$nextMultiRequestIndex], $currentResult); } } $results[kCurrentContext::$multiRequest_index] = $this->serializer->serialize($currentResult); // in case a serve action is included in a multirequest, return only the result of the serve action // in order to avoid serializing the kRendererBase object and returning the internal server paths to the client if ($currentResult instanceof kRendererBase) { return $currentResult; } } return $results; }
public function handleMultiRequest() { // arrange the parameters by request index $commonParams = array(); $listOfRequests = array(); $dependencies = array(); $allDependencies = array(); $pastResults = array(); foreach ($this->params as $paramName => $paramValue) { $explodedName = explode(':', $paramName, 2); if (count($explodedName) <= 1 || !is_numeric($explodedName[0])) { $commonParams[$paramName] = $paramValue; continue; } $requestIndex = (int) $explodedName[0]; $paramName = $explodedName[1]; if (!array_key_exists($requestIndex, $listOfRequests)) { $listOfRequests[$requestIndex] = array(); } $listOfRequests[$requestIndex][$paramName] = $paramValue; $matches = array(); if (preg_match('/\\{([0-9]*)\\:result\\:?(.*)?\\}/', $paramValue, $matches)) { $pastResultsIndex = $matches[0]; $resultIndex = $matches[1]; $resultKey = $matches[2]; if (!isset($dependencies[$requestIndex][$pastResultsIndex])) { $dependencies[$resultIndex][$pastResultsIndex] = $resultKey; } $allDependencies[$pastResultsIndex] = true; } } // process the requests $results = array(); for ($i = 1; isset($listOfRequests[$i]); $i++) { $currentParams = $listOfRequests[$i]; if (!isset($currentParams["service"]) || !isset($currentParams["action"])) { break; } kCurrentContext::$multiRequest_index = $i; $currentService = $currentParams["service"]; $currentAction = $currentParams["action"]; // copy derived common params to current params if (isset($commonParams['clientTag']) && !isset($currentParams['clientTag'])) { $currentParams['clientTag'] = $commonParams['clientTag']; } if (isset($commonParams['ks']) && !isset($currentParams['ks'])) { $currentParams['ks'] = $commonParams['ks']; } if (isset($commonParams['partnerId']) && !isset($currentParams['partnerId'])) { $currentParams['partnerId'] = $commonParams['partnerId']; } // check if we need to replace params with prev results foreach ($currentParams as $key => &$val) { if (isset($pastResults[$val])) { $val = $pastResults[$val]; } else { if (isset($allDependencies[$val])) { $val = null; } } } // cached parameters should be different when the request is part of a multirequest // as part of multirequest - the cached data is a serialized php object // when not part of multirequest - the cached data is the actual response $currentParams['multirequest'] = true; unset($currentParams['format']); $cache = new KalturaResponseCacher($currentParams); $success = true; $errorCode = null; $this->onRequestStart($currentService, $currentAction, $currentParams, $i, true); $cachedResult = $cache->checkCache('X-Kaltura-Part-Of-MultiRequest'); if ($cachedResult) { $currentResult = unserialize($cachedResult); } else { if ($i != 1) { kMemoryManager::clearMemory(); KalturaCriterion::clearTags(); } try { $currentResult = $this->dispatcher->dispatch($currentService, $currentAction, $currentParams); } catch (Exception $ex) { $success = false; $errorCode = $ex->getCode(); $currentResult = $this->getExceptionObject($ex, $currentService, $currentAction); } $cache->storeCache($currentResult, "", true); } $this->onRequestEnd($success, $errorCode, $i); if (isset($dependencies[$i])) { foreach ($dependencies[$i] as $currentDependency => $dependencyName) { if (strlen(trim($dependencyName)) > 0) { $resultPathArray = explode(":", $dependencyName); } else { $resultPathArray = array(); } $currValue = $this->getValueFromObject($currentResult, $resultPathArray); $pastResults[$currentDependency] = $currValue; } } $results[$i] = $this->serializer->serialize($currentResult); // in case a serve action is included in a multirequest, return only the result of the serve action // in order to avoid serializing the kRendererBase object and returning the internal server paths to the client if ($currentResult instanceof kRendererBase) { return $currentResult; } } return $results; }