Ejemplo n.º 1
0
 /**
  * @param string $assetName          Asset name, eg. "style.css". See gulpfile.js for available assets.
  * @param string $manifestCollection Manifest collection, eg. "rev-styles.css". See gulpfile.js for available collections.
  *
  * @return mixed
  *
  * @throws \Undine\Functions\Exception\JsonParseException
  */
 public function getRevAsset($assetName, $manifestCollection)
 {
     $fileName = $this->manifestPath . '/' . $manifestCollection;
     if (!file_exists($fileName)) {
         throw new \RuntimeException(sprintf('Manifest file "%s" doest not exist.', $fileName));
     }
     $json = file_get_contents($fileName);
     if ($json === false) {
         $error = error_get_last();
         throw new \RuntimeException(sprintf('Unable to read the manifest file "%s": %s', $fileName, $error['message']));
     }
     $manifest = \Undine\Functions\json_parse($json);
     if (!isset($manifest[$assetName])) {
         throw new \RuntimeException(sprintf('The asset named "%s" could not be found in manifest collection "%s"; found assets are: "%s".', $assetName, $manifestCollection, implode('", "', array_keys($manifest))));
     }
     return $manifest[$assetName];
 }
Ejemplo n.º 2
0
 /**
  * Finds a JSON line that should be our response.
  * Should handle edge-cases where the Oxygen module's output is polluted prematurely (by PHP errors)
  * or in the shutdown context (by register_shutdown_function output).
  *
  * @param string $responseId
  * @param RequestInterface $request
  * @param array $requestOptions
  * @param ResponseInterface $response
  * @param TransferInfo $transferInfo
  *
  * @return array
  *
  * @throws OxygenException
  * @throws ResponseException
  */
 private function extractData($responseId, RequestInterface $request, array $requestOptions, ResponseInterface $response, TransferInfo $transferInfo)
 {
     $createException = function ($code, \Exception $previous = null) use($request, $requestOptions, $response, $transferInfo) {
         throw new ResponseException($code, $request, $requestOptions, $response, $transferInfo, $previous);
     };
     if ($response->getBody()->getSize() > 10 * 1024 * 1024) {
         // Safe-guard; don't parse the body if it's larger than 10MB.
         throw $createException(ResponseException::BODY_TOO_LARGE);
     }
     // Find all lines that might represent a JSON string.
     $matchFound = preg_match(sprintf('{^({"oxygenResponseId":"%s",.*?})\\s?$}m', preg_quote($responseId)), (string) $response->getBody(), $matches);
     if (!$matchFound) {
         throw $createException(ResponseException::RESPONSE_NOT_FOUND);
     }
     try {
         $data = \Undine\Functions\json_parse($matches[1]);
     } catch (JsonParseException $e) {
         throw $createException(ResponseException::RESPONSE_INVALID_JSON);
     }
     // Our response should always resolve to an array.
     if (!is_array($data)) {
         throw $createException(ResponseException::RESPONSE_NOT_AN_ARRAY);
     }
     if (isset($data['exception'])) {
         if (!is_array($data['exception'])) {
             throw $createException(ResponseException::EXCEPTION_NOT_ARRAY);
         }
         try {
             throw OxygenException::createFromData($data['exception'], $request, $requestOptions, $response, $transferInfo);
         } catch (ExceptionInterface $e) {
             throw $createException(ResponseException::MALFORMED_EXCEPTION, $e);
         }
     } elseif (isset($data['actionResult'])) {
         if (!is_array($data['actionResult'])) {
             throw $createException(ResponseException::ACTION_RESULT_NOT_ARRAY);
         }
         if (!isset($data['stateResult'])) {
             throw $createException(ResponseException::STATE_EMPTY);
         }
         if (!is_array($data['stateResult'])) {
             throw $createException(ResponseException::STATE_NOT_ARRAY);
         }
         return $data;
     }
     throw $createException(ResponseException::RESULT_NOT_FOUND, $request, $response, $transferInfo);
 }