Skip to content

Commit 11d0ed7

Browse files
authored
Merge pull request #572 from sangnguyenplus/laravel-6-compatible
Add support Laravel 6.0 & remove deprecated functions
2 parents b7bc937 + 39187db commit 11d0ed7

File tree

8 files changed

+47
-39
lines changed

8 files changed

+47
-39
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"require": {
1818
"php": ">=7.0.0",
1919
"fzaninotto/faker": "~1.8",
20-
"illuminate/routing": "5.5.* || 5.6.* || 5.7.* || 5.8.*",
21-
"illuminate/support": "5.5.* || 5.6.* || 5.7.* || 5.8.*",
22-
"illuminate/console": "5.5.* || 5.6.* || 5.7.* || 5.8.*",
20+
"illuminate/routing": "^5.5|^6.0",
21+
"illuminate/support": "^5.5|^6.0",
22+
"illuminate/console": "^5.5|^6.0",
2323
"mpociot/documentarian": "^0.2.0",
2424
"mpociot/reflection-docblock": "^1.0.1",
2525
"ramsey/uuid": "^3.8",

resources/views/documentarian.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@foreach($parsedRoutes as $groupName => $routes)
99
#{!! $groupName !!}
1010
{{-- We pick the first non-empty description we see. --}}
11-
{!! array_first($routes, function ($route) { return $route['groupDescription'] !== ''; })['groupDescription'] ?? '' !!}
11+
{!! \Illuminate\Support\Arr::first($routes, function ($route) { return $route['groupDescription'] !== ''; })['groupDescription'] ?? '' !!}
1212
@foreach($routes as $parsedRoute)
1313
@if($writeCompareFile === true)
1414
{!! $parsedRoute['output'] !!}

src/Tools/Generator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Faker\Factory;
66
use ReflectionClass;
77
use ReflectionMethod;
8+
use Illuminate\Support\Str;
89
use Illuminate\Routing\Route;
910
use Mpociot\Reflection\DocBlock;
1011
use Mpociot\Reflection\DocBlock\Tag;
@@ -227,7 +228,7 @@ protected function getQueryParametersFromDocBlock(array $tags)
227228

228229
list($description, $value) = $this->parseDescription($description, 'string');
229230
if (is_null($value) && ! $this->shouldExcludeExample($tag)) {
230-
$value = str_contains($description, ['number', 'count', 'page'])
231+
$value = Str::contains($description, ['number', 'count', 'page'])
231232
? $this->generateDummyValue('integer')
232233
: $this->generateDummyValue('string');
233234
}

src/Tools/ResponseStrategies/TransformerTagsStrategy.php

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

55
use ReflectionClass;
66
use ReflectionMethod;
7+
use Illuminate\Support\Arr;
78
use League\Fractal\Manager;
89
use Illuminate\Routing\Route;
910
use Mpociot\ApiDoc\Tools\Flags;
@@ -80,15 +81,15 @@ private function getTransformerClass($tag)
8081
*/
8182
private function getClassToBeTransformed(array $tags, ReflectionMethod $transformerMethod)
8283
{
83-
$modelTag = array_first(array_filter($tags, function ($tag) {
84+
$modelTag = Arr::first(array_filter($tags, function ($tag) {
8485
return ($tag instanceof Tag) && strtolower($tag->getName()) == 'transformermodel';
8586
}));
8687

8788
$type = null;
8889
if ($modelTag) {
8990
$type = $modelTag->getContent();
9091
} else {
91-
$parameter = array_first($transformerMethod->getParameters());
92+
$parameter = Arr::first($transformerMethod->getParameters());
9293
if ($parameter->hasType() && ! $parameter->getType()->isBuiltin() && class_exists((string) $parameter->getType())) {
9394
// ladies and gentlemen, we have a type!
9495
$type = (string) $parameter->getType();
@@ -146,6 +147,6 @@ private function getTransformerTag(array $tags)
146147
})
147148
);
148149

149-
return array_first($transFormerTags);
150+
return Arr::first($transFormerTags);
150151
}
151152
}

src/Tools/RouteMatcher.php

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

33
namespace Mpociot\ApiDoc\Tools;
44

5+
use Illuminate\Support\Str;
56
use Illuminate\Routing\Route;
67
use Dingo\Api\Routing\RouteCollection;
78
use Illuminate\Support\Facades\Route as RouteFacade;
@@ -71,18 +72,18 @@ private function shouldIncludeRoute(Route $route, array $routeRule, array $mustI
7172
? ! empty(array_intersect($route->versions(), $routeRule['match']['versions'] ?? []))
7273
: true;
7374

74-
return str_is($mustIncludes, $route->getName())
75-
|| str_is($mustIncludes, $route->uri())
76-
|| (str_is($routeRule['match']['domains'] ?? [], $route->getDomain())
77-
&& str_is($routeRule['match']['prefixes'] ?? [], $route->uri())
75+
return Str::is($mustIncludes, $route->getName())
76+
|| Str::is($mustIncludes, $route->uri())
77+
|| (Str::is($routeRule['match']['domains'] ?? [], $route->getDomain())
78+
&& Str::is($routeRule['match']['prefixes'] ?? [], $route->uri())
7879
&& $matchesVersion);
7980
}
8081

8182
private function shouldExcludeRoute(Route $route, array $routeRule)
8283
{
8384
$excludes = $routeRule['exclude'] ?? [];
8485

85-
return str_is($excludes, $route->getName())
86-
|| str_is($excludes, $route->uri());
86+
return Str::is($excludes, $route->getName())
87+
|| Str::is($excludes, $route->uri());
8788
}
8889
}

src/Tools/Traits/ParamHelpers.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Mpociot\ApiDoc\Tools\Traits;
44

5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
7+
58
trait ParamHelpers
69
{
710
/**
@@ -36,9 +39,9 @@ protected function cleanParams(array $params)
3639
*/
3740
protected function cleanValueFrom($name, $value, array &$values = [])
3841
{
39-
if (str_contains($name, '[')) {
42+
if (Str::contains($name, '[')) {
4043
$name = str_replace(['][', '[', ']', '..'], ['.', '.', '', '.*.'], $name);
4144
}
42-
array_set($values, str_replace('.*', '.0', $name), $value);
45+
Arr::set($values, str_replace('.*', '.0', $name), $value);
4346
}
4447
}

tests/Unit/GeneratorTestCase.php

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

33
namespace Mpociot\ApiDoc\Tests\Unit;
44

5+
use Illuminate\Support\Arr;
56
use Orchestra\Testbench\TestCase;
67
use Mpociot\ApiDoc\Tools\Generator;
78
use Mpociot\ApiDoc\Tools\DocumentationConfig;
@@ -315,7 +316,7 @@ public function can_parse_response_tag()
315316
{
316317
$route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
317318
$parsed = $this->generator->processRoute($route);
318-
$response = array_first($parsed['response']);
319+
$response = Arr::first($parsed['response']);
319320

320321
$this->assertTrue(is_array($parsed));
321322
$this->assertArrayHasKey('showresponse', $parsed);
@@ -336,7 +337,7 @@ public function can_parse_response_tag_with_status_code()
336337
{
337338
$route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
338339
$parsed = $this->generator->processRoute($route);
339-
$response = array_first($parsed['response']);
340+
$response = Arr::first($parsed['response']);
340341

341342
$this->assertTrue(is_array($parsed));
342343
$this->assertArrayHasKey('showresponse', $parsed);
@@ -385,7 +386,7 @@ public function can_parse_transformer_tag($serializer, $expected)
385386
config(['apidoc.fractal.serializer' => $serializer]);
386387
$route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
387388
$parsed = $this->generator->processRoute($route);
388-
$response = array_first($parsed['response']);
389+
$response = Arr::first($parsed['response']);
389390

390391
$this->assertTrue(is_array($parsed));
391392
$this->assertArrayHasKey('showresponse', $parsed);
@@ -403,7 +404,7 @@ public function can_parse_transformer_tag_with_model()
403404
{
404405
$route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
405406
$parsed = $this->generator->processRoute($route);
406-
$response = array_first($parsed['response']);
407+
$response = Arr::first($parsed['response']);
407408

408409
$this->assertTrue(is_array($parsed));
409410
$this->assertArrayHasKey('showresponse', $parsed);
@@ -421,7 +422,7 @@ public function can_parse_transformer_collection_tag()
421422
{
422423
$route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
423424
$parsed = $this->generator->processRoute($route);
424-
$response = array_first($parsed['response']);
425+
$response = Arr::first($parsed['response']);
425426

426427
$this->assertTrue(is_array($parsed));
427428
$this->assertArrayHasKey('showresponse', $parsed);
@@ -440,7 +441,7 @@ public function can_parse_transformer_collection_tag_with_model()
440441
{
441442
$route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
442443
$parsed = $this->generator->processRoute($route);
443-
$response = array_first($parsed['response']);
444+
$response = Arr::first($parsed['response']);
444445

445446
$this->assertTrue(is_array($parsed));
446447
$this->assertArrayHasKey('showresponse', $parsed);
@@ -469,7 +470,7 @@ public function can_call_route_and_generate_response()
469470
],
470471
];
471472
$parsed = $this->generator->processRoute($route, $rules);
472-
$response = array_first($parsed['response']);
473+
$response = Arr::first($parsed['response']);
473474

474475
$this->assertTrue(is_array($parsed));
475476
$this->assertArrayHasKey('showresponse', $parsed);
@@ -496,7 +497,7 @@ public function can_override_config_during_response_call()
496497
],
497498
];
498499
$parsed = $this->generator->processRoute($route, $rules);
499-
$response = json_decode(array_first($parsed['response'])['content'], true);
500+
$response = json_decode(Arr::first($parsed['response'])['content'], true);
500501
$originalValue = $response['app.env'];
501502

502503
$now = time();
@@ -509,7 +510,7 @@ public function can_override_config_during_response_call()
509510
],
510511
];
511512
$parsed = $this->generator->processRoute($route, $rules);
512-
$response = json_decode(array_first($parsed['response'])['content'], true);
513+
$response = json_decode(Arr::first($parsed['response'])['content'], true);
513514
$newValue = $response['app.env'];
514515
$this->assertEquals($now, $newValue);
515516
$this->assertNotEquals($originalValue, $newValue);
@@ -530,7 +531,7 @@ public function can_override_url_path_parameters_with_bindings()
530531
],
531532
];
532533
$parsed = $this->generator->processRoute($route, $rules);
533-
$response = json_decode(array_first($parsed['response'])['content'], true);
534+
$response = json_decode(Arr::first($parsed['response'])['content'], true);
534535
$param = $response['param'];
535536
$this->assertEquals($rand, $param);
536537
}
@@ -550,7 +551,7 @@ public function replaces_optional_url_path_parameters_with_bindings()
550551
],
551552
];
552553
$parsed = $this->generator->processRoute($route, $rules);
553-
$response = json_decode(array_first($parsed['response'])['content'], true);
554+
$response = json_decode(Arr::first($parsed['response'])['content'], true);
554555
$param = $response['param'];
555556
$this->assertEquals($rand, $param);
556557
}
@@ -573,12 +574,12 @@ public function uses_correct_bindings_by_prefix()
573574
],
574575
];
575576
$parsed = $this->generator->processRoute($route1, $rules);
576-
$response = json_decode(array_first($parsed['response'])['content'], true);
577+
$response = json_decode(Arr::first($parsed['response'])['content'], true);
577578
$param = $response['param'];
578579
$this->assertEquals($rand1, $param);
579580

580581
$parsed = $this->generator->processRoute($route2, $rules);
581-
$response = json_decode(array_first($parsed['response'])['content'], true);
582+
$response = json_decode(Arr::first($parsed['response'])['content'], true);
582583
$param = $response['param'];
583584
$this->assertEquals($rand2, $param);
584585
}
@@ -593,7 +594,7 @@ public function can_parse_response_file_tag()
593594

594595
$route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
595596
$parsed = $this->generator->processRoute($route);
596-
$response = array_first($parsed['response']);
597+
$response = Arr::first($parsed['response']);
597598

598599
$this->assertTrue(is_array($parsed));
599600
$this->assertArrayHasKey('showresponse', $parsed);
@@ -618,7 +619,7 @@ public function can_add_or_replace_key_value_pair_in_response_file()
618619

619620
$route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
620621
$parsed = $this->generator->processRoute($route);
621-
$response = array_first($parsed['response']);
622+
$response = Arr::first($parsed['response']);
622623

623624
$this->assertTrue(is_array($parsed));
624625
$this->assertArrayHasKey('showresponse', $parsed);
@@ -717,7 +718,7 @@ public function uses_configured_settings_when_calling_route()
717718
],
718719
];
719720
$parsed = $this->generator->processRoute($route, $rules);
720-
$response = array_first($parsed['response']);
721+
$response = Arr::first($parsed['response']);
721722

722723
$this->assertTrue(is_array($parsed));
723724
$this->assertArrayHasKey('showresponse', $parsed);

tests/Unit/RouteMatcherTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mpociot\ApiDoc\Tests\Unit;
44

5+
use Illuminate\Support\Str;
56
use Dingo\Api\Routing\Router;
67
use Orchestra\Testbench\TestCase;
78
use Mpociot\ApiDoc\Tools\RouteMatcher;
@@ -108,7 +109,7 @@ public function testRespectsPrefixesRuleForLaravelRouter()
108109
$routes = $this->matcher->getRoutesToBeDocumented($routeRules);
109110
$this->assertCount(4, $routes);
110111
foreach ($routes as $route) {
111-
$this->assertTrue(str_is('prefix2/*', $route['route']->uri()));
112+
$this->assertTrue(Str::is('prefix2/*', $route['route']->uri()));
112113
}
113114
}
114115

@@ -130,14 +131,14 @@ public function testRespectsPrefixesRuleForDingoRouter()
130131
$routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
131132
$this->assertCount(4, $routes);
132133
foreach ($routes as $route) {
133-
$this->assertTrue(str_is('prefix1/*', $route['route']->uri()));
134+
$this->assertTrue(Str::is('prefix1/*', $route['route']->uri()));
134135
}
135136

136137
$routeRules[0]['match']['prefixes'] = ['prefix2/*'];
137138
$routes = $this->matcher->getDingoRoutesToBeDocumented($routeRules);
138139
$this->assertCount(4, $routes);
139140
foreach ($routes as $route) {
140-
$this->assertTrue(str_is('prefix2/*', $route['route']->uri()));
141+
$this->assertTrue(Str::is('prefix2/*', $route['route']->uri()));
141142
}
142143
}
143144

@@ -337,14 +338,14 @@ public function testMergesRoutesFromDifferentRuleGroupsForLaravelRouter()
337338

338339
$routes = collect($routes);
339340
$firstRuleGroup = $routes->filter(function ($route) {
340-
return str_is('prefix1/*', $route['route']->uri())
341-
&& str_is('domain1.*', $route['route']->getDomain());
341+
return Str::is('prefix1/*', $route['route']->uri())
342+
&& Str::is('domain1.*', $route['route']->getDomain());
342343
});
343344
$this->assertCount(2, $firstRuleGroup);
344345

345346
$secondRuleGroup = $routes->filter(function ($route) {
346-
return str_is('prefix2/*', $route['route']->uri())
347-
&& str_is('domain2.*', $route['route']->getDomain());
347+
return Str::is('prefix2/*', $route['route']->uri())
348+
&& Str::is('domain2.*', $route['route']->getDomain());
348349
});
349350
$this->assertCount(2, $secondRuleGroup);
350351
}

0 commit comments

Comments
 (0)