teapot

Simple PHP library to improve verbosity in HTTP response codes

  • 所有者: shrikeh/teapot
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Teapot

build_status_img
code_quality_img
latest_stable_version_img
latest_unstable_version_img
license_img
twitter_img

This is a very simple library that aims to aid verbosity in any Web-based application by defining clearly the HTTP 1.1 response codes as constants. It includes two main components: an interface, which contains the constants, and an exception specifically for HTTP.

Usage

Using the StatusCodes interface

Assuming for a moment a PHPUnit test on a cURL client response:

<?php

/**
 * @dataProvider someUrlProvider
 */
public function testResponseIsOK($url)
{
    $client = new Client($url);
    $response = $client->get();

    $this->assertSame(200, $response->getStatusCode());
}

This becomes:

<?php

use Teapot\StatusCode;
...
$this->assertSame(StatusCode::OK, $response->getStatusCode());

While this is a trivial example, the additional verbosity of the code is clearer with other HTTP status codes:

<?php

use Teapot\StatusCode;

$code = $response->getStatusCode();

$this->assertNotEquals(StatusCode::NOT_FOUND, $code);
$this->assertNotEquals(StatusCode::FORBIDDEN, $code);
$this->assertNotEquals(StatusCode::MOVED_PERMANENTLY, $code);
$this->assertSame(StatusCode::CREATED, $code);

As StatusCode is an interface without any methods, you can directly implement it if you prefer:

<?php

use Teapot\StatusCode;

class FooController implements StatusCode
{
    public function badAction()
    {
        if ($this->request->getMethod() == 'POST') {
            throw new \Exception('Bad!', self::METHOD_NOT_ALLOWED);
        }
    }
}

This may be beneficial in an abstract class, so that child classes don't need to explicitly use the interface.

There are various "helper" interfaces within the library, such as WebDAV and Http. Additionally, the various status codes are split into the RFCs that defined them: the Http helper interface extends RFCs 2616, 2324, and 2774, for example. This allows you very granular control of what status codes you want to allow within your application.

All constants have doc blocks that use the official W3C and IETF draft specification descriptions of the status code, to aid IDEs and for reference.

Using the HttpException

The HttpException is very straightforward. It simply is a named exception to aid verbosity:

<?php

use Teapot\HttpException;
use Teapot\StatusCode;

throw new HttpException(
    'Sorry this page does not exist!',
    StatusCode::NOT_FOUND
);

The exception itself uses the StatusCode interface, allowing you to avoid manually and explicitly importing it if you prefer:

<?php

use Teapot\HttpException;

throw new HttpException(
    'Sorry this page does not exist!',
    HttpException::NOT_FOUND
);

Installation

Run the following command.

composer require shrikeh/teapot

Coding Standards

The entire library is intended to be PSR-1, PSR-2 and
PSR-4 compliant.

Get in touch

If you have any suggestions, feel free to email me at barney+teapot@shrikeh.net or ping me on Twitter with @shrikeh.

主要指標

概覽
名稱與所有者shrikeh/teapot
主編程語言PHP
編程語言PHP (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2013-03-26 19:28:10
推送於2024-10-07 12:25:34
最后一次提交2023-09-08 12:52:13
發布數12
最新版本名稱3.0.0 (發布於 )
第一版名稱0.1 (發布於 2013-04-02 11:40:13)
用户参与
星數276
關注者數10
派生數16
提交數263
已啟用問題?
問題數10
打開的問題數1
拉請求數41
打開的拉請求數0
關閉的拉請求數4
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?