Skip to content

Commit

Permalink
Moved require'd() handling into Context.
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Mar 10, 2017
1 parent f31a84a commit 7b02d70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
19 changes: 19 additions & 0 deletions src/Task/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Deployer\Server\Environment;
use Deployer\Server\ServerInterface;
use Deployer\Exception\Exception;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -69,6 +70,24 @@ public static function get()
return end(self::$contexts);
}

/**
* Returns the current context when available.
*
* Throws a Exception when not called within a task-context and therefore no Context is available.
*
* This method provides a usefull error to the end-user to make him/her aware
* to use a function in the required task-context.
*
* @throws Exception
*/
public static function required($callerName)
{
if (!self::get()) {
throw new Exception("'$callerName' can only be used within a 'task()'-function!");
}
}


/**
* @return Context
*/
Expand Down
22 changes: 4 additions & 18 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Deployer\Task\GroupTask;
use Deployer\Type\Result;
use Deployer\Cluster\ClusterFactory;
use Deployer\Exception\Exception;
use Monolog\Logger;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
Expand Down Expand Up @@ -561,7 +560,7 @@ function has($name)
*/
function ask($message, $default = null, $suggestedChoices = null)
{
requiresTaskContext(__FUNCTION__);
Context::required(__FUNCTION__);

if (($suggestedChoices !== null) && (empty($suggestedChoices))) {
throw new \InvalidArgumentException('Suggested choices should not be empty');
Expand Down Expand Up @@ -594,7 +593,7 @@ function ask($message, $default = null, $suggestedChoices = null)
*/
function askChoice($message, array $availableChoices, $default = null, $multiselect = false)
{
requiresTaskContext(__FUNCTION__);
Context::required(__FUNCTION__);

if (empty($availableChoices)) {
throw new \InvalidArgumentException('Available choices should not be empty');
Expand Down Expand Up @@ -629,7 +628,7 @@ function askChoice($message, array $availableChoices, $default = null, $multisel
*/
function askConfirmation($message, $default = false)
{
requiresTaskContext(__FUNCTION__);
Context::required(__FUNCTION__);

if (isQuiet()) {
return $default;
Expand All @@ -652,7 +651,7 @@ function askConfirmation($message, $default = false)
*/
function askHiddenResponse($message)
{
requiresTaskContext(__FUNCTION__);
Context::required(__FUNCTION__);

if (isQuiet()) {
return '';
Expand Down Expand Up @@ -751,16 +750,3 @@ function parse($value)
{
return Context::get()->getEnvironment()->parse($value);
}

/**
* Throws a Exception when not called within a task-context.
*
* This method provides a usefull error to the end-user to make him/her aware
* to use a function in the required task-context.
*/
function requiresTaskContext($callerName)
{
if (!Context::get()) {
throw new Exception("'$callerName' can only be used within a 'task()'-function!");
}
}

0 comments on commit 7b02d70

Please sign in to comment.