本文整理汇总了PHP中Zend\Mvc\MvcEvent::setError方法的典型用法代码示例。如果您正苦于以下问题:PHP MvcEvent::setError方法的具体用法?PHP MvcEvent::setError怎么用?PHP MvcEvent::setError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Mvc\MvcEvent
的用法示例。
在下文中一共展示了MvcEvent::setError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testOnDispatchErrorStoreAndStreamImage
/**
* @covers ::onDispatchError
*/
public function testOnDispatchErrorStoreAndStreamImage()
{
$id = 'someId';
$resource = 'someResource';
$this->event->setError(Application::ERROR_ROUTER_NO_MATCH);
$image = $this->getMockBuilder(ImageEntity::class)->setMethods(['getLength', 'getResource'])->getMock();
$image->setId($id);
$image->setType('image/jpeg');
$image->setName('image.jpg');
$image->method('getLength')->willReturn(1024);
$image->method('getResource')->willReturn($resource);
$this->manager->expects($this->once())->method('matchUri')->willReturn($id);
$this->repository->expects($this->once())->method('find')->with($this->equalTo($id))->willReturn($image);
$this->manager->expects($this->once())->method('store')->with($this->identicalTo($image));
$this->listener->onDispatchError($this->event);
$response = $this->event->getResponse();
$this->assertInstanceOf(Stream::class, $response);
$this->assertEquals(Response::STATUS_CODE_200, $response->getStatusCode());
$this->assertEquals($image->getName(), $response->getStreamName());
$this->assertEquals($image->getResource(), $response->getStream());
$headers = $response->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('Content-Type'));
$this->assertEquals($image->getType(), $headers->get('Content-Type')->getFieldValue());
$this->assertTrue($headers->has('Content-Length'));
$this->assertEquals($image->getLength(), $headers->get('Content-Length')->getFieldValue());
}
示例2: testOnRenderErrorCreatesAnApiProblemResponse
public function testOnRenderErrorCreatesAnApiProblemResponse()
{
$response = new Response();
$request = new Request();
$request->getHeaders()->addHeaderLine('Accept', 'application/json');
$event = new MvcEvent();
$event->setError(Application::ERROR_EXCEPTION);
$event->setRequest($request);
$event->setResponse($response);
$this->listener->onRenderError($event);
$this->assertTrue($event->propagationIsStopped());
$this->assertSame($response, $event->getResponse());
$this->assertEquals(406, $response->getStatusCode());
$headers = $response->getHeaders();
$this->assertTrue($headers->has('Content-Type'));
$this->assertEquals('application/problem+json', $headers->get('content-type')->getFieldValue());
$content = json_decode($response->getContent(), true);
$this->assertArrayHasKey('status', $content);
$this->assertArrayHasKey('title', $content);
$this->assertArrayHasKey('describedBy', $content);
$this->assertArrayHasKey('detail', $content);
$this->assertEquals(406, $content['status']);
$this->assertEquals('Not Acceptable', $content['title']);
$this->assertContains('www.w3.org', $content['describedBy']);
$this->assertContains('accept', $content['detail']);
}
示例3: onRoute
/**
* @param MvcEvent $e
*/
public static function onRoute(MvcEvent $e)
{
if (!$e->getRequest() instanceof HttpRequest) {
return;
}
$app = $e->getTarget();
$rbacService = $app->getServiceManager()->get('ZfcRbac\\Service\\Rbac');
$match = $app->getMvcEvent()->getRouteMatch();
$controller = $match->getParam('controller');
$action = $match->getParam('action');
$resource = sprintf('%s:%s', $controller, $action);
try {
if ($rbacService->getFirewall('controller')->isGranted($resource)) {
return;
}
} catch (InvalidArgumentException $ex) {
//if Exception, default to unauthorized
}
try {
$e->setError($rbacService::ERROR_CONTROLLER_UNAUTHORIZED)->setParam('identity', $rbacService->getIdentity())->setParam('controller', $controller)->setParam('action', $action);
$app->getEventManager()->trigger('dispatch.error', $e);
} catch (InvalidArgumentException $ex) {
return;
}
}
示例4: checkPermission
public function checkPermission(MvcEvent $e)
{
$params = $e->getRouteMatch()->getParams();
if (!isset($params['backend']) || !$params['backend']) {
return;
}
$serviceLocator = $e->getApplication()->getServiceManager();
$config = $serviceLocator->get('config');
if (Authentication::getInstance()->hasIdentity()) {
// Check if user has permission to access the current page
$user = Authentication::getInstance()->getIdentity();
/** @var \Acl\Service\Acl $acl */
$acl = $serviceLocator->get('Acl\\Service\\Acl');
if (!$acl->isAllowed($user->role, $params['controller'], $params['action'])) {
$e->setError(self::ERROR_FORBIDDEN);
$e->getApplication()->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $e);
}
} else {
$request = $e->getRequest();
/** @var \Zend\Http\Response $response */
$response = $e->getResponse();
$router = $e->getRouter();
$url = $router->assemble([], ['name' => $config['acl']['signin_route']]) . '?continue=' . $request->getUri()->toString();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
}
示例5: testOnBootstrapListenersWithHttpRequest
public function testOnBootstrapListenersWithHttpRequest()
{
$module = new Module();
$application = $this->createApplication();
$sm = $application->getServiceManager();
$sm->setService('FilterManager', new FilterPluginManager());
foreach ($module->getServiceConfig()['invokables'] as $key => $value) {
$sm->setInvokableClass($key, $value);
}
foreach ($module->getServiceConfig()['factories'] as $key => $value) {
$sm->setFactory($key, $value);
}
$sm->get('ViewHelperManager')->setService('bodyClass', $this->getMock(BodyClass::class));
$event = new MvcEvent();
$event->setApplication($application);
$em = $application->getEventManager();
$em->getSharedManager()->clearListeners(LayoutUpdater::class);
$module->onBootstrap($event);
$layoutUpdater = $sm->get(LayoutUpdaterInterface::class);
$this->assertEquals(['default'], $layoutUpdater->getHandles());
$mvcEvent = new MvcEvent();
$mvcEvent->setApplication($application);
$mvcEvent->setName(MvcEvent::EVENT_DISPATCH_ERROR);
$mvcEvent->setError('test-error');
$em->triggerEvent($mvcEvent);
$this->assertEquals(['default', 'test-error'], $layoutUpdater->getHandles());
}
示例6: terminateEvent
protected function terminateEvent(MvcEvent $event, $error, \Exception $exception)
{
$eventManager = $event->getApplication()->getEventManager();
$event->setError($error);
$event->setParam('exception', $exception);
$event->stopPropagation(true);
$eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
}
示例7: testOnError_WithApiException
public function testOnError_WithApiException()
{
$event = new MvcEvent();
$event->setError("The resource doesn't support the specified HTTP verb.");
$event->setParam('exception', new MethodNotAllowedException());
$event->setResponse(new Response());
$result = $this->testedObject->onError($event);
$this->assertInstanceOf(JsonModel::class, $result);
}
示例8: onRoute
public static function onRoute(MvcEvent $e)
{
$app = $e->getTarget();
$route = $e->getRouteMatch()->getMatchedRouteName();
$security = $app->getServiceManager()->get('SpiffySecurity\\Service\\Security');
if (!$security->getFirewall('route')->isGranted($route)) {
$e->setError($security::ERROR_ROUTE_UNAUTHORIZED)->setParam('identity', $security->getIdentity())->setParam('route', $route);
$app->getEventManager()->trigger('dispatch.error', $e);
}
}
示例9: onDispatchError
public function onDispatchError(MvcEvent $e)
{
if ($e->getError() !== Zf2Application::ERROR_ROUTER_NO_MATCH) {
return;
}
$routeMatch = new RouteMatch(array());
$routeMatch->setParam('controller', 'Zf1Module\\DispatchController');
$e->setError(null);
$e->setRouteMatch($routeMatch);
return $routeMatch;
}
示例10: onEvent
/**
* Capture Error converted to exception, then trigger a dispatch error
* @param MvcEvent $e
*/
public function onEvent(MvcEvent $event)
{
$exception = ErrorHandler::stop();
if ($exception instanceof \Exception) {
$event->setError(Application::ERROR_EXCEPTION);
$event->setParam('exception', $exception);
$this->getEventManager()->trigger($event::EVENT_DISPATCH_ERROR, $event);
} elseif (!$event->getError() && $event->getResponse()->getStatusCode() == 404) {
$event->setError(Application::ERROR_CONTROLLER_CANNOT_DISPATCH);
$this->getEventManager()->trigger($event::EVENT_DISPATCH_ERROR, $event);
}
}
示例11: onRoute
public static function onRoute(MvcEvent $e)
{
$app = $e->getTarget();
$service = $app->getServiceManager()->get('BjyAuthorize\\Service\\Authorize');
$match = $app->getMvcEvent()->getRouteMatch();
$routeName = $match->getMatchedRouteName();
$allowed = $service->isAllowed('route/' . $routeName);
if (!$allowed) {
$e->setError('error-unauthorized-route')->setParam('route', $routeName)->setParam('identity', $service->getIdentity());
$app->getEventManager()->trigger('dispatch.error', $e);
}
}
示例12: onRoute
/**
* @param MvcEvent $e
*/
public static function onRoute(MvcEvent $e)
{
if (!$e->getRequest() instanceof HttpRequest) {
return;
}
$app = $e->getTarget();
$route = $e->getRouteMatch()->getMatchedRouteName();
$rbacService = $app->getServiceManager()->get('ZfcRbac\\Service\\Rbac');
if (!$rbacService->getFirewall('route')->isGranted($route)) {
$e->setError($rbacService::ERROR_ROUTE_UNAUTHORIZED)->setParam('identity', $rbacService->getIdentity())->setParam('route', $route);
$app->getEventManager()->trigger('dispatch.error', $e);
}
}
示例13: onRoute
public static function onRoute(MvcEvent $e)
{
$app = $e->getTarget();
$service = $app->getServiceManager()->get('BjyAuthorize\\Service\\Authorize');
$match = $app->getMvcEvent()->getRouteMatch();
$controller = $match->getParam('controller');
$action = $match->getParam('action');
$controllerResource = sprintf('controller/%s', $controller);
$actionResource = sprintf('controller/%s:%s', $controller, $action);
$allowed = $service->isAllowed($controllerResource) || $service->isAllowed($actionResource);
if (!$allowed) {
$e->setError('error-unauthorized-controller')->setParam('identity', $service->getIdentity())->setParam('controller', $controller)->setParam('action', $action);
$app->getEventManager()->trigger('dispatch.error', $e);
}
}
示例14: triggerForbiddenEvent
/**
*
* @param MvcEvent $event
*/
protected function triggerForbiddenEvent(MvcEvent $event)
{
$event->setError('route-forbidden');
$event->setParam('exception', new UnauthorizedException('You are forbidden!', 403));
$event->stopPropagation(true);
if ($this->hasErrorViewModel()) {
$event->setViewModel($this->errorView);
}
$response = $event->getResponse();
$response->setStatusCode(403);
$event->setResponse($response);
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
}
示例15: onRoute
public function onRoute(MvcEvent $mvcEvent)
{
$request = $mvcEvent->getRequest();
$response = $mvcEvent->getResponse();
/**
* If it's a CLI request - return.
*/
if ($request instanceof \Zend\Console\Request) {
return;
}
$app = $mvcEvent->getApplication();
$match = $app->getMvcEvent()->getRouteMatch();
$routeName = $match->getMatchedRouteName();
$method = strtolower($request->getMethod());
if (!$this->acl->hasResource($routeName)) {
return;
}
$role = null;
/**
* Infer that if :
* 1. the request is protected under 2 legged oauth,
* then it is a 2 legged oauth request
* 2. the request is protected under 3 legged oauth,
* then it is a 3 legged oauth request
* 3. otherwise it is not protected
*/
if ($this->acl->isAllowed(BgOauthProviderAcl::TWO_LEGGED, $routeName, $method)) {
$role = BgOauthProviderAcl::TWO_LEGGED;
$this->oauthProvider->is2LeggedEndpoint();
} elseif ($this->acl->isAllowed(BgOauthProviderAcl::THREE_LEGGED, $routeName, $method)) {
$role = BgOauthProviderAcl::THREE_LEGGED;
} else {
return;
}
try {
$this->oauthProvider->checkOAuthRequest();
} catch (\OAuthException $e) {
$error = \OAuthProvider::reportProblem($e, false);
$response->setStatusCode(Response::STATUS_CODE_401);
$response->setContent($error);
$response->getHeaders()->addHeaders(array('WWW-Authenticate' => $error));
$mvcEvent->setError(self::ERROR);
$mvcEvent->getApplication()->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $mvcEvent);
return $response;
}
//Success!
}