Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpKernel] refactored HTTP exceptions to be more flexible
- Loading branch information
1 parent
b2f5ac8
commit 10dc18b
Showing
5 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/HttpKernel/Exception/BaseHttpException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Exception; | ||
|
||
/** | ||
* BaseHttpException. | ||
* | ||
* @author Kris Wallsmith <kris@symfony.com> | ||
*/ | ||
abstract class BaseHttpException extends \RuntimeException implements HttpExceptionInterface | ||
{ | ||
protected $statusCode; | ||
protected $statusMessage; | ||
protected $headers; | ||
|
||
public function __construct($statusCode, $statusMessage, array $headers = array(), $message = null, $code = 0, \Exception $previous = null) | ||
{ | ||
$this->statusCode = $statusCode; | ||
$this->statusMessage = $statusMessage; | ||
$this->headers = $headers; | ||
|
||
parent::__construct($message, 0, $previous); | ||
} | ||
|
||
public function getStatusCode() | ||
{ | ||
return $this->statusCode; | ||
} | ||
|
||
public function getStatusMessage() | ||
{ | ||
return $this->statusMessage; | ||
} | ||
|
||
public function getHeaders() | ||
{ | ||
return $this->headers; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/HttpKernel/Exception/HttpExceptionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Exception; | ||
|
||
/** | ||
* Interface for HTTP error exceptions. | ||
* | ||
* @author Kris Wallsmith <kris@symfony.com> | ||
*/ | ||
interface HttpExceptionInterface | ||
{ | ||
/** | ||
* Returns the status code. | ||
* | ||
* @return integer An HTTP response status code | ||
*/ | ||
function getStatusCode(); | ||
|
||
/** | ||
* Return the status message. | ||
* | ||
* @return string An HTTP response status message | ||
*/ | ||
function getStatusMessage(); | ||
|
||
/** | ||
* Returns response headers. | ||
* | ||
* @return array Response headers | ||
*/ | ||
function getHeaders(); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/HttpKernel/Exception/MethodNotAllowedHttpException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Exception; | ||
|
||
/** | ||
* MethodNotAllowedHttpException. | ||
* | ||
* @author Kris Wallsmith <kris@symfony.com> | ||
*/ | ||
class MethodNotAllowedHttpException extends BaseHttpException | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* WARNING: The status message will be sent as a response header | ||
* regardless of debug mode. | ||
* | ||
* @param array $allow An array of allowed methods | ||
* @param string $statusMessage The HTTP response status message | ||
* @param string $message The internal exception message | ||
* @param integer $code The internal exception code | ||
* @param Exception $previous The previous exception | ||
*/ | ||
public function __construct(array $allow, $statusMessage = 'Method Not Allowed', $message = null, $code = 0, \Exception $previous = null) | ||
{ | ||
$headers = array('Allow' => strtoupper(implode(', ', $allow))); | ||
|
||
parent::__construct(405, $statusMessage, $headers, $message ?: $statusMessage, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters