public function prepareViewModel(MvcEvent $event, $action) { if ($event->getTarget()->forward()->getNumNestedForwards() > 0) { return $event->getResult(); } $result = $event->getResult(); if (!isset($result)) { $result = new Result(); } $response = $event->getResponse(); $response->setStatusCode($result->getStatusCode()); $response->getHeaders()->addHeaders($result->getHeaders()); $controller = $event->getTarget(); $viewModel = $controller->acceptableViewModelSelector($controller->getOptions()->getAcceptCriteria()); //set the template if ($viewModel instanceof JsonModel && count($viewModel->getVariables()) == 0) { if ($response->getStatusCode() == 200) { $response->setStatusCode(204); } return $response; } elseif ($viewModel->getTemplate() == null) { $viewModel->setTemplate($controller->getOptions()->getTemplates()[$action]); } $event->setResult($viewModel); return $viewModel; }
public function getList(MvcEvent $event) { $options = $event->getTarget()->getOptions(); $documentManager = $options->getModelManager(); $metadata = $documentManager->getClassMetadata($options->getClass()); if ($list = $event->getParam('list')) { $list = $list->getValues(); } unset($this->range); $criteria = $this->getCriteria($event); //filter list on criteria if (count($criteria) > 0 && $list) { $list = $this->applyCriteriaToList($list, $criteria, $metadata); } if ($list) { $total = count($list); } else { //load the total from the db $totalQuery = $documentManager->createQueryBuilder()->find($metadata->name); $total = $this->addCriteriaToQuery($totalQuery, $criteria, $metadata, $documentManager)->getQuery()->execute()->count(); } if ($total == 0) { $result = new Result([]); $result->setStatusCode(204); $event->setResult($result); return $result; } $offset = $this->getOffset($event); if ($offset > $total - 1) { throw new Exception\BadRangeException(); } $sort = $this->getSort($event); if ($list) { //apply any required sort to the result if (count($sort) > 0) { $this->applySortToList($list, $sort, $metadata); } $list = array_slice($list, $offset, $this->getLimit($event)); } else { $resultsQuery = $documentManager->createQueryBuilder()->find($metadata->name); $this->addCriteriaToQuery($resultsQuery, $criteria, $metadata, $documentManager); $resultsQuery->limit($this->getLimit($event))->skip($offset); $list = $this->addSortToQuery($resultsQuery, $sort)->eagerCursor(true)->getQuery()->execute(); } $max = $offset + count($list) - 1; $result = new Result($list); $result->addHeader(ContentRange::fromString("Content-Range: {$offset}-{$max}/{$total}")); $event->setResult($result); return $result; }
public function deleteList(MvcEvent $event) { if ($list = $event->getParam('list')) { foreach ($list->getKeys() as $key) { $list->remove($key); } } else { $options = $event->getTarget()->getOptions(); $options->getModelManager()->createQueryBuilder($options->getClass())->remove()->getQuery()->execute(); } if (!($result = $event->getResult())) { $result = new Result([]); $result->setStatusCode(204); $event->setResult($result); } return $result; }
protected function doAction(MvcEvent $event, $metadata, $documentManager) { $options = $event->getTarget()->getOptions(); if (!($document = $event->getParam('document'))) { // document not set, so load it $document = $documentManager->createQueryBuilder()->find($metadata->name)->field($options->getProperty())->equals($event->getParam('id'))->getQuery()->getSingleResult(); if (!$document) { throw new Exception\DocumentNotFoundException(); } } $result = new Result($document); if (isset($metadata->stamp['updatedOn'])) { $lastModified = new LastModified(); $lastModified->setDate($metadata->getFieldValue($document, $metadata->stamp['updatedOn'])); $result->addHeader($lastModified); } $result->addHeader($options->getCacheControl()); $event->setResult($result); return $result; }
protected function handleAssociatedCollection(MvcEvent $event, $metadata, $documentManager, $field) { $deeperResource = $event->getParam('deeperResource'); if (count($deeperResource) == 0) { $document = $this->loadDocument($event, $metadata, $documentManager, $field); $collection = $metadata->getFieldValue($document, $field); foreach ($collection->getKeys() as $key) { $collection->remove($key); } $result = new Result([]); $result->setStatusCode(204); $event->setResult($result); return $result; } if (isset($metadata->fieldMappings[$field]['reference'])) { $event->getRequest()->getQuery()->set($metadata->fieldMappings[$field]['mappedBy'], $event->getParam('id')); $targetOptions = $this->getRestControllerMap($event)->getOptionsFromEndpoint($event->getTarget()->getOptions()->getEndpoint() . '.' . $field); $id = array_shift($deeperResource); $event->setParam('id', $id); $event->setParam('deeperResource', $deeperResource); $event->setParam('document', null); return $event->getTarget()->forward()->dispatch('shard.rest.' . $targetOptions->getEndpoint(), ['id' => $id]); } $document = $this->loadDocument($event, $metadata, $documentManager, $field); $endpoint = $event->getTarget()->getOptions()->getEndpoint(); $collection = $metadata->getFieldValue($document, $field); if (!($targetDocument = $this->selectItemFromCollection($collection, $deeperResource[0], $this->getRestControllerMap($event)->getOptionsFromEndpoint($endpoint . '.' . $field)->getProperty()))) { //embedded document not found in collection throw new Exception\DocumentNotFoundException(); } if (!isset($targetDocument)) { //embedded document not found in collection throw new Exception\DocumentNotFoundException(); } $id = array_shift($deeperResource); $event->setParam('id', $id); if (count($deeperResource) == 0) { $collection->removeElement($targetDocument); $result = new Result([]); $result->setStatusCode(204); $event->setResult($result); return $result; } else { array_shift($deeperResource); $event->setParam('deeperResource', $deeperResource); $event->setParam('document', $targetDocument); return $event->getTarget()->forward()->dispatch('shard.rest.' . $endpoint . '.' . $field); } }
public function create(MvcEvent $event) { $batchRequest = $event->getRequest(); $router = $this->getRouter(); $controllerLoader = $this->getControllerLoader(); $responseModel = []; foreach ($event->getParam('data') as $key => $requestData) { $request = $this->getSubRequest($batchRequest, $requestData); $response = new Response(); $subEvent = new MvcEvent(); $subEvent->setRequest($request); $subEvent->setResponse($response); $match = $router->match($request); RouteListener::resolveController($match); $contentModel = null; if (!isset($match) || $match->getMatchedRouteName() != 'rest') { $contentModel = $this->createExceptionContentModel(new Exception\RuntimeException(sprintf('%s uri is not a rest route, so is not supported by batch controller.', $requestData['uri'])), $subEvent); } else { try { $controller = $controllerLoader->get($match->getParam('controller')); } catch (\Zend\ServiceManager\Exception\ServiceNotFoundException $exception) { $contentModel = $this->createExceptionContentModel($exception, $subEvent); $response->setStatusCode(404); } $subEvent->setRouteMatch($match); $controller->setEvent($subEvent); if (!isset($contentModel)) { try { $contentModel = $controller->dispatch($request, $response); } catch (\Exception $exception) { $contentModel = $this->createExceptionContentModel($exception, $subEvent); } } } foreach ($response->getHeaders() as $header) { $headers[$header->getFieldName()] = $header->getFieldValue(); } $responseModel[$key] = ['status' => $response->getStatusCode(), 'headers' => $headers]; if ($contentModel instanceof ModelInterface) { $responseModel[$key]['content'] = $contentModel->getVariables(); } } $result = new Result(); $result->setSerializedModel($responseModel); $event->setResult($result); return $result; }