Skip to content

Commit

Permalink
Merge pull request #12 from chrishalbert/fullMigrationHookSupport
Browse files Browse the repository at this point in the history
Full migration hook support
  • Loading branch information
chrishalbert authored Jul 20, 2018
2 parents c5aeba4 + c37d731 commit e4fa163
Show file tree
Hide file tree
Showing 10 changed files with 439 additions and 33 deletions.
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ Now when you create migrations, you should see your stubbed migration using your
* Use Case: After a migration is generated, you want to remind the user to add schema changes to the release notes.

### Usage
Open up the nomadic.php and add 2 different types of hooks. You can pass a closure, or you can pass a NomadicHookInterface.
The benefit of passing the NomadicHookInterface, is that you get the same data passed to the create(). These arguments are
passed to the execute() method, which is what is called.
Open up the nomadic.php where you can add 2 different types of hooks.

#### Create Hooks
These are hooks used when you run `php artisan make:migration`. These hooks can ONLY be defined here in the config file. You can pass a closure, or you can pass a NomadicHookInterface. The benefit of passing the NomadicCreateHookInterface, is that you get the same data passed to the create(). These arguments are
passed to the execute() method, which is what is called.
```
return [
'hooks' => [
Expand All @@ -137,12 +139,36 @@ return [
},
],
'postCreate' => [
new HookInterfaceImplementation()
new NomadicCreateHookInterfaceImplementation()
]
],
];
```

#### Migration Hooks
These hooks are excuted when the migration runs. The construct hook can only be set in the config file. Everything else can be set here in the configuration
or modified at runtime.
```
return [
// Hooks executed with the migrations
'hooks' => [
'construct' => [ // Can only be defined in the configs
],
'preMigrate' => [ // Executed before up()
],
'postMigrate' => [ // Executed after up()
],
'preRollback' => [ // Executed before rolling down()
],
'postRollback' => [ // Executed after rolling down()
],
'destruct' => [ // Executed during destruction
]
],
];
```


## Feature Requests/Bugs
Submit feature requests or bugs to [laravel-nomadic Issues](https://github.com/chrishalbert/laravel-nomadic/issues).

I know that there are some good ideas out there!
16 changes: 16 additions & 0 deletions src/Hooks/NomadicBaseHookInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ChrisHalbert\LaravelNomadic\Hooks;

/**
* Interface NomadicBaseHookInterface
* @package ChrisHalbert\LaravelNomadic\Hooks
*/
interface NomadicBaseHookInterface
{
/**
* Executes a function with parameters the create receives.
* @return string
*/
public function execute();
}
12 changes: 12 additions & 0 deletions src/Hooks/NomadicCreateHookInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ChrisHalbert\LaravelNomadic\Hooks;

/**
* Interface NomadicCreateHookInterface
* This is a placeholder until NomadicHookInterface is deprecated in favor of this more descriptive interface name.
* @package ChrisHalbert\LaravelNomadic\Hooks
*/
interface NomadicCreateHookInterface extends NomadicHookInterface, NomadicBaseHookInterface
{
}
22 changes: 22 additions & 0 deletions src/Hooks/NomadicMigrationHookInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ChrisHalbert\LaravelNomadic\Hooks;

/**
* Interface NomadicBaseHookInterface
* @package ChrisHalbert\LaravelNomadic\Hooks
*/
interface NomadicMigrationHookInterface extends NomadicBaseHookInterface
{
/**
* Executes a function with parameters the create receives.
* @param string $name The name of the migration.
* @param string $path The path.
* @param string $table The name of the table.
* @param boolean $create Whether to use create stub.
* @param string $className The generated name of hte class.
* @param string $filePath The full path to the file.
* @return string
*/
public function execute($name = '', $path = '', $table = null, $create = false, $className = '', $filePath = '');
}
162 changes: 162 additions & 0 deletions src/NomadicMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ChrisHalbert\LaravelNomadic;

use ChrisHalbert\LaravelNomadic\Hooks\NomadicMigrationHookInterface;
use Illuminate\Database\Migrations\Migration;

/**
Expand All @@ -10,6 +11,18 @@
*/
abstract class NomadicMigration extends Migration
{
const CONSTRUCT = 'construct';

const PRE_MIGRATE = 'preMigrate';

const POST_MIGRATE = 'postMigrate';

const PRE_ROLLBACK = 'preRollback';

const POST_ROLLBACK = 'postRollback';

const DESTRUCT = 'destruct';

/**
* Additional properties or column names in your migration table.
* @var array
Expand All @@ -28,15 +41,83 @@ abstract class NomadicMigration extends Migration
*/
protected $fileName;

/**
* Migration hooks.
* @var array
*/
private $migrationHooks = [
self::CONSTRUCT => [],
self::PRE_MIGRATE => [],
self::POST_MIGRATE => [],
self::PRE_ROLLBACK => [],
self::POST_ROLLBACK => [],
self::DESTRUCT => []
];

/**
* NomadicMigration constructor.
* @param NomadicRepositoryInterface $repository The repository.
* @throws \Exception If the hook isset not as an array though.
*/
public function __construct(NomadicRepositoryInterface $repository)
{
$this->properties = array();
$this->repository = $repository;
$this->fileName = basename((new \ReflectionClass($this))->getFileName(), '.php');
$configHooks = config('nomadic.hooks');
foreach ($this->migrationHooks as $hook => &$values) {
if (isset($configHooks[$hook]) && !is_array($configHooks[$hook])) {
throw new \Exception("Configs for nomadic hook `{$hook}` must be an array.");
}

if (isset($configHooks[$hook])) {
$values = $configHooks[$hook];
}
}

$this->runHooks(self::CONSTRUCT);
}

/**
* Runs the up with the hooks.
* @return void
*/
public function up()
{
$this->runHooks(self::PRE_MIGRATE);
$this->migrate();
$this->runHooks(self::POST_MIGRATE);
}

/**
* Runs the down with the hooks.
* @return void
*/
public function down()
{
$this->runHooks(self::PRE_ROLLBACK);
$this->rollback();
$this->runHooks(self::POST_ROLLBACK);
}

/**
* Placeholder to define migrations. Not an abstract method in case the developer
* chooses to override the up() and to ensure backwards compatibility.
* @return void
*/
public function migrate()
{
return;
}

/**
* Placeholder to define rollback. Not an abstract method in case the developer
* chooses to override the down() and to ensure backwards compatibility.
* @return void
*/
public function rollback()
{
return;
}

/**
Expand Down Expand Up @@ -79,6 +160,87 @@ public function getProperties($persist = false)
return $this->properties;
}

/**
* Adds a hook to a specific hook type.
* @param string $name Name of the hook.
* @param NomadicMigrationHookInterface $hook The hook being added.
* @return void
* @throws \Exception If the hook is not an instance of a NomadicMigrationHookInterface.
*/
public function addHook($name, NomadicMigrationHookInterface $hook)
{
$this->verifyValidHook($name);

$this->migrationHooks[$name][] = $hook;
}

/**
* Returns the hooks for a hook type.
* @param string $name Name of the hook.
* @return array
* @throws \Exception If the hook is not an instance of a NomadicMigrationHookInterface.
*/
public function getHooks($name)
{
$this->verifyValidHook($name);

return $this->migrationHooks[$name];
}

/**
* Clears certain hooks.
* @param string $name Name of a hook.
* @return void
* @throws \Exception If the hook is not an instance of a NomadicMigrationHookInterface.
*/
public function clearHooks($name)
{
$this->verifyValidHook($name);
$this->migrationHooks[$name] = [];
}

/**
* Destructor for the class.
*/
public function __destruct()
{
$this->runHooks(self::DESTRUCT);
}

/**
* Verifies whether or not the hook is a valid type.
* @param string $name Name of a hook.
* @throws \Exception If the hook is not an instance of a NomadicMigrationHookInterface.
*/
protected function verifyValidHook($name)
{
if (!isset($this->migrationHooks[$name])) {
throw new \Exception("Invalid migration hook `{$name}`.");
}
}

/**
* Runs an array of hooks.
* @param string $name Name of a hook type.
* @throws \Exception If the hook is not an instance of a NomadicMigrationHookInterface.
*/
protected function runHooks($name)
{
$this->verifyValidHook($name);
foreach ($this->migrationHooks[$name] as $hook) {
$this->runHook($hook);
}
}

/**
* Runs a hook.
* @param NomadicMigrationHookInterface $hook A hook to execute.
*/
protected function runHook(NomadicMigrationHookInterface $hook)
{
$hook->execute();
}

/**
* Reassigns the properties with the database.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/NomadicMigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected function appendTraits($stub, $traits)
return $stub;
}

protected function firePostCreateHooks()
protected function firePostCreateHooks($table = null)
{
$this->fireHook($this->postCreate);
}
Expand Down
26 changes: 23 additions & 3 deletions src/nomadic.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,33 @@
'traits' => [
],

// Pass in hooks that execute before a migration is created, and after a migration is created
// Variety of hooks
// preCreate, postCreate: Executed with `php artisan make:migration`, must be defined here
// other hooks: Executed with the migrations
'hooks' => [
'preCreate' => [

// Runs before a migration is created
],
'postCreate' => [

// Runs after a migration is created
],
'construct' => [
// Runs after the parent constructor
],
'preMigrate' => [
// Runs before the migration code
],
'postMigrate' => [
// Runs after the migration code
],
'preRollback' => [
// Runs before the rollback
],
'postRollback' => [
// Runs after the rollback
],
'destruct' => [
// Runs before the parent destructor
]
],
];
4 changes: 2 additions & 2 deletions src/stubs/blank.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DummyClass extends NomadicMigration
/**
* @return void
*/
public function up()
public function migrate()
{
// Use $this->setProperty($migrationColumn, $insertedIds);
// The $migrationColumn MUST be added to the configs/nomadic.php
Expand All @@ -21,7 +21,7 @@ class DummyClass extends NomadicMigration
*
* @return void
*/
public function down()
public function rollback()
{
// And now you can $insertedIds = $this->getProperty($migrationColumn) and delete
}
Expand Down
Loading

0 comments on commit e4fa163

Please sign in to comment.