• Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple PHP library to improve verbosity in HTTP response codes

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 [email protected] or ping me on Twitter with @shrikeh.

More Repositories

1

csrf-nginx-redis-lua

A simple nginx conf file to allow your backend (Varnish, Apache Traffic Server, etc) to not worry about CSRF tokens and put the onus on the front (nginx) instance
39
star
2

ant-phptools

A Composer-friendly port of the PEAR-based Template for Jenkins Jobs for PHP Projects (http://jenkins-php.org)
19
star
3

ansible-github-deploy-key

Role to automatically change your deploy key, therefore ensuring a fresh key every time. For the security-minded, or those who just want to totally forget about the need to setup deploy keys.
7
star
4

spdy-sandwich

Interim version of the SPDY sandwich for testing - don't use in prod!
5
star
5

guzzle-middleware-responsetimer

Middleware for logging response times for calls to a PSR-3 compatible logger
PHP
3
star
6

Sphere

A PHP library, written for Zend Framework integration, to represent unique contextual IDs using matrices, with hierarchical mapping and inheritance.
PHP
2
star
7

ansible-my-mac

Shell
1
star
8

Macaroons

PHP port of the excellent libmacaroons library (https://github.com/rescrv/libmacaroons)
PHP
1
star
9

simple-symfony-skeleton

Skeleton Symfony application for technical tests etc
PHP
1
star
10

ansible-mktemp

Plugin to allow creation of a temporary file or directory
Python
1
star
11

php-coding-bible

Personal coding bible, derived from previous incarnations created at various companies
PHP
1
star
12

itech-media-technical-test

PHP
1
star
13

inviqa-provisioning

Just a little test script to provision Macs and Unix laptops via Ansible
Shell
1
star
14

command-and-emission-control-demo

PHP
1
star
15

php-immutable-collections

Trait-based helper library to take care of the heavy lifting of immutable collections
PHP
1
star
16

bounce

PHP Micro component for event dispatch
PHP
1
star
17

symfony-default-kermel

A testable, environment-driven variant of the default generated kernel created by Symfony.
PHP
1
star
18

work-life-balance

A PHP library to help remind PHP developers on what matters
PHP
1
star