Skip to content

Commit

Permalink
Close #82
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jan 26, 2019
1 parent 716ad52 commit a20a16e
Show file tree
Hide file tree
Showing 207 changed files with 11,287 additions and 12,359 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
build/
.idea/
composer.lock
perf/blackfire.io.env
4 changes: 2 additions & 2 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Framework agnostic JSON API implementation

Copyright 2015 info@neomerx.com
Copyright 2015-2019 info@neomerx.com

This product includes software developed at Neomerx (www.neomerx.com).
This product includes software developed at Neomerx (www.neomerx.com).
69 changes: 46 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[![Project Management](https://img.shields.io/badge/project-management-blue.svg)](https://waffle.io/neomerx/json-api)
[![Build Status](https://travis-ci.org/neomerx/json-api.svg?branch=master)](https://travis-ci.org/neomerx/json-api)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/neomerx/json-api/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/neomerx/json-api/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/neomerx/json-api/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/neomerx/json-api/?branch=master)
[![Build Status](https://travis-ci.org/neomerx/json-api.svg?branch=master)](https://travis-ci.org/neomerx/json-api)
[![License](https://img.shields.io/packagist/l/neomerx/json-api.svg)](https://packagist.org/packages/neomerx/json-api)

## Description
Expand All @@ -10,18 +9,19 @@

A good API is one of most effective ways to improve the experience for your clients. Standardized approaches for data formats and communication protocols increase productivity and make integration between applications smooth.

This framework agnostic package implements [JSON API](http://jsonapi.org/) specification **version v1.0** and helps focusing on core application functionality rather than on protocol implementation. It supports document structure, errors, data fetching as described in [JSON API Format](http://jsonapi.org/format/) and covers parsing and checking HTTP request parameters and headers. For instance it helps to correctly respond with ```Unsupported Media Type``` (HTTP code 415) and ```Not Acceptable``` (HTTP code 406) to invalid requests. You don't need to manually validate all input parameters on every request. You can configure what parameters are supported by your services and this package will check incoming requests automatically. It greatly simplifies API development and fully support specification. In particular
This framework agnostic package implements [JSON API](http://jsonapi.org/) specification **version v1.1** and helps focusing on core application functionality rather than on protocol implementation. It supports document structure, errors, data fetching as described in [JSON API Format](http://jsonapi.org/format/) and covers parsing and checking HTTP request parameters and headers. For instance it helps to correctly respond with ```Unsupported Media Type``` (HTTP code 415) and ```Not Acceptable``` (HTTP code 406) to invalid requests. You don't need to manually validate all input parameters on every request. You can configure what parameters are supported by your services and this package will check incoming requests automatically. It greatly simplifies API development and fully support specification. In particular

* Resource attributes and relationships
* Polymorphic resource data and relationships
* Compound documents with inclusion of related resources (circular resource references supported)
* Meta information for document, resources, errors, relationship and link objects
* Profiles
* Parsing HTTP `Accept` and `Content-Type` headers in accordance with [RFC 7231](https://tools.ietf.org/html/rfc7231)
* Parsing HTTP query parameters (e.g. pagination, sorting and etc)
* Sparse fieldsets and customized included paths
* Errors

High code quality and **100% test coverage** with **200+ tests**. Production ready.
High code quality and **100% test coverage** with **150+ tests**. Production ready.

**To find out more, please check out the [Wiki](https://github.com/neomerx/json-api/wiki) and [Sample App](/sample)**.

Expand Down Expand Up @@ -50,8 +50,10 @@ Assuming you've got an ```$author``` of type ```\Author``` you can encode it to

```php
$encoder = Encoder::instance([
'\Author' => '\AuthorSchema',
], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1'));
Author::class => AuthorSchema::class,
])
->withUrlPrefix('http://example.com/api/v1')
->withEncodeOptions(JSON_PRETTY_PRINT);

echo $encoder->encodeData($author) . PHP_EOL;
```
Expand All @@ -60,15 +62,22 @@ will output

```json
{
"data": {
"type": "people",
"id": "123",
"attributes": {
"first_name": "John",
"last_name": "Dow"
"data" : {
"type" : "people",
"id" : "123",
"attributes" : {
"first-name": "John",
"last-name": "Doe"
},
"relationships" : {
"comments" : {
"links": {
"related" : "http://example.com/api/v1/people/123/comments"
}
}
},
"links": {
"self": "http://example.com/api/v1/people/123"
"links" : {
"self" : "http://example.com/api/v1/people/123"
}
}
}
Expand All @@ -79,36 +88,50 @@ The ```AuthorSchema``` provides information about resource's attributes and migh
```php
class AuthorSchema extends BaseSchema
{
protected $resourceType = 'people';
public function getType(): string
{
return 'people';
}

public function getId($author): ?string
{
/** @var Author $author */
return $author->authorId;
}

public function getAttributes($author, array $fieldKeysFilter = null): ?array
public function getAttributes($author): iterable
{
/** @var Author $author */
return [
'first_name' => $author->firstName,
'last_name' => $author->lastName,
'first-name' => $author->firstName,
'last-name' => $author->lastName,
];
}

public function getRelationships($author): iterable
{
return [
'comments' => [
self::RELATIONSHIP_LINKS_SELF => false,
self::RELATIONSHIP_LINKS_RELATED => true,

// Data include supported as other cool features
// self::RELATIONSHIP_DATA => $author->comments,
],
];
}
}
```

The first ```EncoderOptions``` parameter ```JSON_PRETTY_PRINT``` is a PHP predefined [JSON constant](http://php.net/manual/en/json.constants.php).
Parameter ```http://example.com/api/v1``` is a URL prefix that will be applied to all encoded links unless they have a flag set telling not to add any prefixes.

The second ```EncoderOptions``` parameter ```http://example.com/api/v1``` is a URL prefix that will be applied to all encoded links unless they have ```$treatAsHref``` flag set to ```true```.
Parameter ```JSON_PRETTY_PRINT``` is a PHP predefined [JSON constant](http://php.net/manual/en/json.constants.php).

A sample program with encoding of multiple, nested, filtered objects and more is [here](sample).

**For more advanced usage please check out the [Wiki](https://github.com/neomerx/json-api/wiki)**.

## Versions

Current version is 2.x (PHP 7.1+) for older PHP (PHP 5.5 - 7.0, HHVM) please use version 1.x.
Current version is 3.x (PHP 7.1+) for older PHP (PHP 5.5 - 7.0, HHVM) please use version 1.x.

## Questions?

Expand Down
12 changes: 4 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,21 @@
}
],
"require": {
"php": ">=7.1.0",
"psr/log": "^1.0"
"php": ">=7.1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"mockery/mockery": "^1.0",
"scrutinizer/ocular": "^1.4",
"squizlabs/php_codesniffer": "^2.9",
"monolog/monolog": "^1.23",
"phpmd/phpmd": "^2.6"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Neomerx\\JsonApi\\": "src/"
},
"files": ["src/I18n/translate.php"]
"files": ["src/I18n/format.php"]
},
"autoload-dev": {
"psr-4": {
Expand All @@ -48,10 +46,8 @@
"scripts": {
"test": ["@test-unit", "@test-cs", "@test-md"],
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
"test-unit-with-coverage": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"test-unit-phpdbg": "phpdbg -qrr ./vendor/bin/phpunit --coverage-text",
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",

"perf-test": "docker-compose run --rm cli_7_1_php php /app/sample/sample.php -t=10000"
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming"
}
}
46 changes: 0 additions & 46 deletions docker-compose.yml

This file was deleted.

13 changes: 13 additions & 0 deletions perf/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM php:7.3-cli

ARG DEBIAN_FRONTEND=noninteractive

RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
&& mkdir -p /tmp/blackfire \
&& curl -A "Docker" -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version | tar zxp -C /tmp/blackfire \
&& curl -A "Docker" -L -s https://blackfire.io/api/v1/releases/client/linux_static/amd64 | tar zxp -C /tmp/blackfire \
&& mv /tmp/blackfire/blackfire /usr/bin/blackfire \
&& mv /tmp/blackfire/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so \
&& printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > /usr/local/etc/php/conf.d/blackfire.ini \
&& rm -Rf /tmp/blackfire \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
7 changes: 7 additions & 0 deletions perf/blackfire.io.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Credential should be taken from https://blackfire.io/my/settings/credentials
#
BLACKFIRE_CLIENT_ID=<put Client ID here>
BLACKFIRE_CLIENT_TOKEN=<put Client Token here>
BLACKFIRE_SERVER_ID=<put Server ID here>
BLACKFIRE_SERVER_TOKEN=<put Server Token here>
15 changes: 15 additions & 0 deletions perf/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.7'

services:
cli_php:
build:
context: ./
dockerfile: Dockerfile
container_name: cli_php_json_api_blackfire
env_file: blackfire.io.env
volumes:
- type: bind
source: ./../
target: /app
working_dir: /app
tty: true
28 changes: 28 additions & 0 deletions perf/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Overview

This is performance test suite for the library that uses [Blackfire](https://blackfire.io) _Performance Management Solution_.

## Prerequisites
- Install [docker](https://docs.docker.com/install/#supported-platforms);
- Install [docker-compose](https://docs.docker.com/compose/install/).

## Installation

- Copy `blackfire.io.env.sample` to `blackfire.io.env`;
- Put your Client ID, Client Token, Server ID and Server Token to `blackfire.io.env` from [Blackfire.io credentials page](https://blackfire.io/my/settings/credentials) (registration needed).

## Profile Performance

```bash
$ docker-compose run --rm cli_php blackfire run php -d zend.assertions=-1 /app/sample/sample.php -t=100
```

The output will contain basic performance info and a URL with detailed profiling info [such as this one](https://blackfire.io/profiles/207fb294-d851-48ad-a31c-db29478172e3/graph).

> Note: The **first** run will download necessary docker images which takes some time. The subsequent runs will not require such downloads and be faster.
The created container can be removed from the local machine with

```bash
$ docker rmi perf_cli_php
```
Loading

0 comments on commit a20a16e

Please sign in to comment.