1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Hashid; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
|
8
|
|
|
class HashidServiceProvider extends ServiceProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Indicates if loading of the provider is deferred. |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
protected $defer = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Bootstrap the service provider. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function boot() |
23
|
|
|
{ |
24
|
|
|
// |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Register the service provider. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function register() |
33
|
|
|
{ |
34
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/hashid.php', 'hashid'); |
35
|
|
|
|
36
|
|
|
$this->registerServices(); |
37
|
|
|
|
38
|
|
|
if ($this->app->runningInConsole()) { |
39
|
|
|
$this->registerForConsole(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Register service bindings. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
protected function registerServices() |
49
|
|
|
{ |
50
|
|
|
foreach ($this->getSingletonBindings() as $abstract => $concrete) { |
51
|
|
|
$this->app->singleton($abstract, function ($app) use ($concrete) { |
52
|
|
|
$reflector = new ReflectionClass($concrete); |
53
|
|
|
|
54
|
|
|
if (is_null($reflector->getConstructor())) { |
55
|
|
|
return new $concrete; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $reflector->newInstanceArgs([$app]); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$this->app->alias($abstract, $concrete); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($this->getClassAliases() as $abstract => $alias) { |
65
|
|
|
$this->app->alias($abstract, $alias); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get singleton bindings to be registered. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
protected function getSingletonBindings() |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
|
|
'hashid' => HashidManager::class, |
78
|
|
|
'hashid.connection.hex' => HexConnection::class, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get class aliases to be registered. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
protected function getClassAliases() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
Base62Connection::class => 'hashid.connection.base62', |
91
|
|
|
Base64Connection::class => 'hashid.connection.base64', |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Register for the console application. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function registerForConsole() |
101
|
|
|
{ |
102
|
|
|
$this->publishes([ |
103
|
|
|
__DIR__.'/../config/hashid.php' => config_path('hashid.php'), |
104
|
|
|
], 'hashid'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the services provided by the provider. |
109
|
|
|
* |
110
|
|
|
* @return string[] |
111
|
|
|
*/ |
112
|
|
|
public function provides() |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
'hashid', HashidManager::class, |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|