From e802bffc566be643a377c14b25f99971b1c19c9b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 10 Mar 2017 09:55:15 +0100 Subject: [PATCH 1/3] Provide a usefull error when ask*() is not used wihtin a task() --- src/functions.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/functions.php b/src/functions.php index 91672706b..d6f6d0a8c 100644 --- a/src/functions.php +++ b/src/functions.php @@ -16,6 +16,8 @@ use Deployer\Task\Context; 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; @@ -23,7 +25,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; -use Deployer\Cluster\ClusterFactory; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -560,6 +561,8 @@ function has($name) */ function ask($message, $default = null, $suggestedChoices = null) { + requiresTaskContext(__FUNCTION__); + if (($suggestedChoices !== null) && (empty($suggestedChoices))) { throw new \InvalidArgumentException('Suggested choices should not be empty'); } @@ -591,6 +594,8 @@ function ask($message, $default = null, $suggestedChoices = null) */ function askChoice($message, array $availableChoices, $default = null, $multiselect = false) { + requiresTaskContext(__FUNCTION__); + if (empty($availableChoices)) { throw new \InvalidArgumentException('Available choices should not be empty'); } @@ -624,6 +629,8 @@ function askChoice($message, array $availableChoices, $default = null, $multisel */ function askConfirmation($message, $default = false) { + requiresTaskContext(__FUNCTION__); + if (isQuiet()) { return $default; } @@ -645,6 +652,8 @@ function askConfirmation($message, $default = false) */ function askHiddenResponse($message) { + requiresTaskContext(__FUNCTION__); + if (isQuiet()) { return ''; } @@ -742,3 +751,16 @@ 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!"); + } +} From f31a84a12b02f9da4195065747f98cfcf8dd3beb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 10 Mar 2017 12:23:11 +0100 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89ec27410..ebdd33754 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add a way to retrieve a defined task [#1008](https://github.com/deployphp/deployer/pull/1008) - Add support for configFile in the NativeSsh implementation [#979](https://github.com/deployphp/deployer/pull/979) - Add `--no-hooks` option for running commands without `before()` and `after()` [#1061](https://github.com/deployphp/deployer/pull/1061) +- Added a usefull error when ask*() is not used wihtin a task() [#1083](https://github.com/deployphp/deployer/pull/1083) ### Changed - Parse hyphens in environment setting names [#1073](https://github.com/deployphp/deployer/pull/1074) From 4b9eb3b9657d8b35606a5c91637257249cfad708 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 10 Mar 2017 15:00:13 +0100 Subject: [PATCH 3/3] Moved require'd() handling into Context. --- src/Task/Context.php | 18 ++++++++++++++++++ src/functions.php | 22 ++++------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Task/Context.php b/src/Task/Context.php index d37efad64..ce5a7897c 100644 --- a/src/Task/Context.php +++ b/src/Task/Context.php @@ -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; @@ -69,6 +70,23 @@ 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 */ diff --git a/src/functions.php b/src/functions.php index d6f6d0a8c..03d81805a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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; @@ -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'); @@ -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'); @@ -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; @@ -652,7 +651,7 @@ function askConfirmation($message, $default = false) */ function askHiddenResponse($message) { - requiresTaskContext(__FUNCTION__); + Context::required(__FUNCTION__); if (isQuiet()) { return ''; @@ -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!"); - } -}