8000 Bug fixes. · dtr0yan/laravel-json-api@aed5958 · GitHub
[go: up one dir, main page]

Skip to content

Commit aed5958

Browse files
committed
Bug fixes.
1 parent a8f2b9d commit aed5958

File tree

6 files changed

+46
-46
lines changed

6 files changed

+46
-46
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,13 @@ Install using [Composer](http://getcomposer.org):
3232
$ composer require cloudcreativity/laravel-json-api
3333
```
3434

35-
Then publish the package config file:
36-
37-
``` bash
38-
$ php artisan vendor:publish --provider="CloudCreativity\JsonApi\ServiceProvider"
39-
```
40-
> Configuration settings are describe in the usage section below.
41-
42-
Add the package service provider to your `config/app.php` providers array:
35+
Add the package service provider to your `config/app.php` providers array. It is important that it is added **before** your application's route service provider.
4336

4437
``` php
4538
\CloudCreativity\JsonApi\ServiceProvider::class
4639
```
4740

48-
And add the following to the list of aliases in the same file (`config/app.php`) so that you can use the `JsonApi` facade:
41+
Plus add the following to the list of aliases in the same file (`config/app.php`) so that you can use the `JsonApi` facade:
4942

5043
``` php
5144
'aliases' => [
@@ -54,6 +47,14 @@ And add the following to the list of aliases in the same file (`config/app.php`)
5447
]
5548
```
5649

50+
Then publish the package config file:
51+
52+
``` bash
53+
$ php artisan vendor:publish --provider="CloudCreativity\JsonApi\ServiceProvider"
54+
```
55+
> Configuration settings are describe in the usage section below.
56+
57+
5758
## Usage
5859

5960
- **Configuration keys** are stored in constants on the `CloudCreativity\JsonApi\Config` class (and will be referred to as **`C::`** below).

src/Facade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace CloudCreativity\JsonApi\Facades;
3+
namespace CloudCreativity\JsonApi;
44

55
use Illuminate\Support\Facades\Facade as BaseFacade;
66
use CloudCreativity\JsonApi\Services\EnvironmentService;

src/Http/Middleware/BootJsonApi.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private function register($name)
6868
$currentRequest = $this->app->make(CurrentRequestInterface::class);
6969

7070
$codecMatcher = $repository->getCodecMatcher($name);
71+
$this->env->registerCodecMatcher($codecMatcher);
72+
7173
$parameters = $parametersFactory
7274
->createParametersParser()
7375
->parse($currentRequest, $exceptionThrower);
@@ -76,8 +78,6 @@ private function register($name)
7678
->createHeadersChecker($exceptionThrower, $codecMatcher)
7779
->checkHeaders($parameters);
7880

79-
$this->env
80-
->registerCodecMatcher($codecMatcher)
81-
->registerParameters($parameters);
81+
$this->env->registerParameters($parameters);
8282
}
8383
}

src/Http/Responses/ResponsesHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CloudCreativity\JsonApi\Http\Responses;
44

55
use CloudCreativity\JsonApi\Services\EnvironmentService;
6+
use Illuminate\Database\Eloquent\Collection;
67
use Illuminate\Http\Response;
78
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
89
use Neomerx\JsonApi\Contracts\Parameters\Headers\MediaTypeInterface;
@@ -80,6 +81,10 @@ public function meta($meta, $statusCode = Response::HTTP_OK, array $headers = []
8081
*/
8182
public function content($data, $statusCode = Response::HTTP_OK, $links = [], $meta = null, array $headers = [])
8283
{
84+
if ($data instanceof Collection) {
85+
$data = $data->all();
86+
}
87+
8388
$content = $this
8489
->getEncoder()
8590
->withLinks($links)

src/Middleware.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ class Middleware
1111
/** The middleware name for registering supported extensions */
1212
const SUPPORTED_EXT = 'json-api-supported-ext';
1313

14+
/**
15+
* @param string|null $codecMatcherName
16+
* @return string
17+
*/
18+
public static function jsonApi($codecMatcherName = null)
19+
{
20+
return ($codecMatcherName) ? sprintf('%s:%s', static::JSON_API, $codecMatcherName) : static::JSON_API;
21+
}
22+
1423
/**
1524
* @return string
1625
*/

src/ServiceProvider.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use CloudCreativity\JsonApi\Repositories\EncoderOptionsRepository;
2727
use CloudCreativity\JsonApi\Repositories\EncodersRepository;
2828
use CloudCreativity\JsonApi\Repositories\SchemasRepository;
29+
use CloudCreativity\JsonApi\Services\EnvironmentService;
2930
use Illuminate\Contracts\Config\Repository;
3031
use Illuminate\Contracts\Http\Kernel;
3132
use Illuminate\Routing\Router;
@@ -41,6 +42,7 @@
4142
use Neomerx\JsonApi\Exceptions\RendererContainer;
4243
use Neomerx\JsonApi\Factories\Factory;
4344
use Neomerx\JsonApi\Responses\Responses;
45+
use Request;
4446

4547
/**
4648
* Class ServiceProvider
@@ -52,7 +54,7 @@ class ServiceProvider extends BaseServiceProvider
5254
/**
5355
* @var bool
5456
*/
55-
protected $defer = true;
57+
protected $defer = false;
5658

5759
/**
5860
* @param Router $router
@@ -61,7 +63,9 @@ class ServiceProvider extends BaseServiceProvider
6163
public function boot(Router $router, Kernel $kernel)
6264
{
6365
// Allow publishing of config file
64-
$this->publishes(__DIR__ . '/config/json-api.php', config_path('json-api.php'));
66+
$this->publishes([
67+
__DIR__ . '/../config/json-api.php' => config_path('json-api.php'),
68+
]);
6569

6670
// Add Json Api middleware to the router.
6771
$router->middleware(M::JSON_API, BootJsonApi::class);
@@ -84,41 +88,44 @@ public function register()
8488
{
8589
$container = $this->app;
8690

91+
// Environment
92+
$container->singleton(EnvironmentService::class);
93+
8794
// Factory
88-
$container->alias(ParametersFactoryInterface::class, FactoryInterface::class);
8995
$container->singleton(FactoryInterface::class, Factory::class);
96+
$container->singleton(ParametersFactoryInterface::class, Factory::class);
9097

9198
// Encoders Repository
9299
$container->singleton(EncodersRepositoryInterface::class, EncodersRepository::class);
93100
$container->singleton(SchemasRepositoryInterface::class, SchemasRepository::class);
94101
$container->singleton(EncoderOptionsRepositoryInterface::class, EncoderOptionsRepository::class);
95102
$container->resolving(EncodersRepositoryInterface::class, function (ConfigurableInterface $repository) {
96-
$repository->configure($this->getConfig(C::ENCODERS, []));
103+
$repository->configure((array) $this->getConfig(C::ENCODERS, []));
97104
});
98105
$container->resolving(EncoderOptionsRepositoryInterface::class, function (EncoderOptionsRepositoryInterface $repository) {
99106
$repository->addModifier(function (MutableConfigInterface $config) {
100107
$key = EncoderOptionsRepositoryInterface::URL_PREFIX;
101108
if (!$config->has($key)) {
102-
$config->set($key, \Request::getSchemeAndHttpHost());
109+
$config->set($key, Request::getSchemeAndHttpHost());
103110
};
104111
});
105112
});
106113

107114
// Decoders Repository
108115
$container->singleton(DecodersRepositoryInterface::class, DecodersRepository::class);
109116
$container->resolving(DecodersRepositoryInterface::class, function (ConfigurableInterface $repository) {
110-
$repository->configure($this->getConfig(C::DECODERS, []));
117+
$repository->configure((array) $this->getConfig(C::DECODERS, []));
111118
});
112119

113120
// Codec Matcher Repository
114121
$container->singleton(CodecMatcherRepositoryInterface::class, CodecMatcherRepository::class);
115122
$container->resolving(CodecMatcherRepositoryInterface::class, function (ConfigurableInterface $repository) {
116-
$repository->configure($this->getConfig(C::CODEC_MATCHER, []));
123+
$repository->configure((array) $this->getConfig(C::CODEC_MATCHER, []));
117124
});
118125

119126
// Laravel Integration
120-
$container->alias(CurrentRequestInterface::class, LaravelIntegration::class);
121-
$container->alias(NativeResponsesInterface::class, LaravelIntegration::class);
127+
$container->singleton(CurrentRequestInterface::class, LaravelIntegration::class);
128+
$container->singleton(NativeResponsesInterface::class, LaravelIntegration::class);
122129
$container->singleton(ResponsesInterface::class, Responses::class);
123130

124131
// Exception Thrower
@@ -128,7 +135,7 @@ public function register()
128135
$container->singleton(RendererInterface::class, StandardRenderer::class);
129136
$container->singleton(RendererContainerInterface::class, RendererContainer::class);
130137
$container->resolving(RendererInterface::class, function (ConfigurableInterface $renderer) {
131-
$renderer->configure($this->getConfig(C::EXCEPTIONS));
138+
$renderer->configure((array) $this->getConfig(C::EXCEPTIONS));
132139
});
133140
$container->resolving(RendererContainerInterface::class, function (RendererContainerInterface $rendererContainer) use ($container) {
134141
/** @var ResponsesInterface $response */
@@ -141,28 +148,6 @@ public function register()
141148
});
142149
}
143150

144-
/**
145-
* @return array
146-
*/
147-
public function provides()
148-
{
149-
return [
150-
ParametersFactoryInterface::class,
151-
FactoryInterface::class,
152-
SchemasRepositoryInterface::class,
153-
EncoderOptionsRepositoryInterface::class,
154-
EncodersRepositoryInterface::class,
155-
DecodersRepositoryInterface::class,
156-
CodecMatcherRepositoryInterface::class,
157-
CurrentRequestInterface::class,
158-
NativeResponsesInterface::class,
159-
ResponsesInterface::class,
160-
ExceptionThrowerInterface::class,
161-
RendererInterface::class,
162-
RendererContainerInterface::class,
163-
];
164-
}
165-
166151
/**
167152
* @param $key
168153
* @param $default
@@ -174,7 +159,7 @@ protected function getConfig($key, $default = null)
174159
$config = $this->app->make('config');
175160
$key = sprintf('%s.%s', C::NAME, $key);
176161

177-
return (array) $config->get($key, $default);
162+
return $config->get($key, $default);
178163
}
179164

180165
}

0 commit comments

Comments
 (0)
0