public function onKernelRequest(GetResponseEvent $event) { $this->logger->debug('Entity builder listener: catch kernel.request event'); // If this is not a master request, skip handling if (!$event->isMasterRequest()) { $this->logger->debug('Entity builder listener: this is not master request, skip'); return; } // If content already prepared if ($event->hasResponse()) { $this->logger->debug('Entity builder listener: event already has response, skip'); return; } // Getting request $request = $event->getRequest(); // Getting action $apiServerAction = $event->getRequest()->attributes->get('apiAction'); /* @var $apiServerAction ApiServerAction */ // Something wrong if (!$apiServerAction) { $this->logger->error('Request parser listener: request has no apiAction attribute, throwing access denied exception'); throw new AccessDeniedHttpException(); } // Creating request data entity try { $apiEntity = $apiServerAction->getRequestedEntity($request->attributes->get('apiData')); } catch (\Exception $e) { $this->logger->notice(sprintf('Request parser listener: unable to convert apiData to entity ("%s"), apiEntity set tu null', $e->getMessage())); $apiEntity = null; } // Setting request attributes $request->attributes->set('requestData', $apiEntity); // Cleaning request attributes $request->attributes->remove('apiData'); }
public function onKernelException(GetResponseForExceptionEvent $event) { $this->logger->notice(sprintf('Exceptions catcher listener: catch kernel.exception event (exception: %s)', $event->getException()->getMessage())); // If this is not a master request, skip handling if (!$event->isMasterRequest()) { $this->logger->debug('Exceptions catcher listener: this is not master request, skip'); return; } // If content already prepared if ($event->hasResponse()) { $this->logger->debug('Exceptions catcher listener: event already has response, skip'); return; } // Getting action $apiServerAction = $event->getRequest()->attributes->get('apiAction'); /* @var $apiServerAction ApiServerAction */ // Something wrong if (!$apiServerAction) { $this->logger->error('Request parser listener: request has no apiAction attribute, sending empty response'); $event->setResponse(new JsonResponse([])); return; } // Getting api server interface $apiServerInterface = $apiServerAction->getApiServerInterface(); // Creating api response $apiResponse = $apiServerInterface->getExceptionResponse($event->getException()->getMessage()); // Setting response $event->setResponse(new JsonResponse($apiResponse->export())); }
private function configure($controllersNamespace, $defaultController, $defaultAction, $requestAction) { // checking base controller namespace if (!$controllersNamespace) { $this->logger->critical('Action: interface namespace is not configured'); throw new ApiServerException(sprintf('Action of interface "%s" error: controllers namespace not configured', $this->getInterfaceName())); } // reset variables $path = []; $controller = null; $action = null; // parsing request action $requestAction = str_replace(['/', '\\'], ':', $requestAction); $requestAction = trim($requestAction, ':'); if ($requestAction) { $requestActionParts = explode(':', $requestAction); if (count($requestActionParts) < 2) { $controller = ucfirst($requestActionParts[0]); } else { $action = lcfirst(array_pop($requestActionParts)); $controller = ucfirst(array_pop($requestActionParts)); $path = $requestActionParts; } } if (!$controller) { $controller = ucfirst($defaultController); } if (!$action) { $action = lcfirst($defaultAction); } if (!$controller || !$action) { $this->logger->notice(sprintf('Action: unable to route action "%s"', $requestAction)); throw new ApiServerException(sprintf('Action of interface "%s" error: unable to route action "%s"', $this->getInterfaceName(), $requestAction)); } foreach ($path as &$subPath) { $subPath = ucfirst($subPath); } // calculating local class name $localClassName = $path; array_unshift($localClassName, 'Request'); array_push($localClassName, $controller); array_push($localClassName, ucfirst($action) . 'Data'); $this->localClassName = implode('\\', $localClassName); // calculating controller class name, method and symfony action route $controllerClassName = $path; array_unshift($controllerClassName, Standard::normalizeNamespace($controllersNamespace)); array_push($controllerClassName, $controller . 'Controller'); $controllerClassName = implode('\\', $controllerClassName); if (!class_exists($controllerClassName)) { $this->logger->notice(sprintf('Action: unable to route action "%s" because class "%s" not exists', $requestAction, $controllerClassName)); throw new ApiServerException(sprintf('Action of interface "%s" error: unable to find "%s" class', $this->getInterfaceName(), $controllerClassName)); } $controllerMethodName = $action . 'Action'; if (!method_exists($controllerClassName, $controllerMethodName)) { $this->logger->notice(sprintf('Action: unable to route action "%s" because class "%s" does not has method "%s"', $requestAction, $controllerClassName, $controllerMethodName)); throw new ApiServerException(sprintf('Action of interface "%s" error: controller "%s" has no method "%s"', $this->getInterfaceName(), $controllerClassName, $controllerMethodName)); } $this->actionRoute = $controllerClassName . '::' . $controllerMethodName; $this->logger->debug(sprintf('Action: route set to "%s"', $this->actionRoute)); }
public function onKernelRequest(GetResponseEvent $event) { $this->logger->debug('Request parser listener: catch kernel.request event'); // If this is not a master request, skip handling if (!$event->isMasterRequest()) { $this->logger->debug('Request parser listener: this is not master request, skip'); return; } // If content already prepared if ($event->hasResponse()) { $this->logger->debug('Request parser listener: event already has response, skip'); return; } // getting request $request = $event->getRequest(); // getting content $content = $request->getContent(); $this->logger->debug(sprintf('Request parser listener: request content >>> %s', $content)); $content = @json_decode($content, true); if (!is_array($content)) { $this->logger->notice('Request parser listener: request content is not in the JSON format, throwing access denied exception'); throw new AccessDeniedHttpException(); } // creating ApiRequest $apiRequest = new ApiRequest($content); $this->logger->debug(sprintf('Request parser listener: request params. Token: "%s", user token: "%s", action: "%s"', $apiRequest->getToken(), $apiRequest->getUserToken(), $apiRequest->getAction())); if (!$apiRequest->getToken()) { $this->logger->notice('Request parser listener: request has no token, throwing access denied exception'); throw new AccessDeniedHttpException(); } // Calculating interface name $host = strtolower($request->server->get('SERVER_NAME')); $interfaceName = array_key_exists($host, $this->interfacesHosts) ? $this->interfacesHosts[$host] : $this->defaultInterface; if (!$interfaceName) { $this->logger->notice(sprintf('Request parser listener: interface for hostname "%s" not detected, throwing access denied exception', $host)); throw new AccessDeniedHttpException(); } $this->logger->debug(sprintf('Request parser listener: "%s" interface selected for handling connection from "%s"', $interfaceName, $host)); // Building interface $apiServerInterface = $this->apiServerInterfaceFactory->buildInterface($interfaceName); // Building action $apiServerAction = $apiServerInterface->buildAction($apiRequest->getAction()); $this->logger->debug(sprintf('Request parser listener: action route selected >>> %s', $apiServerAction->getActionRoute())); // Setting request attributes $request->attributes->set('apiClientToken', $apiRequest->getToken()); $request->attributes->set('apiUserToken', $apiRequest->getUserToken()); $request->attributes->set('apiAction', $apiServerAction); $request->attributes->set('apiData', $apiRequest->getData()); // Setting route $request->attributes->set('_controller', $apiServerAction->getActionRoute()); }
/** * @Route("/elevage/{pageEleveurSlug}", name="getPageEleveur_route") * @Method("GET") * * @param $pageEleveurSlug * @param Request $request * @return RedirectResponse|Response */ public function getAction($pageEleveurSlug, Request $request) { $pageEleveur = $this->pageEleveurService->findBySlug($pageEleveurSlug); if (!$pageEleveur) { throw new NotFoundHttpException(null, null); } /** @var AnonymousToken $token */ $token = $this->tokenStorage->getToken(); /** @var User $user */ $user = $token->getUser(); $isOwner = $user !== 'anon.' && $pageEleveur->getOwner()->getId() === $user->getId(); $isPreview = $request->query->has('preview'); if ($isPreview && !$isOwner) { $this->logger->notice('Requete visiteur sur page eleveur en mode preview', ['user' => $user, 'pageEleveurSlug' => $pageEleveurSlug, 'pageEleveurId' => $pageEleveur->getId()]); return new RedirectResponse($this->router->generate('getPageEleveur_route', ['pageEleveurSlug' => $pageEleveurSlug])); } return $this->templating->renderResponse('base.html.twig', [TwigNodeTemplateTreeSection::TEMPLATE_TREE_BRANCH => 'editable/page-eleveur', 'pageEleveur' => $pageEleveur, 'isEditable' => $isOwner && !$isPreview]); }
public function testCountErrorsWithDebugHandler() { $handler = new DebugHandler(); $logger = new Logger(__METHOD__, array($handler)); $this->assertTrue($logger->debug('test message')); $this->assertTrue($logger->info('test message')); $this->assertTrue($logger->notice('test message')); $this->assertTrue($logger->warning('test message')); $this->assertTrue($logger->error('test message')); $this->assertTrue($logger->critical('test message')); $this->assertTrue($logger->alert('test message')); $this->assertTrue($logger->emergency('test message')); $this->assertSame(4, $logger->countErrors()); }
public function apiUserAuthenticated(ApiUserInterface $user) { $this->logger->notice(sprintf('Connection: trying to authenticate user as "%s" [id: %s]', $user->getName(), $user->getId())); if ($this->getApiUser()) { $this->logger->notice(sprintf('Connection: user is already authenticated as "%s" [id: %s]', $this->getApiUser()->getName(), $this->getApiUser()->getId())); throw new ApiServerException('User is already authenticated'); } if (!$user || !$user->getId()) { $this->logger->error('Connection: trying to authenticate with undefined user'); throw new ApiServerException('Trying to authenticate with undefined user'); } $this->apiUser = $user; $this->logger->notice('Connection: user authenticated'); $this->generateNewApiUserToken(); }
/** * @param string $nom * @param User $owner * @return PageEleveur * @throws HistoryException */ public function create($nom, User $owner) { if ($this->pageEleveurBranchRepository->findByOwner($owner)) { $this->logger->notice('', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); throw new HistoryException(HistoryException::DEJA_OWNER); } $commit = new PageEleveurCommit(null, $nom, null, null, null, null, null, null); $pageEleveurBranch = new PageEleveurBranch(); $pageEleveurBranch->setCommit($commit); try { $pageEleveurBranch->setSlug(static::slug($nom)); } catch (InvalidArgumentException $e) { throw new HistoryException(HistoryException::NOM_INVALIDE); } $pageEleveurBranch->setOwner($owner); if ($this->pageEleveurBranchRepository->findBySlug($pageEleveurBranch->getSlug())) { throw new HistoryException(HistoryException::SLUG_DEJA_EXISTANT); } $this->doctrine->persist($pageEleveurBranch->getCommit()); $this->doctrine->persist($pageEleveurBranch); $this->doctrine->flush([$pageEleveurBranch->getCommit(), $pageEleveurBranch]); return $this->fromBranch($pageEleveurBranch); }