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