Skip to content

Commit 2bff22e

Browse files
authored
Merge pull request #9 from chrishalbert/exposeHooks
[exposeHooks] Add the class name and file path
2 parents 0731b97 + 396cf0c commit 2bff22e

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

src/Hooks/NomadicHookInterface.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ interface NomadicHookInterface
1010
{
1111
/**
1212
* Executes a function with parameters the create receives.
13-
* @param string $name The name of the migration.
14-
* @param string $path The path.
15-
* @param string $table The name of the table.
16-
* @param boolean $create Whether to use create stub.
13+
* @param string $name The name of the migration.
14+
* @param string $path The path.
15+
* @param string $table The name of the table.
16+
* @param boolean $create Whether to use create stub.
17+
* @param string $className The generated name of hte class.
18+
* @param string $filePath The full path to the file.
1719
* @return string
1820
*/
19-
public function execute($name = '', $path = '', $table = null, $create = false);
21+
public function execute($name = '', $path = '', $table = null, $create = false, $className = '', $filePath = '');
2022
}

src/NomadicMigrationCreator.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,30 @@
1212
*/
1313
class NomadicMigrationCreator extends MigrationCreator
1414
{
15+
/**
16+
* Use statement.
17+
* @const string
18+
*/
1519
const USE_STUB = "use %s;";
1620

21+
/**
22+
* Exception message.
23+
* @const string
24+
*/
1725
const INVALID_HOOK = "Hook must be an instance of a %s or %s, `%s` given.";
1826

27+
/**
28+
* Name of the class being created.
29+
* @var string|null
30+
*/
31+
protected $className = null;
32+
33+
/**
34+
* Full path to the file of the migration.
35+
* @var string|null
36+
*/
37+
protected $filePath = null;
38+
1939
/**
2040
* The registered pre create hooks.
2141
*
@@ -73,12 +93,41 @@ public function afterCreateExecute($callback, $params = null)
7393
*/
7494
public function create($name, $path, $table = null, $create = false)
7595
{
76-
$params = [$name, $path, $table, $create];
96+
$params = [$name, $path, $table, $create, $this->getClassName($name), $this->getPath($name, $path)];
7797
$this->registerHooks($params);
7898
$this->firePreCreateHooks();
7999
return parent::create($name, $path, $table, $create);
80100
}
81101

102+
/**
103+
* Gets the class name.
104+
* @param string $name
105+
* @return null|string
106+
*/
107+
protected function getClassName($name)
108+
{
109+
if (!isset($this->className)) {
110+
$this->className = parent::getClassName($name);
111+
}
112+
113+
return $this->className;
114+
}
115+
116+
/**
117+
* Gets the file path.
118+
* @param string $name
119+
* @param string $path
120+
* @return string
121+
*/
122+
protected function getPath($name, $path)
123+
{
124+
if (!isset($this->filePath)) {
125+
$this->filePath = parent::getPath($name, $path);
126+
}
127+
128+
return $this->filePath;
129+
}
130+
82131
protected function appendHook(&$hookStack, $callback, $params)
83132
{
84133
// Maintain backwards compatability

tests/NomadicMigrationCreatorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testCreateWithHooks()
6767
};
6868

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

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

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

9595
public function tearDown()

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class TestHookConfig implements NomadicHookInterface
88
{
9-
public function execute($name = '', $path = '', $table = null, $create = false)
9+
public function execute($name = '', $path = '', $table = null, $create = false, $className = '', $filePath = '')
1010
{}
1111
}
1212

0 commit comments

Comments
 (0)