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