本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent::isMasterRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForControllerResultEvent::isMasterRequest方法的具体用法?PHP GetResponseForControllerResultEvent::isMasterRequest怎么用?PHP GetResponseForControllerResultEvent::isMasterRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
的用法示例。
在下文中一共展示了GetResponseForControllerResultEvent::isMasterRequest方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if ('json' !== $request->getRequestFormat()) {
return;
}
$result = $event->getControllerResult();
if (null === $result) {
$event->setResponse(new JsonResponse(null, 204));
return;
}
if ($result instanceof Response) {
return;
}
$response = new JsonResponse();
if ($request->isMethod('POST')) {
$response->setStatusCode(201);
}
if (!is_scalar($result)) {
$result = $this->getSerializer()->normalize($result, null, ['groups' => $this->options['serialization_default_groups']]);
}
$response->setData($result);
$event->setResponse($response);
}
示例2: onKernelView
/**
* Persists, updates or delete data return by the controller if applicable.
*
* @param GetResponseForControllerResultEvent $event
*
* @return mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if (!in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_DELETE])) {
return;
}
$resourceType = $request->attributes->get('_resource_type');
if (!$resourceType) {
return;
}
$controllerResult = $event->getControllerResult();
if (null === ($objectManager = $this->getManager($resourceType, $controllerResult))) {
return $controllerResult;
}
switch ($request->getMethod()) {
case Request::METHOD_POST:
$objectManager->persist($controllerResult);
break;
case Request::METHOD_DELETE:
$objectManager->remove($controllerResult);
$event->setControllerResult(null);
break;
}
$objectManager->flush();
}
示例3: onKernelView
/**
* In an API context, converts any data to a JSON-LD response.
*
* @param GetResponseForControllerResultEvent $event
*
* @return JsonLdResponse|mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$controllerResult = $event->getControllerResult();
if ($controllerResult instanceof Response) {
return;
}
$request = $event->getRequest();
$format = $request->attributes->get('_api_format');
if (self::FORMAT !== $format) {
return;
}
switch ($request->getMethod()) {
case Request::METHOD_POST:
$status = 201;
break;
case Request::METHOD_DELETE:
$status = 204;
break;
default:
$status = 200;
break;
}
$resourceType = $request->attributes->get('_resource_type');
$response = new JsonLdResponse($resourceType ? $this->normalizer->normalize($controllerResult, self::FORMAT, $resourceType->getNormalizationContext() + ['request_uri' => $request->getRequestUri()]) : $controllerResult, $status);
$event->setResponse($response);
}
示例4: onKernelView
/**
* Auto-convert to JsonResponse whether the controller result is a ProgressBar instance.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$result = $event->getControllerResult();
if (!$event->isMasterRequest() || !$request->isXmlHttpRequest() || !$result instanceof ProgressBar) {
return;
}
$event->setResponse($this->createResponse($result));
}
示例5: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$this->logger->debug('Response builder listener: catch kernel.request event');
// If this is not a master request, skip handling
if (!$event->isMasterRequest()) {
$this->logger->debug('Response builder listener: this is not master request, skip');
return;
}
// If content already prepared
if ($event->hasResponse()) {
$this->logger->debug('Response builder listener: event already has response, skip');
return;
}
// Getting controller result
$result = $event->getControllerResult();
// If result is Response instance
if ($result instanceof Response) {
$this->logger->debug('Response builder listener: controller result is already an instance of Response, skip');
return;
}
// Getting action
$apiServerAction = $event->getRequest()->attributes->get('apiAction');
/* @var $apiServerAction ApiServerAction */
// Something wrong
if (!$apiServerAction) {
$this->logger->error('Response parser listener: request has no apiAction attribute, throwing access denied exception');
throw new AccessDeniedHttpException();
}
// Getting api server interface
$apiServerInterface = $apiServerAction->getApiServerInterface();
// Getting connection
$apiServerConnection = $event->getRequest()->attributes->get('apiConnection');
/* @var $apiServerConnection ApiServerConnection */
// Something wrong
if (!$apiServerConnection) {
$this->logger->error('Response parser listener: request has no apiConnection attribute, throwing access denied exception');
throw new AccessDeniedHttpException();
}
// Creating api response
try {
$apiResponse = $apiServerInterface->getApiResponse($result);
$newApiUserToken = $apiServerConnection->getNewApiUserToken();
if ($newApiUserToken) {
$this->logger->debug('Response builder listener: applying new api user token to response');
$apiResponse->setUserToken($newApiUserToken);
}
} catch (\Exception $e) {
$this->logger->error(sprintf('Response parser listener: problem with building response ("%s"), sending exception response', $e->getMessage()));
$apiResponse = $apiServerInterface->getExceptionResponse($e->getMessage());
}
// Setting response
$event->setResponse(new JsonResponse($apiResponse->export()));
$this->logger->debug('Response builder listener: response built');
}
示例6: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$apiResponse = $event->getControllerResult();
if (!$apiResponse instanceof ApiResponse) {
return;
}
$response = $this->toSymfonyResponse($apiResponse, $event->getRequest());
$event->setResponse($response);
}
示例7: onKernelView
/**
* Validates data returned by the controller if applicable.
*
* @param GetResponseForControllerResultEvent $event
*
* @return mixed
*
* @throws ValidationException
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$resourceType = $event->getRequest()->attributes->get('_resource_type');
if (!$resourceType || !in_array($event->getRequest()->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT])) {
return;
}
$violations = $this->validator->validate($event->getControllerResult(), null, $resourceType->getValidationGroups());
if (0 !== count($violations)) {
throw new ValidationException($violations);
}
}
示例8: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$result = $event->getControllerResult();
if (!$result instanceof ControllerResult) {
return;
}
$attributes = $event->getRequest()->attributes;
$app = array_replace($this->appDefaults, $attributes->get('_app', []), $result->app->all());
$result->app->replace($app);
$attributes->set('_app', $app);
}
示例9: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$result = $event->getControllerResult();
if (!$result instanceof ControllerResult) {
return;
}
$content = '';
$template = $result->app->get('page_template');
if ($template) {
$content = trim($this->templating->render($template, $result->view->all()));
}
$event->setResponse(new Response($content));
}