Skip to content

Commit 887f28b

Browse files
committed
Support laravel 5.4
* several route methods renamed. * additional parameters in `make` deprecated.
1 parent c149fb0 commit 887f28b

File tree

3 files changed

+75
-19
lines changed

3 files changed

+75
-19
lines changed

src/Entities/RouteInfo.php

+49-16
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,31 @@ public function __construct($route, $options = [])
5454
public function toArray()
5555
{
5656
return array_merge([
57-
'name' => $this->route->getName(),
58-
'methods' => $this->route->getMethods(),
59-
'domain' => $this->route->domain(),
60-
'path' => $this->preparePath(),
61-
'action' => $this->route->getAction(),
62-
'wheres' => $this->extractWheres(),
63-
'errors' => $this->errors,
57+
'name' => $this->route->getName(),
58+
'methods' => $this->getMethods(),
59+
'domain' => $this->route->domain(),
60+
'path' => $this->preparePath(),
61+
'action' => $this->route->getAction(),
62+
'wheres' => $this->extractWheres(),
63+
'errors' => $this->errors,
6464
], $this->getMeta(), $this->options);
6565
}
6666

67+
/**
68+
* Cross version get methods.
69+
*
70+
* @return array
71+
*/
72+
private function getMethods()
73+
{
74+
// Laravel <5.4
75+
if (method_exists($this->route, 'getMethods')) {
76+
$this->route->getMethods();
77+
}
78+
// Laravel 5.4+
79+
return $this->route->methods();
80+
}
81+
6782
protected function extractWheres()
6883
{
6984
$prop = $this->getRouteReflection()->getProperty('wheres');
@@ -73,7 +88,7 @@ protected function extractWheres()
7388

7489
// Хак, чтобы в json всегда был объект
7590
if (empty($wheres)) {
76-
return (object)[];
91+
return (object) [];
7792
}
7893

7994
return $wheres;
@@ -94,7 +109,7 @@ protected function extractAnnotation()
94109
{
95110
$reflection = $this->getActionReflection();
96111

97-
if (!is_null($reflection)) {
112+
if (! is_null($reflection)) {
98113
return $reflection->getDocComment();
99114
}
100115

@@ -114,7 +129,7 @@ protected function extractFormRequest()
114129
// TODO Write the reasoning behind following lines.
115130
try {
116131
$class = $parameter->getClass();
117-
} catch (\ReflectionException $e){
132+
} catch (\ReflectionException $e) {
118133
break;
119134
}
120135

@@ -170,18 +185,21 @@ protected function getActionReflection()
170185
list($controller, $action) = explode('@', $uses);
171186

172187
// Если нет контроллера.
173-
if (!class_exists($controller)) {
188+
if (! class_exists($controller)) {
174189
$this->setError('uses', 'controller does not exists');
190+
175191
return null;
176192
}
177193

178194
// Если нет метода в контроллере.
179-
if (!method_exists($controller, $action)) {
195+
if (! method_exists($controller, $action)) {
180196
$this->setError('uses', 'controller@method does not exists');
197+
181198
return null;
182199
}
183200

184-
return $this->actionReflection = new \ReflectionMethod($controller, $action);
201+
return $this->actionReflection = new \ReflectionMethod($controller,
202+
$action);
185203
}
186204

187205
if (is_callable($uses)) {
@@ -195,14 +213,29 @@ protected function getActionReflection()
195213

196214
protected function preparePath()
197215
{
198-
$path = $this->route->getPath();
216+
$path = $this->getUri();
199217
if ($path === '/') {
200218
return $path;
201219
}
202220

203221
return trim($path, '/');
204222
}
205223

224+
/**
225+
* Backwards compatible uri getter.
226+
*
227+
* @return string
228+
*/
229+
protected function getUri(){
230+
if (method_exists($this->route, 'getPath')){
231+
// Laravel <5.4
232+
return $this->route->getPath();
233+
}
234+
235+
// Laravel 5.4+
236+
return $this->route->uri();
237+
}
238+
206239
protected function setError($type, $text, $params = [])
207240
{
208241
$this->errors[$type] = trans($text, $params);
@@ -215,9 +248,9 @@ protected function getMeta()
215248
{
216249
if ($this->addMeta) {
217250
return [
218-
'annotation' => $this->extractAnnotation(),
251+
'annotation' => $this->extractAnnotation(),
219252
'formRequest' => $this->extractFormRequest(),
220-
'errors' => $this->errors,
253+
'errors' => $this->errors,
221254
];
222255
}
223256

src/Providers/RepositoryServiceProvider.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Asvae\ApiTester\Providers;
44

55

6+
use Asvae\ApiTester\Collections\RouteCollection;
67
use Asvae\ApiTester\Contracts\RequestRepositoryInterface;
78
use Asvae\ApiTester\Contracts\RouteRepositoryInterface;
89
use Asvae\ApiTester\Repositories\RouteRepository;
@@ -33,7 +34,9 @@ public function register()
3334
$repositories[] = $app->make($repository);
3435
}
3536

36-
return $app->make(RouteRepository::class, ['repositories' => $repositories]);
37+
$routeCollection = $app->make(RouteCollection::class);
38+
39+
return new RouteRepository($routeCollection, $repositories);
3740
});
3841

3942
$this->app->singleton(

src/Providers/StorageServiceProvide.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
namespace Asvae\ApiTester\Providers;
1010

1111

12+
use Asvae\ApiTester\Collections\RequestCollection;
1213
use Asvae\ApiTester\Contracts\StorageInterface;
1314
use Asvae\ApiTester\Storages\FireBaseStorage;
15+
use Asvae\ApiTester\Storages\JsonStorage;
1416
use Firebase\Token\TokenGenerator;
1517
use Illuminate\Contracts\Container\Container;
18+
use Illuminate\Filesystem\Filesystem;
1619
use Illuminate\Support\ServiceProvider;
20+
use League\Flysystem\Exception;
1721

1822
class StorageServiceProvide extends ServiceProvider
1923
{
@@ -35,9 +39,25 @@ public function register()
3539
// Регистрируем конкретный сторэйдж из списка доступных.
3640
$this->app->singleton(StorageInterface::class, function (Container $app) {
3741
// Defined driver
38-
$driver = config('api-tester.storage_drivers')[config('api-tester.storage_driver')];
42+
$selectedDriver = config('api-tester.storage_driver');
43+
$driverClassName = config('api-tester.storage_drivers')[$selectedDriver];
44+
$requestCollection = $app->make(RequestCollection::class);
3945

40-
return $app->make($driver['class'], $driver['options']);
46+
if ($selectedDriver === 'firebase'){
47+
$tokenGenerator = $app->make(TokenGenerator::class);
48+
$base = config('api-tester.storage_drivers.firebase.options.base');
49+
50+
return new FireBaseStorage($requestCollection, $tokenGenerator, $base);
51+
}
52+
53+
if ($selectedDriver === 'file'){
54+
$fileSystem = $app->make(Filesystem::class);
55+
$path = config('api-tester.storage_drivers.file.options.path');
56+
57+
return new JsonStorage($fileSystem, $requestCollection, $path);
58+
}
59+
60+
throw new Exception("Driver $selectedDriver doesn't exist. Use either 'firebase' or 'file'.");
4161
});
4262

4363
// Регистрация токен-генератора. Привязывается к ключу а не классу,

0 commit comments

Comments
 (0)