8000 Initial commit. · sablesoft/laravel-json-api@770f0e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 770f0e7

Browse files
committed
Initial commit.
0 parents  commit 770f0e7

File tree

12 files changed

+619
-0
lines changed

12 files changed

+619
-0
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.php]
11+
indent_size = 4
12+
13+
[*.md]
14+
indent_size = 2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
vendor/
3+
composer.lock

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "cloudcreativity/laravel-json-api",
3+
"description": "JSON API (jsonapi.org) support for Laravel applications.",
4+
"keywords": [
5+
"jsonapi.org",
6+
"json-api",
7+
"jsonapi",
8+
"cloudcreativity",
9+
"json",
10+
"api",
11+
"laravel"
12+
],
13+
"homepage": "https://github.com/cloudcreativity/laravel-json-api",
14+
"support": {
15+
"issues": "https://github.com/cloudcreativity/laravel-json-api/issues"
16+
},
17+
"license": "Apache-2.0",
18+
"authors": [
19+
{
20+
"name": "Cloud Creativity Ltd",
21+
"email": "info@cloudcreativity.co.uk"
22+
}
23+
],
24+
"require": {
25+
"php": ">=5.5.0",
26+
"neomerx/json-api": "^0.5.11",
27+
"illuminate/support": "^5.0",
28+
"cloudcreativity/json-api": "dev-feature-config",
29+
"illuminate/routing": "^5.0"
30+
},
31+
"require-dev": {
32+
"phpunit/phpunit": "^4.7"
33+
},
34+
"minimum-stability": "stable",
35+
"autoload": {
36+
"psr-4": {
37+
"CloudCreativity\\JsonApi\\": "src/"
38+
}
39+
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"CloudCreativity\\JsonApi\\": "test/"
43+
}
44+
},
45+
"config": {
46+
"bin-dir": "bin"
47+
}
48+
}

phpunit.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true">
4+
<testsuites>
5+
<testsuite name="JSON API Test Suite">
6+
<directory>./test/</directory>
7+
</testsuite>
8+
</testsuites>
9+
</phpunit>

src/Config/Config.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CloudCreativity\JsonApi\Config;
4+
5+
/**
6+
* Class Config
7+
* @package CloudCreativity\JsonApi
8+
*/
9+
class Config
10+
{
11+
12+
/** The config file name. */
13+
const KEY = 'json-api';
14+
15+
/** If a true boolean, Json Api support will be initialised for the entire application. */
16+
const IS_GLOBAL = 'is-global';
17+
18+
/** The key for schema definitions */
19+
const SCHEMAS = 'schemas';
20+
21+
/** The key for encoder options */
22+
const ENCODER_OPTIONS = 'encoder-options';
23+
24+
/** The ket for the codec matcher configuration */
25+
const CODEC_MATCHER = 'codec-matcher';
26+
27+
/** The key for Exception Render Container config */
28+
const EXCEPTION_RENDER_CONTAINER = 'exception-render-container';
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace CloudCreativity\JsonApi\Http\Middleware;
4+
5+
use CloudCreativity\JsonApi\Contracts\Config\CodecMatcherRepositoryInterface;
6+
use Illuminate\Contracts\Foundation\Application;
7+
use Illuminate\Http\Request;
8+
use Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface;
9+
10+
class InitCodecMatcher
11+
{
12+
13+
/**
14+
* @var Application
15+
*/
16+
private $_application;
17+
18+
/**
19+
* @param Application $application
20+
*/
21+
public function __construct(Application $application)
22+
{
23+
$this->_application = $application;
24+
}
25+
26+
/**
27+
* @param Request $request
28+
* @param \Closure $next
29+
* @return mixed
30+
*/
31+
public function handle(Request $request, \Closure $next, $codecMatcherName = null)
32+
{
33+
/** @var CodecMatcherRepositoryInterface $repository */
34+
$repository = $this->_application->make(CodecMatcherRepositoryInterface::class);
35+
$codecMatcher = $repository->get($codecMatcherName ?: CodecMatcherRepositoryInterface::DEFAULTS);
36+
$this->_application->instance(CodecMatcherInterface::class, $codecMatcher);
37+
38+
return $next($request);
39+
}
40+
}

src/Http/Middleware/Middleware.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace CloudCreativity\JsonApi\Http\Middleware;
4+
5+
/**
6+
* Class Middleware
7+
* @package CloudCreativity\JsonApi
8+
*/
9+
class Middleware
10+
{
11+
12+
/** Middleware that boots Json Api support */
13+
const JSON_API = 'json-api';
14+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace CloudCreativity\JsonApi\Integration;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Response;
7+
use Neomerx\JsonApi\Contracts\Integration\CurrentRequestInterface;
8+
use Neomerx\JsonApi\Contracts\Integration\NativeResponsesInterface;
9+
10+
class LaravelIntegration implements CurrentRequestInterface, NativeResponsesInterface
11+
{
12+
13+
/**
14+
* @var Request
15+
*/
16+
private $_request;
17+
18+
/**
19+
* @param Request $request
20+
*/
21+
public function __construct(Request $request)
22+
{
23+
$this->_request = $request;
24+
}
25+
26+
/**
27+
* @return Request
28+
*/
29+
public function getRequest()
30+
{
31+
return $this->_request;
32+
}
33+
34+
/**
35+
* Get content.
36+
* @return string|null
37+
*/
38+
public function getContent()
39+
{
40+
$content = $this
41+
->getRequest()
42+
->getContent();
43+
44+
return !empty($content) ? $content : null;
45+
}
46+
47+
/**
48+
* Get inputs.
49+
* @return array
50+
*/
51+
public function getQueryParameters()
52+
{
53+
return $this
54+
->getRequest()
55+
->query();
56+
}
57+
58+
/**
59+
* Get header value.
60+
*
61+
* @param string $name
62+
* @return string|null
63+
*/
64+
public function getHeader($name)
65+
{
66+
return $this
67+
->getRequest()
68+
->header($name, null);
69+
}
70+
71+
/**
72+
* Create HTTP response.
73+
*
74+
* @param string|null $content
75+
* @param int $statusCode
76+
* @param array $headers
77+
*
78+
* @return mixed
79+
*/
80+
public function createResponse($content, $statusCode, array $headers)
81+
{
82+
return new Response($content, $statusCode, $headers);
83+
}
84+
}

src/JsonApiProvider.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace CloudCreativity\JsonApi;
4+
5+
use CloudCreativity\JsonApi\Config\CodecMatcherRepository;
6+
use CloudCreativity\JsonApi\Config\EncoderOptionsRepository;
7+
use CloudCreativity\JsonApi\Config\EncodersRepository;
8+
use CloudCreativity\JsonApi\Config\SchemasRepository;
9+
use CloudCreativity\JsonApi\Contracts\Config\CodecMatcherRepositoryInterface;
10+
use CloudCreativity\JsonApi\Contracts\Config\EncoderOptionsRepositoryInterface;
11+
use CloudCreativity\JsonApi\Contracts\Config\EncodersRepositoryInterface;
12+
use CloudCreativity\JsonApi\Contracts\Config\SchemasRepositoryInterface;
13+
use CloudCreativity\JsonApi\Exceptions\RenderContainer;
14+
use CloudCreativity\JsonApi\Integration\LaravelIntegration;
15+
use CloudCreativity\JsonApi\Error\ExceptionThrower;
16+
use CloudCreativity\JsonApi\Config\Config as C;
17+
use CloudCreativity\JsonApi\Http\Middleware\Middleware as M;
18+
use CloudCreativity\JsonApi\Http\Middleware\InitCodecMatcher;
19+
use Illuminate\Contracts\Config\Repository;
20+
use Illuminate\Contracts\Container\Container;
21+
use Illuminate\Contracts\Http\Kernel;
22+
use Illuminate\Routing\Router;
23+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
24+
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
25+
use Neomerx\JsonApi\Factories\Factory;
26+
use Neomerx\JsonApi\Contracts\Integration\CurrentRequestInterface;
27+
use Neomerx\JsonApi\Contracts\Integration\NativeResponsesInterface;
28+
use Neomerx\JsonApi\Contracts\Responses\ResponsesInterface;
29+
use Neomerx\JsonApi\Contracts\Integration\ExceptionThrowerInterface;
30+
use Neomerx\JsonApi\Contracts\Exceptions\RenderContainerInterface;
31+
use Neomerx\JsonApi\Responses\Responses;
32+
33+
/**
34+
* Class ServiceProvider
35+
* @package CloudCreativity\JsonApi
36+
*/
37+
class ServiceProvider extends BaseServiceProvider
38+
{
39+
40+
/**
41+
* @var bool
42+
*/
43+
protected $defer = true;
44+
45+
/**
46+
* @param Router $router
47+
* @param Repository $repository
48+
* @param Kernel $kernel
49+
*/
50+
public function boot(Router $router, Repository $repository, Kernel $kernel)
51+
{
52+
// Add Json Api middleware to the router.
53+
$router->middleware(M::JSON_API, InitCodecMatcher::class);
54+
55+
// If the whole application is set to be a Json Api, push the init middleware into the kernel.
56+
$key = sprintf('%s.%s', C::KEY, C::IS_GLOBAL);
57+
$global = $repository->get($key, false);
58+
59+
if (true === $global && method_exists($kernel, 'pushMiddleware')) {
60+
$kernel->pushMiddleware(InitCodecMatcher::class);
61+
}
62+
}
63+
64+
/**
65+
* Register JSON API services.
66+
*
67+
* @return void
68+
*/
69+
public function register()
70+
{
71+
// Factory
72+
$this->app->singleton(FactoryInterface::class, Factory::class);
73+
74+
// Schemas Repository
75+
$this->app->singleton(SchemasRepositoryInterface::class, function (Container $container) {
76+
/** @var Repository $config */
77+
$config = $container->make('config');
78+
$key = sprintf('%s.%s', C::KEY, C::SCHEMAS);
79+
return new SchemasRepository((array) $config->get($key));
80+
});
81+
82+
// Encoder Options Repository
83+
$this->app->singleton(EncoderOptionsRepositoryInterface::class, function (Container $container) {
84+
/** @var Repository $config */
85+
$config = $container->make('config');
86+
$key = sprintf('%s.%s', C::KEY, C::ENCODER_OPTIONS);
87+
return new EncoderOptionsRepository((array) $config->get($key));
88+
});
89+
90+
// Encoders Repository
91+
$this->app->singleton(EncodersRepositoryInterface::class, EncodersRepository::class);
92+
93+
// Codec Matcher Repository
94+
$this->app->singleton(CodecMatcherRepositoryInterface::class, CodecMatcherRepository::class);
95+
96+
// Laravel Integration
97+
$this->app->alias(CurrentRequestInterface::class, LaravelIntegration::class);
98+
$this->app->alias(NativeResponsesInterface::class, LaravelIntegration::class);
99+
$this->app->singleton(ResponsesInterface::class, Responses::class);
100+
101+
// Exception Thrower
102+
$this->app->singleton(ExceptionThrowerInterface::class, ExceptionThrower::class);
103+
104+
// Exception Render Container
105+
$this->app->singleton(RenderContainerInterface::class, function (Container $container) {
106+
/** @var Repository $config */
107+
$config = $container->make('config');
108+
$key = sprintf('%s.%s', C::KEY, C::EXCEPTION_RENDER_CONTAINER);
109+
$renderContainer = new RenderContainer();
110+
$renderContainer->configure((array) $config->get($key));
111+
return $renderContainer;
112+
});
113+
}
114+
115+
/**
116+
* @return array
117+
*/
118+
public function provides()
119+
{
120+
return [
121+
FactoryInterface::class,
122+
SchemasRepositoryInterface::class,
123+
EncoderOptionsRepositoryInterface::class,
124+
EncodersRepositoryInterface::class,
125+
CodecMatcherRepositoryInterface::class,
126+
CurrentRequestInterface::class,
127+
NativeResponsesInterface::class,
128+
ResponsesInterface::class,
129+
RenderContainerInterface::class,
130+
];
131+
}
132+
}

0 commit comments

Comments
 (0)
0