22
33namespace Javaabu \MobileVerification ;
44
5+ use Carbon \Carbon ;
6+ use Illuminate \Support \Str ;
57use Laravel \Passport \Passport ;
68use Illuminate \Support \ServiceProvider ;
79use League \OAuth2 \Server \AuthorizationServer ;
1214
1315class MobileVerificationServiceProvider extends ServiceProvider
1416{
17+ /**
18+ * The package migrations, in order of creation.
19+ *
20+ * @var array|string[]
21+ */
22+ protected array $ migrations = [
23+ 'create_mobile_numbers_table '
24+ ];
25+
1526 /**
1627 * Bootstrap the application services.
1728 */
1829 public function boot ()
1930 {
2031 // declare publishes
2132 if ($ this ->app ->runningInConsole ()) {
22- $ this ->registerMigrations ();
33+ // Publish migrations with current timestamp
34+ foreach ($ this ->migrations as $ migration ) {
35+ $ vendorMigration = __DIR__ . '/../database/migrations/ ' . $ migration . '.php ' ;
36+ $ appMigration = $ this ->generateMigrationName ($ migration , now ()->addSecond ());
2337
24- $ this ->publishes ([
25- __DIR__ .'/../database/migrations ' => database_path ('migrations ' ),
26- ], 'mobile-verification-migrations ' );
38+ $ this ->publishes ([
39+ $ vendorMigration => $ appMigration ,
40+ ], 'mobile-verification-migrations ' );
41+ }
2742
2843 $ this ->publishes ([
2944 __DIR__ . '/../config/mobile-verification.php ' => config_path ('mobile-verification.php ' ),
@@ -38,18 +53,6 @@ public function boot()
3853 $ this ->loadTranslationsFrom (__DIR__ .'/../lang ' , 'mobile-verification ' );
3954 }
4055
41- /**
42- * Register migration files.
43- *
44- * @return void
45- */
46- protected function registerMigrations (): void
47- {
48- if (MobileVerification::$ runsMigrations ) {
49- $ this ->loadMigrationsFrom (__DIR__ .'/../database/migrations ' );
50- }
51- }
52-
5356 /**
5457 * Register the application services.
5558 */
@@ -80,4 +83,28 @@ protected function makeGrant(): MobileGrant
8083
8184 return $ grant ;
8285 }
86+
87+ protected function generateMigrationName (string $ migrationFileName , Carbon $ now ): string
88+ {
89+ $ migrationsPath = 'migrations/ ' . dirname ($ migrationFileName ) . '/ ' ;
90+ $ migrationFileName = basename ($ migrationFileName );
91+
92+ $ len = strlen ($ migrationFileName ) + 4 ;
93+
94+ if (Str::contains ($ migrationFileName , '/ ' )) {
95+ $ migrationsPath .= Str::of ($ migrationFileName )->beforeLast ('/ ' )->finish ('/ ' );
96+ $ migrationFileName = Str::of ($ migrationFileName )->afterLast ('/ ' );
97+ }
98+
99+ foreach (glob (database_path ("{$ migrationsPath }*.php " )) as $ filename ) {
100+ if ((substr ($ filename , -$ len ) === $ migrationFileName . '.php ' )) {
101+ return $ filename ;
102+ }
103+ }
104+
105+ $ timestamp = $ now ->format ('Y_m_d_His ' );
106+ $ migrationFileName = Str::of ($ migrationFileName )->snake ()->finish ('.php ' );
107+
108+ return database_path ($ migrationsPath . $ timestamp . '_ ' . $ migrationFileName );
109+ }
83110}
0 commit comments