Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @before hook #242

Merged
merged 1 commit into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Compiler
'MacroStop',
'TaskStart',
'TaskStop',
'Before',
'BeforeStop',
'After',
'AfterStop',
'Finished',
Expand Down Expand Up @@ -334,6 +336,30 @@ protected function compileTaskStop($value)
return preg_replace($pattern, '$1<?php $__container->endTask(); ?>$2', $value);
}

/**
* Compile Envoy before statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileBefore($value)
{
$pattern = $this->createPlainMatcher('before');

return preg_replace($pattern, '$1<?php $_vars = get_defined_vars(); $__container->before(function($task) use ($_vars) { extract($_vars, EXTR_SKIP) ; $2', $value);
}

/**
* Compile Envoy before stop statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileBeforeStop($value)
{
return preg_replace($this->createPlainMatcher('endbefore'), '$1}); ?>$2', $value);
}

/**
* Compile Envoy after statements into valid PHP.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Console/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ protected function runTask($container, $task)
return;
}

foreach ($container->getBeforeCallbacks() as $callback) {
call_user_func($callback, $task);
}

if (($exitCode = $this->runTaskOverSSH($container->getTask($task, $macroOptions))) > 0) {
foreach ($container->getErrorCallbacks() as $callback) {
call_user_func($callback, $task);
Expand Down
28 changes: 28 additions & 0 deletions src/TaskContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class TaskContainer
*/
protected $error = [];

/**
* All of the "before" callbacks.
*
* @var array
*/
protected $before = [];

/**
* All of the "after" callbacks.
*
Expand Down Expand Up @@ -446,6 +453,27 @@ public function endTask()
}
}

/**
* Register a before-task callback.
*
* @param \Closure $callback
* @return void
*/
public function before(Closure $callback)
{
$this->before[] = $callback;
}

/**
* Get all of the before-task callbacks.
*
* @return array
*/
public function getBeforeCallbacks()
{
return $this->before;
}

/**
* Register an after-task callback.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ public function test_it_compiles_finished_statement()

$this->assertSame(1, preg_match('/\$__container->finished\(.*?\}\);/s', $result, $matches));
}

public function test_compile_before_statement()
{
$str = <<<'EOL'
@before
echo "Running {{ $task }} task.";
@endbefore
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame(1, preg_match('/\$__container->before\(.*?\}\);/s', $result, $matches));
}
}