1
|
|
|
<?php namespace Propaganistas\LaravelFakeId; |
2
|
|
|
|
3
|
|
|
use Closure; |
4
|
|
|
use Illuminate\Support\ServiceProvider; |
5
|
|
|
use Jenssegers\Optimus\Optimus; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
7
|
|
|
|
8
|
|
|
class FakeIdServiceProvider extends ServiceProvider |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Indicates if loading of the provider is deferred. |
13
|
|
|
* |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
protected $defer = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Boots the service provider. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function boot() |
24
|
|
|
{ |
25
|
|
|
// Publish config. |
26
|
|
|
$this->publishes([ |
27
|
|
|
__DIR__ . '/../config/config.php' => config_path('fakeid.php'), |
28
|
|
|
]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Register the service provider. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function register() |
37
|
|
|
{ |
38
|
|
|
// Merge default config. |
39
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'fakeid'); |
40
|
|
|
|
41
|
|
|
// Register setup command. |
42
|
|
|
$this->app->singleton('fakeid.command.setup', function($app) { |
|
|
|
|
43
|
|
|
return new Commands\FakeIdSetupCommand(); |
44
|
|
|
}); |
45
|
|
|
$this->commands('fakeid.command.setup'); |
46
|
|
|
|
47
|
|
|
// Register FakeId driver. |
48
|
|
|
$this->app->singleton('fakeid', function($app) { |
49
|
|
|
return new Optimus( |
50
|
|
|
$app['config']['fakeid.prime'], |
51
|
|
|
$app['config']['fakeid.inverse'], |
52
|
|
|
$app['config']['fakeid.random'] |
53
|
|
|
); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
$this->registerRouterMacro(); |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the services provided by the provider. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function provides() |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
'fakeid', |
69
|
|
|
'fakeid.command.setup' |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Register the custom router macro. |
75
|
|
|
*/ |
76
|
|
|
protected function registerRouterMacro() |
77
|
|
|
{ |
78
|
|
|
$this->app['router']->macro('fakeIdModel', function($key, $class, Closure $callback = null) { |
79
|
|
|
$this->bind($key, function($value) use ($key, $class, $callback) { |
|
|
|
|
80
|
|
|
if (is_null($value)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// For model binders, we will attempt to retrieve the models using the first |
85
|
|
|
// method on the model instance. If we cannot retrieve the models we'll |
86
|
|
|
// throw a not found exception otherwise we will return the instance. |
87
|
|
|
$instance = $this->container->make($class); |
|
|
|
|
88
|
|
|
|
89
|
|
|
// Decode FakeId first if applicable. |
90
|
|
|
if (in_array(FakeIdTrait::class, class_uses($class))) { |
91
|
|
|
$value = $this->container->make('fakeid')->decode($value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($model = $instance->where($instance->getRouteKeyName(), $value)->first()) { |
95
|
|
|
return $model; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// If a callback was supplied to the method we will call that to determine |
99
|
|
|
// what we should do when the model is not found. This just gives these |
100
|
|
|
// developer a little greater flexibility to decide what will happen. |
101
|
|
|
if ($callback instanceof Closure) { |
102
|
|
|
return call_user_func($callback, $value); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
throw new NotFoundHttpException; |
106
|
|
|
}); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.