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
|
7 |
|
public function boot() |
23
|
|
|
{ |
24
|
|
|
// |
25
|
7 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Register the service provider. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
7 |
|
public function register() |
33
|
|
|
{ |
34
|
7 |
|
$this->setupAssets(); |
35
|
|
|
|
36
|
7 |
|
$this->registerServices(); |
37
|
7 |
|
$this->registerCommands(); |
38
|
7 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Setup package assets. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
7 |
|
protected function setupAssets() |
46
|
|
|
{ |
47
|
7 |
|
if (is_a($this->app, 'Laravel\Lumen\Application')) { |
48
|
|
|
$this->app->configure('hashid'); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
$this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid'); |
52
|
|
|
|
53
|
7 |
|
if ($this->app->runningInConsole()) { |
54
|
7 |
|
$this->publishes([$config => config_path('hashid.php')], 'hashid'); |
55
|
7 |
|
} |
56
|
7 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Register service bindings. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
7 |
|
protected function registerServices() |
64
|
7 |
|
{ |
65
|
7 |
|
foreach ($this->getSingletonBindings() as $abstract => $concrete) { |
66
|
7 |
|
$this->app->singleton($abstract, function ($app) use ($concrete) { |
67
|
5 |
|
return $this->createInstance($concrete, [$app]); |
68
|
7 |
|
}); |
69
|
|
|
|
70
|
7 |
|
$this->app->alias($abstract, $concrete); |
71
|
7 |
|
} |
72
|
|
|
|
73
|
7 |
|
foreach ($this->getClassAliases() as $abstract => $alias) { |
74
|
7 |
|
$this->app->alias($abstract, $alias); |
75
|
7 |
|
} |
76
|
7 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create a new instance from class name. |
80
|
|
|
* |
81
|
|
|
* @param string $class |
82
|
|
|
* @param array $args |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
5 |
|
protected function createInstance($class, array $args = []) |
86
|
|
|
{ |
87
|
5 |
|
$reflector = new ReflectionClass($class); |
88
|
|
|
|
89
|
5 |
|
if (is_null($reflector->getConstructor())) { |
90
|
|
|
return new $class; |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
return $reflector->newInstanceArgs($args); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get singleton bindings to be registered. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
7 |
|
protected function getSingletonBindings() |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
7 |
|
'hashid' => HashidManager::class, |
105
|
7 |
|
'hashid.driver.base64' => Base64Driver::class, |
106
|
7 |
|
'hashid.driver.base64.integer' => Base64IntegerDriver::class, |
107
|
7 |
|
'hashid.driver.hex' => HexDriver::class, |
108
|
7 |
|
'hashid.driver.hex.integer' => HexIntegerDriver::class, |
109
|
7 |
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get class aliases to be registered. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
7 |
|
protected function getClassAliases() |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
7 |
|
Base62Driver::class => 'hashid.driver.base62', |
121
|
7 |
|
Base62IntegerDriver::class => 'hashid.driver.base62.integer', |
122
|
7 |
|
OptimusDriver::class => 'hashid.driver.optimus', |
123
|
7 |
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Register console commands. |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
7 |
|
protected function registerCommands() |
132
|
|
|
{ |
133
|
7 |
|
if ($this->app->runningInConsole()) { |
134
|
7 |
|
$this->commands([ |
135
|
7 |
|
Console\AlphabetGenerateCommand::class, |
136
|
7 |
|
Console\OptimusGenerateCommand::class, |
137
|
7 |
|
]); |
138
|
7 |
|
} |
139
|
7 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the services provided by the provider. |
143
|
|
|
* |
144
|
|
|
* @return string[] |
145
|
|
|
*/ |
146
|
2 |
|
public function provides() |
147
|
|
|
{ |
148
|
2 |
|
return array_merge( |
149
|
2 |
|
array_keys($bindings = $this->getSingletonBindings()), |
150
|
2 |
|
array_values($bindings) |
151
|
2 |
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.