|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RajTechnologies\Tools\commands; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\File; |
| 6 | +use Illuminate\Console\Command; |
| 7 | + |
| 8 | +class PWAPublish extends Command |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The console command name. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $name = 'laravel-pwa:publish'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Publish Service Worker|Offline HTMl|manifest file for PWA application.'; |
| 23 | + |
| 24 | + public $composer; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a new command instance. |
| 28 | + */ |
| 29 | + public function __construct() |
| 30 | + { |
| 31 | + parent::__construct(); |
| 32 | + |
| 33 | + $this->composer = app()['composer']; |
| 34 | + } |
| 35 | + |
| 36 | + public function handle() |
| 37 | + { |
| 38 | + $publicDir = public_path(); |
| 39 | + |
| 40 | + $manifestTemplate = file_get_contents(__DIR__.'/../stubs/pwa_app/manifest.stub'); |
| 41 | + $this->createFile($publicDir. DIRECTORY_SEPARATOR, 'manifest.json', $manifestTemplate); |
| 42 | + $this->info('manifest.json file is published.'); |
| 43 | + |
| 44 | + $offlineHtmlTemplate = file_get_contents(__DIR__.'/../stubs/pwa_app/offline.stub'); |
| 45 | + $this->createFile($publicDir. DIRECTORY_SEPARATOR, 'offline.html', $offlineHtmlTemplate); |
| 46 | + $this->info('offline.html file is published.'); |
| 47 | + |
| 48 | + $swTemplate = file_get_contents(__DIR__.'/../stubs/pwa_app/sw.stub'); |
| 49 | + $this->createFile($publicDir. DIRECTORY_SEPARATOR, 'sw.js', $swTemplate); |
| 50 | + $this->info('sw.js (Service Worker) file is published.'); |
| 51 | + |
| 52 | + $this->info('Generating autoload files'); |
| 53 | + $this->composer->dumpOptimized(); |
| 54 | + |
| 55 | + $this->info('Greeting!.. Enjoy PWA site...'); |
| 56 | + } |
| 57 | + |
| 58 | + public static function createFile($path, $fileName, $contents) |
| 59 | + { |
| 60 | + if (!file_exists($path)) { |
| 61 | + mkdir($path, 0755, true); |
| 62 | + } |
| 63 | + |
| 64 | + $path = $path.$fileName; |
| 65 | + |
| 66 | + file_put_contents($path, $contents); |
| 67 | + } |
| 68 | +} |
0 commit comments