/** * Perform content negotiation * * For HTTP methods expecting body content, attempts to match the incoming * content-type against the list of allowed content types, and then performs * appropriate content deserialization. * * If an error occurs during deserialization, an ApiProblemResponse is * returned, indicating an issue with the submission. * * @param MvcEvent $e * @return null|ApiProblemResponse */ public function __invoke(MvcEvent $e) { $request = $e->getRequest(); if (!method_exists($request, 'getHeaders')) { // Not an HTTP request; nothing to do return; } $routeMatch = $e->getRouteMatch(); $parameterData = new ParameterDataContainer(); // route parameters: $routeParams = $routeMatch->getParams(); $parameterData->setRouteParams($routeParams); // query parameters: $parameterData->setQueryParams($request->getQuery()->toArray()); // body parameters: $bodyParams = array(); $contentType = $request->getHeader('Content-type'); switch ($request->getMethod()) { case $request::METHOD_POST: if ($contentType && $contentType->match('application/json')) { $bodyParams = $this->decodeJson($request->getContent()); break; } $bodyParams = $request->getPost()->toArray(); break; case $request::METHOD_PATCH: case $request::METHOD_PUT: case $request::METHOD_DELETE: $content = $request->getContent(); if ($contentType && $contentType->match('multipart/form-data')) { $parser = new MultipartContentParser($contentType, $request); $bodyParams = $parser->parse(); if ($request->getFiles()->count()) { $this->attachFileCleanupListener($e, $parser->getUploadTempDir()); } break; } if ($contentType && $contentType->match('application/json')) { $bodyParams = $this->decodeJson($content); break; } // Stolen from AbstractRestfulController parse_str($content, $bodyParams); if (!is_array($bodyParams) || 1 == count($bodyParams) && isset($bodyParams[0])) { $bodyParams = $content; } break; default: break; } if ($bodyParams instanceof ApiProblemResponse) { return $bodyParams; } $bodyParams = $bodyParams ?: array(); $parameterData->setBodyParams($bodyParams); $e->setParam('ZFContentNegotiationParameterData', $parameterData); }
public function testOnFinishDoesNotRemoveUploadFilesTheListenerDidNotCreate() { $tmpDir = MultipartContentParser::getUploadTempDir(); $tmpFile = tempnam($tmpDir, 'php'); file_put_contents($tmpFile, 'File created by ' . __CLASS__); $files = new Parameters(array('test' => array('error' => UPLOAD_ERR_OK, 'name' => 'test.txt', 'type' => 'text/plain', 'tmp_name' => $tmpFile, 'size' => filesize($tmpFile)))); $request = new Request(); $request->setFiles($files); $event = new MvcEvent(); $event->setRequest($request); $this->listener->onFinish($event); $this->assertTrue(file_exists($tmpFile)); unlink($tmpFile); }