Skip to content

Commit d7ac9b1

Browse files
authored
Add Laravel 5.5 compatibility (#141)
* auto includes (#114) * auto includes * auto includes * polish auto includes * edit readme * Laravel 5.5. compatibility (#122) * make compatible with laravel 5.5 * rename config file * Apply fixes from StyleCI (#134) * add php 7.2 * add to config file * Update README.md (#137) Discovery
1 parent 4453641 commit d7ac9b1

15 files changed

+137
-137
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.6
54
- 7.0
65
- 7.1
76
- 7.2
@@ -16,7 +15,4 @@ before_script:
1615
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
1716

1817
script:
19-
- vendor/bin/phpunit --testdox --coverage-text --coverage-clover=coverage.clover
20-
21-
after_script:
22-
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
18+
- vendor/bin/phpunit --testdox --coverage-text --coverage-clover=coverage.clover

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable changes to `laravel-fractal` will be documented in this file
44

5+
6+
## 5.0.0 - 2017-XX-XX
7+
8+
- Laravel 5.5 support, dropped support for all older versions
9+
- renamed config file from `laravel-fractal` to `fractal`
10+
- added auto-includes
11+
12+
## 4.5.0 - 2017-08-20
13+
- add `default_paginator` to config file
14+
15+
516
## 4.4.0 - 2017-08-20
617
- add `fractal_class` to config file
718

README.md

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,7 @@ You can pull in the package via composer:
6969
$ composer require spatie/laravel-fractal
7070
```
7171

72-
Next up, the service provider must be registered:
73-
74-
```php
75-
// config/app.php
76-
'providers' => [
77-
...
78-
Spatie\Fractal\FractalServiceProvider::class,
79-
80-
];
81-
```
72+
The package will automatically register itself.
8273

8374
If you want to make use of the facade you must install it as well:
8475

@@ -103,56 +94,43 @@ This is the contents of the published file:
10394

10495
```php
10596
return [
106-
107-
/*
108-
|--------------------------------------------------------------------------
109-
| Default Serializer
110-
|--------------------------------------------------------------------------
111-
|
112-
| The default serializer to be used when performing a transformation. It
113-
| may be left empty to use Fractal's default one. This can either be a
114-
| string or a League\Fractal\Serializer\SerializerAbstract subclass.
115-
|
116-
*/
117-
97+
/*
98+
* The default serializer to be used when performing a transformation. It
99+
* may be left empty to use Fractal's default one. This can either be a
100+
* string or a League\Fractal\Serializer\SerializerAbstract subclass.
101+
*/
118102
'default_serializer' => '',
119-
120-
/*
121-
|--------------------------------------------------------------------------
122-
| Default Paginator
123-
|--------------------------------------------------------------------------
124-
|
125-
| The default paginator to be used when performing a transformation. It
126-
| may be left empty to use Fractal's default one. This can either be a
127-
| string or a League\Fractal\Paginator\PaginatorInterface subclass.
128-
|
129-
*/
130103

104+
/* The default paginator to be used when performing a transformation. It
105+
* may be left empty to use Fractal's default one. This can either be a
106+
* string or a League\Fractal\Paginator\PaginatorInterface subclass.*/
131107
'default_paginator' => '',
132108

133109
/*
134-
|--------------------------------------------------------------------------
135-
| JsonApiSerializer links support
136-
|--------------------------------------------------------------------------
137-
|
138-
| League\Fractal\Serializer\JsonApiSerializer will use this value to
139-
| as a prefix for generated links. Set to `null` to disable this.
140-
|
141-
*/
110+
* League\Fractal\Serializer\JsonApiSerializer will use this value to
111+
* as a prefix for generated links. Set to `null` to disable this.
112+
*/
142113
'base_url' => null,
143114

144115
/*
145-
|--------------------------------------------------------------------------
146-
| Fractal Class
147-
|--------------------------------------------------------------------------
148-
|
149-
| If you wish to override or extend the default Spatie\Fractal\Fractal
150-
| instance provide the name of the class you want to use.
151-
|
152-
*/
153-
'fractal_class' => 'Spatie\Fractal\Fractal',
154-
155-
];
116+
* If you wish to override or extend the default Spatie\Fractal\Fractal
117+
* instance provide the name of the class you want to use.
118+
*/
119+
'fractal_class' => Spatie\Fractal\Fractal::class,
120+
121+
'auto_includes' => [
122+
123+
/*
124+
* If enabled Fractal will automatically add the includes who's
125+
* names are present in the `include` request parameter.
126+
*/
127+
'enabled' => true,
128+
129+
/*
130+
* The name of key in the request to where we should look for the includes to include.
131+
*/
132+
'request_key' => 'include',
133+
],
156134
```
157135

158136
## Usage

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
}
2222
],
2323
"require": {
24-
"php" : "^5.6|^7.0",
25-
"illuminate/contracts": "~5.1.0|~5.2.0|~5.3.0|~5.4.0",
26-
"illuminate/support": "~5.1.0|~5.2.0|~5.3.0|~5.4.0",
24+
"php" : "^7.0",
25+
"illuminate/contracts": "5.5.x-dev",
26+
"illuminate/support": "5.5.x-dev",
2727
"spatie/fractalistic": "^2.5"
2828
},
2929
"require-dev": {
30-
"orchestra/testbench": "~3.2.0|3.3.0|~3.4.0",
31-
"phpunit/phpunit" : "^5.7.5"
30+
"orchestra/testbench": "3.5.x-dev",
31+
"phpunit/phpunit" : "^6.2"
3232
},
3333
"autoload": {
3434
"psr-4": {
@@ -58,5 +58,8 @@
5858
"Fractal": "Spatie\\Fractal\\FractalFacade"
5959
}
6060
}
61-
}
61+
},
62+
"minimum-stability": "dev",
63+
"prefer-stable": true
64+
6265
}

config/fractal.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* The default serializer to be used when performing a transformation. It
6+
* may be left empty to use Fractal's default one. This can either be a
7+
* string or a League\Fractal\Serializer\SerializerAbstract subclass.
8+
*/
9+
'default_serializer' => '',
10+
11+
/* The default paginator to be used when performing a transformation. It
12+
* may be left empty to use Fractal's default one. This can either be a
13+
* string or a League\Fractal\Paginator\PaginatorInterface subclass.*/
14+
'default_paginator' => '',
15+
16+
/*
17+
* League\Fractal\Serializer\JsonApiSerializer will use this value to
18+
* as a prefix for generated links. Set to `null` to disable this.
19+
*/
20+
'base_url' => null,
21+
22+
/*
23+
* If you wish to override or extend the default Spatie\Fractal\Fractal
24+
* instance provide the name of the class you want to use.
25+
*/
26+
'fractal_class' => Spatie\Fractal\Fractal::class,
27+
28+
'auto_includes' => [
29+
30+
/*
31+
* If enabled Fractal will automatically add the includes who's
32+
* names are present in the `include` request parameter.
33+
*/
34+
'enabled' => true,
35+
36+
/*
37+
* The name of key in the request to where we should look for the includes to include.
38+
*/
39+
'request_key' => 'include',
40+
],
41+
];

resources/config/laravel-fractal.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/Fractal.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use League\Fractal\Manager;
7+
use Illuminate\Http\Request;
78
use Illuminate\Http\JsonResponse;
89
use League\Fractal\Serializer\JsonApiSerializer;
910
use Spatie\Fractalistic\Fractal as Fractalistic;
@@ -30,12 +31,21 @@ public static function create($data = null, $transformer = null, $serializer = n
3031
{
3132
$fractal = parent::create($data, $transformer, $serializer);
3233

34+
if (config('fractal.auto_includes.enabled')) {
35+
$requestKey = config('fractal.auto_includes.request_key');
36+
37+
if (request()->query($requestKey)) {
38+
$fractal->parseIncludes(explode(',', request()->query($requestKey)));
39+
}
40+
}
41+
3342
if (empty($serializer)) {
34-
$serializer = config('laravel-fractal.default_serializer');
43+
$serializer = config('fractal.default_serializer');
3544
}
3645

3746
if ($data instanceof LengthAwarePaginator) {
38-
$paginator = config('laravel-fractal.default_paginator');
47+
$paginator = config('fractal.default_paginator');
48+
3949
if (empty($paginator)) {
4050
$paginator = IlluminatePaginatorAdapter::class;
4151
}
@@ -56,7 +66,7 @@ public static function create($data = null, $transformer = null, $serializer = n
5666
}
5767

5868
if ($serializer == JsonApiSerializer::class) {
59-
$baseUrl = config('laravel-fractal.base_url');
69+
$baseUrl = config('fractal.base_url');
6070

6171
return $fractal->serializeWith(new $serializer($baseUrl));
6272
}

src/FractalFacade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class FractalFacade extends Facade
1616
*/
1717
protected static function getFacadeAccessor()
1818
{
19-
return 'laravel-fractal';
19+
return 'fractal';
2020
}
2121
}

src/FractalServiceProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ public function boot()
2828
*/
2929
public function register()
3030
{
31-
$this->app->singleton('laravel-fractal', function (...$arguments) {
31+
$this->app->singleton('fractal', function (...$arguments) {
3232
return fractal(...$arguments);
3333
});
3434

35-
$this->app->alias('laravel-fractal', Fractal::class);
35+
$this->app->alias('fractal', Fractal::class);
3636
}
3737

3838
protected function setupConfig()
3939
{
40-
$source = realpath(__DIR__.'/../resources/config/laravel-fractal.php');
40+
$source = realpath(__DIR__.'/../config/fractal.php');
4141

4242
if ($this->app instanceof LaravelApplication) {
43-
$this->publishes([$source => config_path('laravel-fractal.php')]);
43+
$this->publishes([$source => config_path('fractal.php')]);
4444
} elseif ($this->app instanceof LumenApplication) {
45-
$this->app->configure('laravel-fractal');
45+
$this->app->configure('fractal');
4646
}
4747

48-
$this->mergeConfigFrom($source, 'laravel-fractal');
48+
$this->mergeConfigFrom($source, 'fractal');
4949
}
5050
}

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
function fractal($data = null, $transformer = null, $serializer = null)
1414
{
15-
$fractalClass = config('laravel-fractal.fractal_class') ?: Fractal::class;
15+
$fractalClass = config('fractal.fractal_class') ?? Fractal::class;
1616

1717
return $fractalClass::create($data, $transformer, $serializer);
1818
}

tests/ConfigPaginatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function it_uses_the_default_paginator_when_it_is_specified()
2525
new TestTransformer()
2626
)->toArray();
2727

28+
2829
$expectedArray = [
2930
'data' => [
3031
['id' => 1, 'author' => 'Philip K Dick'],

tests/FractalFacadeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function it_can_transform_the_given_array_with_the_given_closure()
3030
/** @test */
3131
public function it_will_use_the_configured_serializer()
3232
{
33-
$this->app['config']->set('laravel-fractal.default_serializer', ArraySerializer::class);
33+
$this->app['config']->set('fractal.default_serializer', ArraySerializer::class);
3434

3535
$response = FractalFacade::create()
3636
->item($this->testBooks[0])
@@ -43,7 +43,7 @@ public function it_will_use_the_configured_serializer()
4343
/** @test */
4444
public function it_will_use_the_provided_serializer_instead_of_the_configured_serializer()
4545
{
46-
$this->app['config']->set('laravel-fractal.default_serializer', DataArraySerializer::class);
46+
$this->app['config']->set('fractal.default_serializer', DataArraySerializer::class);
4747

4848
$actualArray = FractalFacade::create($this->testBooks, new TestTransformer, new ArraySerializer)->toArray();
4949

tests/FractalInstanceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function it_returns_a_default_instance_when_resolving_fractal_using_the_c
2323
/** @test */
2424
public function it_returns_a_configured_instance_when_resolving_fractal_using_the_container()
2525
{
26-
$this->app->forgetInstance('laravel-fractal');
26+
$this->app->forgetInstance('fractal');
2727

28-
app('config')->set('laravel-fractal.fractal_class', FractalExtensionClass::class);
28+
app('config')->set('fractal.fractal_class', FractalExtensionClass::class);
2929

3030
$this->assertInstanceOf(FractalExtensionClass::class, $this->app->make(Fractal::class));
3131
}

0 commit comments

Comments
 (0)