1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EloquentJs\Laravel; |
4
|
|
|
|
5
|
|
|
use EloquentJs\Laravel\Console\GenerateJavascript; |
6
|
|
|
use EloquentJs\Laravel\RouteRegistrar; |
7
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
8
|
|
|
use Illuminate\Routing\Router; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use EloquentJs\Laravel\Controllers\GenericResourceController; |
11
|
|
|
use EloquentJs\Laravel\Events\EloquentJsWasCalled; |
12
|
|
|
use EloquentJs\Laravel\Listeners\ApplyQueryMethodsToBuilder; |
13
|
|
|
use EloquentJs\Translators\JsonQueryTranslator; |
14
|
|
|
use EloquentJs\Translators\QueryTranslator; |
15
|
|
|
|
16
|
|
|
class EloquentJsServiceProvider extends ServiceProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Register the service provider. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function register() |
24
|
|
|
{ |
25
|
|
|
$this->app->bind(QueryTranslator::class, function ($app) { |
26
|
|
|
return new JsonQueryTranslator($app['request']->input('query', '[]')); |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
$this->commands([GenerateJavascript::class]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Boot the service provider. |
34
|
|
|
* |
35
|
|
|
* @param Dispatcher $events |
36
|
|
|
* @param Router $router |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function boot(Dispatcher $events, Router $router) |
40
|
|
|
{ |
41
|
|
|
$events->listen(EloquentJsWasCalled::class, ApplyQueryMethodsToBuilder::class); |
42
|
|
|
|
43
|
|
|
$this->enableGenericResourceRouting($router); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set up the generic resource routes + controller. |
48
|
|
|
* |
49
|
|
|
* @param Router $router |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
protected function enableGenericResourceRouting(Router $router) |
53
|
|
|
{ |
54
|
|
|
$app = $this->app; |
55
|
|
|
|
56
|
|
|
$app->singleton('eloquentjs.router', function ($app) { |
57
|
|
|
return (new RouteRegistrar($app['router'])) |
58
|
|
|
->setController('\\'.GenericResourceController::class); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$router->macro('eloquent', function ($uri, $resource, $options = []) use ($app) { |
62
|
|
|
$app['eloquentjs.router']->addRoute($uri, $resource, $options); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
// Typically you'd have dedicated controllers for each resource. |
66
|
|
|
// Since that's not the case here, we need some way of telling |
67
|
|
|
// our generic controller which resource we're working with. |
68
|
|
|
$app->resolving(function (GenericResourceController $controller, $app) { |
|
|
|
|
69
|
|
|
if ($resource = $app['eloquentjs.router']->getCurrentResource()) { |
70
|
|
|
$controller->setModel($app->make($resource)); |
71
|
|
|
} |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: