Skip to content

Commit

Permalink
Allow custom migrations (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosancao authored Oct 30, 2020
1 parent 941a430 commit 7c5422d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ Instead of adding ``` ShortUrl::routes(); ``` you can call three separates metho

this allows you to add middlewares or prefix routes.

### Migration Customization

If you are not going to use Short Url's default migrations, you should call the
`ShortUrl::ignoreMigrations();` method in the `register` method of your `AppServiceProvider`.
You may export the default migrations using

```
php artisan vendor:publish --tag=shorturl-migrations
```

## Nice!

Laravel short url is now set up on your homepage.
Expand Down
17 changes: 17 additions & 0 deletions src/ShortUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class ShortUrl
{
/**
* Indicates if migrations will be run.
*
* @var bool
*/
public static $runsMigrations = true;

/**
* Register the routes to create urls.
*
Expand Down Expand Up @@ -49,4 +56,14 @@ public function routes()
$this->manageRoutes();
$this->redirectRoute();
}

/**
* Configure package to not register its migrations.
*
* @return static
*/
public static function ignoreMigrations()
{
static::$runsMigrations = false;
}
}
18 changes: 17 additions & 1 deletion src/ShortUrlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public function boot()
$this->loadViewsFrom(__DIR__.'/../resources/views', 'shorturl');

if ($this->app->runningInConsole()) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->registerMigrations();

$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
], 'shorturl-migrations');

$this->publishes([
__DIR__.'/../config/shorturl.php' => config_path('shorturl.php'),
Expand All @@ -34,6 +38,18 @@ public function boot()
}
}

/**
* Register migration files.
*
* @return void
*/
protected function registerMigrations()
{
if (ShortUrl::$runsMigrations) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}

/**
* Register the application services.
*
Expand Down

0 comments on commit 7c5422d

Please sign in to comment.