mirror of
https://github.com/Mibew/mibew.git
synced 2025-03-03 18:38:31 +03:00
Add HTTP Exceptions classes
This commit is contained in:
parent
d7153a4bf1
commit
64e96c1c30
@ -17,19 +17,24 @@
|
||||
|
||||
namespace Mibew;
|
||||
|
||||
use Mibew\AccessControl\Check\CheckResolver;
|
||||
use Mibew\Controller\ControllerResolver;
|
||||
use Mibew\EventDispatcher;
|
||||
use Mibew\Http\Exception\AccessDeniedException as AccessDeniedHttpException;
|
||||
use Mibew\Http\Exception\BadRequestException as BadRequestHttpException;
|
||||
use Mibew\Http\Exception\HttpException;
|
||||
use Mibew\Http\Exception\MethodNotAllowedException as MethodNotAllowedHttpException;
|
||||
use Mibew\Http\Exception\NotFoundException as NotFoundHttpException;
|
||||
use Mibew\Routing\Router;
|
||||
use Mibew\Routing\RouteCollectionLoader;
|
||||
use Mibew\Routing\Exception\AccessDeniedException;
|
||||
use Mibew\Controller\ControllerResolver;
|
||||
use Mibew\AccessControl\Check\CheckResolver;
|
||||
use Mibew\Routing\Exception\AccessDeniedException as AccessDeniedRoutingException;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
||||
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\Routing\Exception\MethodNotAllowedException as MethodNotAllowedRoutingException;
|
||||
use Symfony\Component\Routing\Exception\ResourceNotFoundException as ResourceNotFoundRoutingException;
|
||||
|
||||
/**
|
||||
* Incapsulates whole application
|
||||
@ -76,26 +81,41 @@ class Application
|
||||
$this->router->setContext($context);
|
||||
|
||||
try {
|
||||
// Try to match a route and add extra data to the request.
|
||||
$parameters = $this->router->matchRequest($request);
|
||||
$request->attributes->add($parameters);
|
||||
$request->attributes->set('_operator', $this->extractOperator($request));
|
||||
// Try to match a route, check if the client can access it and add
|
||||
// extra data to the request.
|
||||
try {
|
||||
$parameters = $this->router->matchRequest($request);
|
||||
$request->attributes->add($parameters);
|
||||
$request->attributes->set('_operator', $this->extractOperator($request));
|
||||
|
||||
// Check if the user can access the page
|
||||
$access_check = $this->accessCheckResolver->getCheck($request);
|
||||
if (!call_user_func($access_check, $request)) {
|
||||
throw new AccessDeniedException();
|
||||
// Check if the user can access the page
|
||||
$access_check = $this->accessCheckResolver->getCheck($request);
|
||||
if (!call_user_func($access_check, $request)) {
|
||||
throw new RoutingAccessDeniedException();
|
||||
}
|
||||
} catch (AccessDeniedRoutingException $e) {
|
||||
// Convert the exception to HTTP exception to process it later.
|
||||
throw new AccessDeniedHttpException();
|
||||
} catch (ResourceNotFoundRoutingException $e) {
|
||||
// Convert the exception to HTTP exception to process it later.
|
||||
throw new NotFoundHttpException();
|
||||
} catch (MethodNotAllowedRoutingException $e) {
|
||||
// Convert the exception to HTTP exception to process it later.
|
||||
throw new MethodNotAllowedHttpException();
|
||||
}
|
||||
|
||||
// Get controller and perform its action to get a response.
|
||||
$controller = $this->controllerResolver->getController($request);
|
||||
$response = call_user_func($controller, $request);
|
||||
} catch (AccessDeniedException $e) {
|
||||
} catch (HttpAccessDeniedException $e) {
|
||||
return $this->buildAccessDeniedResponse($request);
|
||||
} catch (ResourceNotFoundException $e) {
|
||||
return new Response('Not Found', 404);
|
||||
} catch (MethodNotAllowedException $e) {
|
||||
return new Response('Method Not Allowed', 405);
|
||||
} catch (HttpException $e) {
|
||||
// Build response based on status code which is stored in exception
|
||||
// instance.
|
||||
$http_status = $e->getStatusCode();
|
||||
$content = Response::$statusTexts[$http_status];
|
||||
|
||||
return new Response($content, $http_status);
|
||||
} catch (\Exception $e) {
|
||||
return new Response('Internal Server Error', 500);
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Http\Exception;
|
||||
|
||||
/**
|
||||
* Access to the resource is forbidden.
|
||||
*
|
||||
* This exception should trigger an HTTP 403 response.
|
||||
*/
|
||||
class AccessDeniedException extends HttpException
|
||||
{
|
||||
public function __construct($message = null, $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct(403, $message, $previous, $code);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Http\Exception;
|
||||
|
||||
/**
|
||||
* The request cannot be fulfilled due to bad syntax.
|
||||
*
|
||||
* This exception should trigger an HTTP 400 response.
|
||||
*/
|
||||
class BadRequestException extends HttpException
|
||||
{
|
||||
public function __construct($message = null, $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct(400, $message, $previous, $code);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Http\Exception;
|
||||
|
||||
/**
|
||||
* A base exception to all HTTP exceptions.
|
||||
*
|
||||
* Please look at {@link \Mibew\Http\Exception\HttpException\::__construct()}
|
||||
* arguments list before create instances of the class.
|
||||
*/
|
||||
class HttpException extends \RuntimeException
|
||||
{
|
||||
/**
|
||||
* Holds HTTP status code related with the exception.
|
||||
* @var int
|
||||
*/
|
||||
private $statusCode;
|
||||
|
||||
/**
|
||||
* Class contructor.
|
||||
*
|
||||
* @param int $status_code HTTP status code related with the exception.
|
||||
* @param string $message The Exception message to throw.
|
||||
* @param \Exception $previous The previous exception used for the exception
|
||||
* chaining.
|
||||
* @param int $code The Exception code.
|
||||
*/
|
||||
public function __construct($status_code, $message = null, \Exception $previous = null, $code = 0)
|
||||
{
|
||||
$this->statusCode = $status_code;
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns HTTP status code related with the exception.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getStatusCode()
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Http\Exception;
|
||||
|
||||
/**
|
||||
* The resource was found but the request method is not allowed.
|
||||
*
|
||||
* This exception should trigger an HTTP 405 response.
|
||||
*/
|
||||
class MethodNotAllowedException extends HttpException
|
||||
{
|
||||
public function __construct($message = null, $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct(405, $message, $previous, $code);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Http\Exception;
|
||||
|
||||
/**
|
||||
* The resource is not found.
|
||||
*
|
||||
* This exception should trigger an HTTP 404 response.
|
||||
*/
|
||||
class NotFoundException extends HttpException
|
||||
{
|
||||
public function __construct($message = null, $code = 0, \Exception $previous = null)
|
||||
{
|
||||
parent::__construct(404, $message, $previous, $code);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user