1 | <?php |
||
8 | class HashidServiceProvider extends ServiceProvider |
||
9 | { |
||
10 | /** |
||
11 | * Register the service provider. |
||
12 | * |
||
13 | * @return void |
||
14 | */ |
||
15 | 6 | public function register() |
|
22 | |||
23 | /** |
||
24 | * Setup package assets. |
||
25 | * |
||
26 | * @return void |
||
27 | */ |
||
28 | 6 | protected function setupAssets() |
|
29 | { |
||
30 | 6 | if (is_a($this->app, 'Laravel\Lumen\Application')) { |
|
31 | $this->app->configure('hashid'); |
||
32 | } |
||
33 | |||
34 | 6 | $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid'); |
|
35 | |||
36 | 6 | if ($this->app->runningInConsole()) { |
|
37 | 6 | $this->publishes([$config => base_path('config/hashid.php')], 'hashid'); |
|
38 | 6 | } |
|
39 | 6 | } |
|
40 | |||
41 | /** |
||
42 | * Register service bindings. |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | 6 | protected function registerServices() |
|
47 | { |
||
48 | $this->app->singleton('hashid', function ($app) { |
||
49 | 4 | return new HashidManager($app); |
|
50 | 6 | }); |
|
51 | 6 | $this->app->alias('hashid', HashidManager::class); |
|
52 | |||
53 | 6 | foreach ($this->getSingletonDrivers() as $class) { |
|
54 | 6 | $this->app->singleton( |
|
55 | 6 | $key = $this->getBindingKeyForDriver($class), |
|
56 | 6 | function () use ($class) { |
|
57 | 6 | return new $class; |
|
58 | } |
||
59 | 6 | ); |
|
60 | 6 | $this->app->alias($key, $class); |
|
61 | 6 | } |
|
62 | |||
63 | 6 | foreach ($this->getNonSingletonDrivers() as $class) { |
|
64 | 6 | $this->app->bind($this->getBindingKeyForDriver($class), $class); |
|
65 | 6 | } |
|
66 | 6 | } |
|
67 | |||
68 | /** |
||
69 | * Get singleton drivers classes. |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | 6 | protected function getSingletonDrivers() |
|
74 | { |
||
75 | return [ |
||
76 | 6 | Base64Driver::class, |
|
77 | 6 | Base64IntegerDriver::class, |
|
78 | 6 | HexDriver::class, |
|
79 | 6 | HexIntegerDriver::class, |
|
80 | 6 | ]; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Get non-singleton drivers classes. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 6 | protected function getNonSingletonDrivers() |
|
89 | { |
||
90 | return [ |
||
91 | 6 | Base62Driver::class, |
|
92 | 6 | Base62IntegerDriver::class, |
|
93 | 6 | HashidsDriver::class, |
|
94 | 6 | HashidsHexDriver::class, |
|
95 | 6 | HashidsIntegerDriver::class, |
|
96 | 6 | HashidsStringDriver::class, |
|
97 | 6 | OptimusDriver::class, |
|
98 | 6 | ]; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get the binding key for the driver class. |
||
103 | * |
||
104 | * @param string $class |
||
105 | * @return string |
||
106 | */ |
||
107 | 6 | protected function getBindingKeyForDriver($class) |
|
108 | { |
||
109 | 6 | return 'hashid.driver.'.Str::snake( |
|
110 | 6 | preg_replace('#Driver$#', '', class_basename($class)) |
|
111 | 6 | ); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Register console commands. |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | 6 | protected function registerCommands() |
|
128 | } |
||
129 |