|
7 | 7 | use ExponentPhpSDK\ExpoRepository; |
8 | 8 | use Illuminate\Support\ServiceProvider; |
9 | 9 | use ExponentPhpSDK\Repositories\ExpoFileDriver; |
| 10 | +use NotificationChannels\ExpoPushNotifications\Repositories\ExpoDatabaseDriver; |
10 | 11 |
|
11 | 12 | class ExpoPushNotificationsServiceProvider extends ServiceProvider |
12 | 13 | { |
13 | 14 | /** |
14 | 15 | * Bootstrap the application services. |
| 16 | + * |
| 17 | + * @return void |
15 | 18 | */ |
16 | 19 | public function boot() |
17 | 20 | { |
| 21 | + $this->setupConfig(); |
| 22 | + |
| 23 | + $repository = $this->getInterestsDriver(); |
| 24 | + |
| 25 | + $this->shouldPublishMigrations($repository); |
| 26 | + |
18 | 27 | $this->app->when(ExpoChannel::class) |
19 | 28 | ->needs(Expo::class) |
20 | | - ->give(function () { |
21 | | - return new Expo(new ExpoRegistrar(new ExpoFileDriver())); |
| 29 | + ->give(function () use ($repository) { |
| 30 | + return new Expo(new ExpoRegistrar($repository)); |
22 | 31 | }); |
23 | 32 |
|
24 | | - //Load routes |
25 | 33 | $this->loadRoutesFrom(__DIR__.'/Http/routes.php'); |
26 | 34 | } |
27 | 35 |
|
28 | 36 | /** |
29 | 37 | * Register the application services. |
| 38 | + * |
| 39 | + * @return void |
30 | 40 | */ |
31 | 41 | public function register() |
32 | 42 | { |
33 | | - $this->app->bind(ExpoRepository::class, ExpoFileDriver::class); |
| 43 | + $this->app->bind(ExpoRepository::class, get_class($this->getInterestsDriver())); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the Expo repository driver based on config. |
| 48 | + * |
| 49 | + * @return ExpoRepository |
| 50 | + */ |
| 51 | + public function getInterestsDriver() |
| 52 | + { |
| 53 | + $driver = config('exponent-push-notifications.interests.driver'); |
| 54 | + |
| 55 | + switch ($driver) { |
| 56 | + case 'database': |
| 57 | + return new ExpoDatabaseDriver(); |
| 58 | + break; |
| 59 | + default: |
| 60 | + return new ExpoFileDriver(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Publishes the configuration files for the package. |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + protected function setupConfig() |
| 70 | + { |
| 71 | + $this->publishes([ |
| 72 | + __DIR__ . '/../config/exponent-push-notifications.php' => config_path('exponent-push-notifications.php'), |
| 73 | + ], 'config'); |
| 74 | + |
| 75 | + $this->mergeConfigFrom(__DIR__.'/../config/exponent-push-notifications.php', 'exponent-push-notifications'); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Publishes the migration files needed in the package. |
| 80 | + * |
| 81 | + * @param ExpoRepository $repository |
| 82 | + * |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + private function shouldPublishMigrations(ExpoRepository $repository) |
| 86 | + { |
| 87 | + if ($repository instanceof ExpoDatabaseDriver && !class_exists('CreateExponentPushNotificationInterestsTable')) { |
| 88 | + $timestamp = date('Y_m_d_His', time()); |
| 89 | + $this->publishes([ |
| 90 | + __DIR__ . '/../migrations/create_exponent_push_notification_interests_table.php.stub' => database_path("/migrations/{$timestamp}_create_exponent_push_notification_interests_table.php"), |
| 91 | + ], 'migrations'); |
| 92 | + } |
34 | 93 | } |
35 | 94 | } |
0 commit comments