2017 © Pedro Peláez
 

library psx

PHP REST API Framework

image

psx/psx

PHP REST API Framework

  • Sunday, March 4, 2018
  • by k42b3
  • Repository
  • 10 Watchers
  • 87 Stars
  • 11,201 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 61 Versions
  • 0 % Grown

The README.md

About

PSX is an innovative PHP framework dedicated to build fully typed REST APIs., (*1)

It helps to improve the API development process by providing the following features:, (*2)

  • Fully typed controller classes
  • Client SDK generator
  • OpenAPI generator
  • Generate model classes based on a TypeSchema specification
  • Uses the Symfony DI container component
  • Works with Doctrine DBAL and migrations
  • Type-safe database interaction
  • Endpoint integration testing

More information about PSX at phpsx.org., (*3)

Installation

To install the framework you can simply install this demo API project., (*4)

composer create-project psx/psx .

Getting started

This repository contains already a fully working demo API build with PSX which you can use as a starting point and to better understand how PSX works. In the following we go based on the demo files through the important concepts of PSX., (*5)

Controller

A controller is the entrypoint of your app which gets invoked by the framework. A controller is a simple PHP class which contains attributes to make specific methods invokable. In the following example we have a simple controller with a getAll and create method which gets invoked if a GET or POST request arrives at the /population endpoint s., (*6)

class Population extends ControllerAbstract
{
    #[Get]
    #[Path('/population')]
    public function getAll(#[Query] ?int $startIndex = null, #[Query] ?int $count = null): Model\PopulationCollection
    {
        return $this->populationTable->getCollection($startIndex, $count);
    }

    #[Post]
    #[Path('/population')]
    public function create(#[Body] Model\Population $payload): Model\Message
    {
        $id = $this->populationService->create($payload);

        $message = new Model\Message();
        $message->setSuccess(true);
        $message->setMessage('Population record successfully created');
        $message->setId($id);
        return $message;
    }
}

One key concept of PSX is that the arguments of your exposed controller methods are mapped to values of the incoming HTTP request, at the getAll method the $startIndex and $count parameter are mapped to a query parameter from the HTTP request, at the create method the $payload parameter is mapped to the request body. If you are familiar with Spring or NestJS you already know this approach., (*7)

PSX uses the symfony DI container, all controller classes are automatically loaded through auto-wiring. This means you can simply define at the constructor all dependencies which are needed for your controller. Please take a look at the container.php if you want to customize which classes are loaded., (*8)

SDK

One of the greatest feature of PSX is that it can automatically generate a client SDK for the API which you have build. To generate the client SDK simply run the following command., (*9)

php bin/psx generate:sdk

This writes the SDK to the output/ folder. By default, the command generates the TypeScript SDK. Based on the controller defined above PSX would generate the following client SDK., (*10)

const client = new Client(...);

client.population().getAll(startIndex?: number, count?: number);
client.population().create(payload: Population);

The client then contains the same schemas which are also defined at the backend but converted to TypeScript. This means you are using exactly the same schema at the backend and frontend. If you change your schema at the backend you can then regenerate the SDK and you will directly see all problems with your new schema. In this sense PSX provides similar features like tRPC but in a language neutral way., (*11)

The generate:sdk command accepts as argument a format which defines the type of SDK which is generated. The following list shows some supported formats., (*12)

  • client-php
  • client-typescript
  • spec-openapi

Model

To enable this SDK generation PSX needs to understand the structure of the incoming or outgoing JSON payload. This is done by using DTO models for every argument and return type. PSX contains a model generator which allows you to generate those models based on a TypeSchema specification. Please take a look at the typeschema.json file which contains the models for our demo API. You can generate all models using the following command s., (*13)

php bin/psx generate:model

The command writes all models to the src/Model folder. You can then use those models at the controller classes., (*14)

Service

PSX recommends to move your actual business logic into a separate service class. The controller then simply invokes methods from your service. While this is not mandatory it improves your code quality since you can easily use this service also in another context. All classes under the service/ folder are automatically loaded thus you can specify all dependencies through simple constructor injection., (*15)

Migrations

PSX uses doctrine migrations which helps to manage your database schema. To generate a new migration you can simply run s., (*16)

php bin/psx migrations:generate

This would create a new migration file at src/Migrations. You can then model your table schema at this migration file. After this you can run the migrate command to execute all needed database changes s., (*17)

php bin/psx migrations:migrate

Please take a look at the doctrine migrations project for more information how the migration system works., (*18)

Table

PSX provides a command which generates table and row classes to interact with your database in a type-safe way. This command should be executed after you have executed all your migrations., (*19)

php bin/psx generate:table

This command then writes all files to the src/Table folder., (*20)

Note in general we think that for API development an ORM is not needed, but it would be easy possible to integrate any existing ORM into PSX., (*21)

Tests

PSX provides a way to easily build an integration test for every controller endpoint. The following extract shows the test which requests the /population endpoint and simply compares the JSON payload with an existing JSON structure., (*22)

public function testGetAll(): void
{
    $response = $this->sendRequest('/population', 'GET');

    $actual = (string) $response->getBody();
    $expect = file_get_contents(__DIR__ . '/resources/collection.json');

    $this->assertEquals(200, $response->getStatusCode(), $actual);
    $this->assertJsonStringEqualsJsonString($expect, $actual, $actual);
}

Through this you can easily build integration tests for every endpoint. Please take a look at the tests/Controller/PopulationTest.php file to see the complete test case., (*23)

Components

Besides the framework PSX is build on various PHP components. These components are independent of the framework and can also be used in another context. The following list contains the most notable packages:, (*24)

  • psx/api
    Parse and generate API specification formats
  • psx/schema
    Parse and generate data schema formats
  • psx/data
    Data processing library to read and write POPOs in different formats
  • psx/sql
    Generate type-safe PHP classes from your database

The Versions

04/03 2018

dev-master

9999999-dev http://phpsx.org

PHP REST API Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

31/12 2017

v4.0.0

4.0.0.0 http://phpsx.org

PHP REST API Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

07/12 2017

v3.0.3

3.0.3.0 http://phpsx.org

PHP REST API Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

09/08 2017

v3.0.2

3.0.2.0 http://phpsx.org

PHP REST API Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

15/04 2017

v3.0.1

3.0.1.0 http://phpsx.org

PHP REST API Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

24/12 2016

v3.0.0

3.0.0.0 http://phpsx.org

PHP REST API Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

11/09 2016

v2.0.1

2.0.1.0 http://phpsx.org

PHP REST Api Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

08/05 2016

v2.0.0

2.0.0.0 http://phpsx.org

PHP REST Api Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

29/04 2016

v2.0.0-RC1

2.0.0.0-RC1 http://phpsx.org

PHP REST Api Framework

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

api framework rest

13/11 2013

v0.7.3

0.7.3.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

27/10 2013

v0.7.2

0.7.2.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

26/10 2013

v0.7.1

0.7.1.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

28/09 2013

v0.7.0

0.7.0.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

22/09 2013

v0.6.4

0.6.4.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

13/09 2013

v0.6.3

0.6.3.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

14/07 2013

v0.6.2

0.6.2.0 http://phpsx.org

PSX Framework

  Sources   Download

GPL-3.0

The Requires

  • php >=5.3

 

28/06 2013

v0.6.1

0.6.1.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

22/06 2013

v0.6.0

0.6.0.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

30/04 2013

v0.5.4

0.5.4.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

29/04 2013

v0.5.3

0.5.3.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

22/04 2013

v0.5.2

0.5.2.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

21/04 2013

v0.5.1

0.5.1.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

24/03 2013

v0.5.0

0.5.0.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3

 

09/12 2012

v0.4.7

0.4.7.0 http://phpsx.org

PSX Framework

  Sources   Download

GPLv3

The Requires

  • php >=5.3