|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ladumor\OneSignal\commands; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\File; |
| 6 | +use Illuminate\Console\Command; |
| 7 | + |
| 8 | +class PublishUserDevice extends Command |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The console command name. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $name = 'one-signal.userDevice:publish'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Publish Migration|Controller|Service of User Device APIs'; |
| 23 | + |
| 24 | + public $composer; |
| 25 | + /** |
| 26 | + * Create a new command instance. |
| 27 | + */ |
| 28 | + public function __construct() |
| 29 | + { |
| 30 | + parent::__construct(); |
| 31 | + |
| 32 | + $this->composer = app()['composer']; |
| 33 | + } |
| 34 | + public function handle() |
| 35 | + { |
| 36 | + $controllerDir = app_path('Http/Controllers/API'); |
| 37 | + if (!File::isDirectory($controllerDir)) { |
| 38 | + File::makeDirectory($controllerDir); |
| 39 | + } |
| 40 | + |
| 41 | + $controllerTemplate = file_get_contents(__DIR__.'/../stubs/UserDeviceAPIController.stub'); |
| 42 | + $this->createFile($controllerDir. DIRECTORY_SEPARATOR, 'UserDeviceAPIController.php', $controllerTemplate); |
| 43 | + $this->info('UserDeviceController published.'); |
| 44 | + |
| 45 | + $modelDir = app_path('Models'); |
| 46 | + |
| 47 | + if (!File::isDirectory($modelDir)) { |
| 48 | + File::makeDirectory($modelDir); |
| 49 | + } |
| 50 | + |
| 51 | + $modelTemplate = file_get_contents(__DIR__.'/../stubs/UserDevice.stub'); |
| 52 | + $this->createFile($modelDir. DIRECTORY_SEPARATOR, 'UserDevice.php', $modelTemplate); |
| 53 | + $this->info('UserDevice published.'); |
| 54 | + |
| 55 | + $repoDir = app_path('Repositories'); |
| 56 | + |
| 57 | + if (!File::isDirectory($repoDir)) { |
| 58 | + File::makeDirectory($repoDir); |
| 59 | + } |
| 60 | + |
| 61 | + $repoTemplate = file_get_contents(__DIR__.'/../stubs/UserDeviceRepository.stub'); |
| 62 | + $this->createFile($repoDir. DIRECTORY_SEPARATOR, 'UserDeviceRepository.php', $repoTemplate); |
| 63 | + $this->info('UserDeviceRepository published.'); |
| 64 | + |
| 65 | + $fileName = date('Y_m_d_His').'_'.'create_user_device_table.php'; |
| 66 | + |
| 67 | + $repoTemplate = file_get_contents(__DIR__.'/../stubs/CreateUserDeviceTable.stub'); |
| 68 | + $this->createFile(base_path('database/migrations/'), $fileName, $repoTemplate); |
| 69 | + $this->info('UserDevice migration created.'); |
| 70 | + |
| 71 | + $this->info('Generating autoload files'); |
| 72 | + $this->composer->dumpOptimized(); |
| 73 | + |
| 74 | + if ($this->confirm("\nDo you want to migrate database? [y|N]", false)) { |
| 75 | + $this->call('migrate'); |
| 76 | + } |
| 77 | + |
| 78 | + $this->info('Greeting From Shailesh Ladumor!'); |
| 79 | + } |
| 80 | + |
| 81 | + public static function createFile($path, $fileName, $contents) |
| 82 | + { |
| 83 | + if (!file_exists($path)) { |
| 84 | + mkdir($path, 0755, true); |
| 85 | + } |
| 86 | + |
| 87 | + $path = $path.$fileName; |
| 88 | + |
| 89 | + file_put_contents($path, $contents); |
| 90 | + } |
| 91 | +} |
0 commit comments