Skip to content

Commit

Permalink
Merge pull request #9 from chrishalbert/exposeHooks
Browse files Browse the repository at this point in the history
[exposeHooks] Add the class name and file path
  • Loading branch information
chrishalbert authored May 19, 2018
2 parents 0731b97 + 396cf0c commit 2bff22e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
12 changes: 7 additions & 5 deletions src/Hooks/NomadicHookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ interface NomadicHookInterface
{
/**
* 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 $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);
public function execute($name = '', $path = '', $table = null, $create = false, $className = '', $filePath = '');
}
51 changes: 50 additions & 1 deletion src/NomadicMigrationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,30 @@
*/
class NomadicMigrationCreator extends MigrationCreator
{
/**
* Use statement.
* @const string
*/
const USE_STUB = "use %s;";

/**
* Exception message.
* @const string
*/
const INVALID_HOOK = "Hook must be an instance of a %s or %s, `%s` given.";

/**
* Name of the class being created.
* @var string|null
*/
protected $className = null;

/**
* Full path to the file of the migration.
* @var string|null
*/
protected $filePath = null;

/**
* The registered pre create hooks.
*
Expand Down Expand Up @@ -73,12 +93,41 @@ public function afterCreateExecute($callback, $params = null)
*/
public function create($name, $path, $table = null, $create = false)
{
$params = [$name, $path, $table, $create];
$params = [$name, $path, $table, $create, $this->getClassName($name), $this->getPath($name, $path)];
$this->registerHooks($params);
$this->firePreCreateHooks();
return parent::create($name, $path, $table, $create);
}

/**
* Gets the class name.
* @param string $name
* @return null|string
*/
protected function getClassName($name)
{
if (!isset($this->className)) {
$this->className = parent::getClassName($name);
}

return $this->className;
}

/**
* Gets the file path.
* @param string $name
* @param string $path
* @return string
*/
protected function getPath($name, $path)
{
if (!isset($this->filePath)) {
$this->filePath = parent::getPath($name, $path);
}

return $this->filePath;
}

protected function appendHook(&$hookStack, $callback, $params)
{
// Maintain backwards compatability
Expand Down
10 changes: 5 additions & 5 deletions tests/NomadicMigrationCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testCreateWithHooks()
};

$this->creator->beforeCreateExecute($beforeCallback); // first
$this->creator->afterCreateExecute($hook, ['afterName', '', 'afterTable', true]); // third
$this->creator->afterCreateExecute($hook, ['afterName', '', 'afterTable', true, 'PostHook', '/file/PostHook.php']); // third
$this->creator->beforeCreateExecute($hook, ['beforeName', 'beforePath']); // second
$this->creator->afterCreateExecute($afterCallback); // fourth

Expand All @@ -86,10 +86,10 @@ public function testCreateWithHooks()
$invocations = $spy->getInvocations();

$this->assertEquals(4, count($invocations));
$this->assertEquals(['beforeCallback', '', null, false], $invocations[0]->parameters);
$this->assertEquals(['beforeName', 'beforePath', null, false], $invocations[1]->parameters);
$this->assertEquals(['afterName', '', 'afterTable', true], $invocations[2]->parameters);
$this->assertEquals(['afterCallback', '', null, false], $invocations[3]->parameters);
$this->assertEquals(['beforeCallback', '', null, false, '', ''], $invocations[0]->parameters);
$this->assertEquals(['beforeName', 'beforePath', null, false, '', ''], $invocations[1]->parameters);
$this->assertEquals(['afterName', '', 'afterTable', true, 'PostHook', '/file/PostHook.php'], $invocations[2]->parameters);
$this->assertEquals(['afterCallback', '', null, false, '', ''], $invocations[3]->parameters);
}

public function tearDown()
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TestHookConfig implements NomadicHookInterface
{
public function execute($name = '', $path = '', $table = null, $create = false)
public function execute($name = '', $path = '', $table = null, $create = false, $className = '', $filePath = '')
{}
}

Expand Down

0 comments on commit 2bff22e

Please sign in to comment.