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