From 2dc329022a8999e323196cee42ac2a2a0221dc2e Mon Sep 17 00:00:00 2001 From: Dipesh Sukhia Date: Sun, 25 Apr 2021 18:20:28 +0530 Subject: [PATCH] laravel 8 support --- README.md | 2 +- ...velCountryStateCityDataServiceProvider.php | 63 +++++++++++-------- .../CountryStateCityDataProvider.php | 0 ...000000_create_country_state_city_table.php | 10 +-- .../stubs/CountryStateCityTableSeeder.stub} | 12 ++-- .../stubs/Models/City.stub} | 2 +- .../stubs/Models/Country.stub} | 2 +- .../stubs/Models/State.stub} | 2 +- 8 files changed, 51 insertions(+), 42 deletions(-) rename src/{ => resources}/DataProviders/CountryStateCityDataProvider.php (100%) rename src/{ => resources}/migrations/2014_02_04_000000_create_country_state_city_table.php (89%) rename src/{seeds/CountryStateCityTableSeeder.php => resources/stubs/CountryStateCityTableSeeder.stub} (79%) rename src/{Models/City.php => resources/stubs/Models/City.stub} (90%) rename src/{Models/Country.php => resources/stubs/Models/Country.stub} (90%) rename src/{Models/State.php => resources/stubs/Models/State.stub} (92%) diff --git a/README.md b/README.md index 1ba7d9e..e1b1f96 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Quality Score](https://img.shields.io/scrutinizer/g/dipeshsukhia/laravel-country-state-city-data.svg?style=flat-square)](https://scrutinizer-ci.com/g/dipeshsukhia/laravel-country-state-city-data) [![Total Downloads](https://img.shields.io/packagist/dt/dipeshsukhia/laravel-country-state-city-data.svg?style=flat-square)](https://packagist.org/packages/dipeshsukhia/laravel-country-state-city-data) -World`s Country State City Provider for Laravel 5.x, 6.x, 7.x +World`s Country State City Provider for Laravel 5.x, 6.x, 7.x, 8.x ## Installation diff --git a/src/LaravelCountryStateCityDataServiceProvider.php b/src/LaravelCountryStateCityDataServiceProvider.php index 68a56a7..78a9e74 100644 --- a/src/LaravelCountryStateCityDataServiceProvider.php +++ b/src/LaravelCountryStateCityDataServiceProvider.php @@ -6,6 +6,7 @@ class LaravelCountryStateCityDataServiceProvider extends ServiceProvider { + const STUB_DIR = __DIR__.'/resources/stubs/'; /** * Bootstrap the application services. */ @@ -14,38 +15,43 @@ public function boot() /* * Optional methods to load your package assets */ - // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'laravel-country-state-city-data'); - // $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-country-state-city-data'); - // $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); - // $this->loadRoutesFrom(__DIR__.'/routes.php'); if ($this->app->runningInConsole()) { + /* model */ + if(is_dir(base_path('app/Models'))){ + $modelDir = base_path('app/Models'); + $modelNameSpace = "\\Models"; + }else{ + $modelDir = base_path('app'); + $modelNameSpace = ""; + } - $this->publishes([ - __DIR__.'/migrations' => database_path('migrations'), - __DIR__.'/Models' => app_path(), - __DIR__.'/DataProviders' => app_path('DataProviders'), - __DIR__.'/seeds' => database_path('seeds'), - ], 'LaravelCountryStateCityData'); + foreach (['Country','State','City'] as $modelName){ + $ModelTemplate = self::getStubContents("Models/{$modelName}.stub"); + $ModelTemplate = str_replace('{{modelNameSpace}}', $modelNameSpace, $ModelTemplate); + file_put_contents($modelDir."/{$modelName}.php", $ModelTemplate); + } - /*$this->publishes([ - __DIR__.'/../config/config.php' => config_path('laravel-country-state-city-data.php'), - ], 'config');*/ + /* model */ - // Publishing the views. - /*$this->publishes([ - __DIR__.'/../resources/views' => resource_path('views/vendor/laravel-country-state-city-data'), - ], 'views');*/ + /* seeders */ + if(is_dir(database_path('seeds'))){ + $seedDir = database_path('seeds'); + $seederNameSpace = "Seeder"; + }else{ + $seedDir = database_path('seeders'); + $seederNameSpace = "Seeders"; + } + $seederTemplate = self::getStubContents('CountryStateCityTableSeeder.stub'); + $seederTemplate = str_replace('{{seederNameSpace}}', $seederNameSpace, $seederTemplate); + $seederTemplate = str_replace('{{modelNameSpace}}', $modelNameSpace, $seederTemplate); + file_put_contents($seedDir.'/CountryStateCityTableSeeder.php', $seederTemplate); + /* seeders */ - // Publishing assets. - /*$this->publishes([ - __DIR__.'/../resources/assets' => public_path('vendor/laravel-country-state-city-data'), - ], 'assets');*/ - - // Publishing the translation files. - /*$this->publishes([ - __DIR__.'/../resources/lang' => resource_path('lang/vendor/laravel-country-state-city-data'), - ], 'lang');*/ + $this->publishes([ + __DIR__.'/resources/migrations' => database_path('migrations'), + __DIR__.'/resources/DataProviders' => app_path('DataProviders'), + ], 'LaravelCountryStateCityData'); // Registering package commands. // $this->commands([]); @@ -65,4 +71,9 @@ public function register() return new LaravelCountryStateCityData; });*/ } + + private function getStubContents($stubName) + { + return file_get_contents(self::STUB_DIR.$stubName); + } } diff --git a/src/DataProviders/CountryStateCityDataProvider.php b/src/resources/DataProviders/CountryStateCityDataProvider.php similarity index 100% rename from src/DataProviders/CountryStateCityDataProvider.php rename to src/resources/DataProviders/CountryStateCityDataProvider.php diff --git a/src/migrations/2014_02_04_000000_create_country_state_city_table.php b/src/resources/migrations/2014_02_04_000000_create_country_state_city_table.php similarity index 89% rename from src/migrations/2014_02_04_000000_create_country_state_city_table.php rename to src/resources/migrations/2014_02_04_000000_create_country_state_city_table.php index 9195482..17ba233 100644 --- a/src/migrations/2014_02_04_000000_create_country_state_city_table.php +++ b/src/resources/migrations/2014_02_04_000000_create_country_state_city_table.php @@ -15,7 +15,7 @@ class CreateCountryStateCityTable extends Migration public function up() { Schema::create('countries', function (Blueprint $table) { - $table->increments('id'); + $table->bigIncrements('id'); $table->string('name',255); $table->enum('status', ['active', 'inactive'])->default('active'); $table->timestamp('created_at')->useCurrent(); @@ -24,8 +24,8 @@ public function up() }); Schema::create('states', function (Blueprint $table) { - $table->increments('id'); - $table->unsignedInteger('country_id'); + $table->bigIncrements('id'); + $table->unsignedBigInteger('country_id'); $table->string('name',255); $table->enum('status', ['active', 'inactive'])->default('active'); $table->timestamp('created_at')->useCurrent(); @@ -37,8 +37,8 @@ public function up() }); Schema::create('cities', function (Blueprint $table) { - $table->increments('id'); - $table->unsignedInteger('state_id'); + $table->bigIncrements('id'); + $table->unsignedBigInteger('state_id'); $table->string('name',255); $table->enum('status', ['active', 'inactive'])->default('active'); $table->timestamp('created_at')->useCurrent(); diff --git a/src/seeds/CountryStateCityTableSeeder.php b/src/resources/stubs/CountryStateCityTableSeeder.stub similarity index 79% rename from src/seeds/CountryStateCityTableSeeder.php rename to src/resources/stubs/CountryStateCityTableSeeder.stub index fa5cd9d..0bec405 100644 --- a/src/seeds/CountryStateCityTableSeeder.php +++ b/src/resources/stubs/CountryStateCityTableSeeder.stub @@ -1,12 +1,12 @@ chunk(15000) as $chunkCities){ City::insert($chunkCities->toArray()); } } -} \ No newline at end of file +} diff --git a/src/Models/City.php b/src/resources/stubs/Models/City.stub similarity index 90% rename from src/Models/City.php rename to src/resources/stubs/Models/City.stub index a22c450..478f445 100644 --- a/src/Models/City.php +++ b/src/resources/stubs/Models/City.stub @@ -1,6 +1,6 @@