Maintain local ⭤ remote in sync with automatic AT Protocol parity for Laravel (alpha & unstable)
at dev 3.1 kB view raw
1<?php 2 3namespace SocialDept\AtpParity; 4 5use Illuminate\Support\ServiceProvider; 6use SocialDept\AtpParity\Commands\DiscoverCommand; 7use SocialDept\AtpParity\Commands\ExportCommand; 8use SocialDept\AtpParity\Commands\ImportCommand; 9use SocialDept\AtpParity\Commands\ImportStatusCommand; 10use SocialDept\AtpParity\Discovery\DiscoveryService; 11use SocialDept\AtpParity\Export\ExportService; 12use SocialDept\AtpParity\Import\ImportService; 13use SocialDept\AtpParity\Publish\PublishService; 14use SocialDept\AtpParity\Support\RecordHelper; 15 16class ParityServiceProvider extends ServiceProvider 17{ 18 public function boot(): void 19 { 20 $this->registerConfiguredMappers(); 21 22 if ($this->app->runningInConsole()) { 23 $this->bootForConsole(); 24 } 25 } 26 27 public function register(): void 28 { 29 $this->mergeConfigFrom(__DIR__.'/../config/parity.php', 'parity'); 30 31 $this->app->singleton(MapperRegistry::class); 32 $this->app->alias(MapperRegistry::class, 'parity'); 33 34 $this->app->singleton(RecordHelper::class, function ($app) { 35 return new RecordHelper($app->make(MapperRegistry::class)); 36 }); 37 38 $this->app->singleton(ImportService::class, function ($app) { 39 return new ImportService($app->make(MapperRegistry::class)); 40 }); 41 42 $this->app->singleton(PublishService::class, function ($app) { 43 return new PublishService($app->make(MapperRegistry::class)); 44 }); 45 46 $this->app->singleton(DiscoveryService::class, function ($app) { 47 return new DiscoveryService($app->make(ImportService::class)); 48 }); 49 50 $this->app->singleton(ExportService::class, function ($app) { 51 return new ExportService( 52 $app->make(MapperRegistry::class), 53 $app->make(ImportService::class) 54 ); 55 }); 56 } 57 58 /** 59 * Register mappers defined in config. 60 */ 61 protected function registerConfiguredMappers(): void 62 { 63 $registry = $this->app->make(MapperRegistry::class); 64 65 foreach (config('parity.mappers', []) as $mapperClass) { 66 if (class_exists($mapperClass)) { 67 $registry->register($this->app->make($mapperClass)); 68 } 69 } 70 } 71 72 protected function bootForConsole(): void 73 { 74 $this->publishes([ 75 __DIR__.'/../config/parity.php' => config_path('parity.php'), 76 ], 'parity-config'); 77 78 $this->publishes([ 79 __DIR__.'/../database/migrations' => database_path('migrations'), 80 ], 'parity-migrations'); 81 82 $this->commands([ 83 DiscoverCommand::class, 84 ExportCommand::class, 85 ImportCommand::class, 86 ImportStatusCommand::class, 87 ]); 88 } 89 90 public function provides(): array 91 { 92 return [ 93 'parity', 94 MapperRegistry::class, 95 RecordHelper::class, 96 ImportService::class, 97 PublishService::class, 98 DiscoveryService::class, 99 ExportService::class, 100 ]; 101 } 102}