From 83f047aab09679a1c36835809f229e4852093248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Fri, 10 Feb 2017 23:57:34 -0300 Subject: [PATCH] Allow to define default options in the tasks (eg: flags) --- src/Task/AbstractTask.php | 12 +++++++++++- src/Task/BuiltIn/FS/AbstractFileTask.php | 4 +++- src/Task/BuiltIn/FS/ChangeModeTask.php | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Task/AbstractTask.php b/src/Task/AbstractTask.php index 0ee7c2f..395dd5f 100644 --- a/src/Task/AbstractTask.php +++ b/src/Task/AbstractTask.php @@ -61,7 +61,8 @@ abstract class AbstractTask if (!is_array($options)) { $options = []; } - $this->options = $options; + + $this->options = array_merge($options, $this->getDefaults()); return $this; } @@ -76,4 +77,13 @@ abstract class AbstractTask $this->runtime = $runtime; return $this; } + + /** + * Return Default options + * @return array + */ + public function getDefaults() + { + return []; + } } diff --git a/src/Task/BuiltIn/FS/AbstractFileTask.php b/src/Task/BuiltIn/FS/AbstractFileTask.php index c2e4685..eb782b2 100644 --- a/src/Task/BuiltIn/FS/AbstractFileTask.php +++ b/src/Task/BuiltIn/FS/AbstractFileTask.php @@ -29,8 +29,10 @@ abstract class AbstractFileTask extends AbstractTask protected function getOptions() { $mandatory = $this->getParameters(); + $defaults = array_keys($this->getDefaults()); + $missing = array_diff($mandatory, $defaults); - foreach ($mandatory as $parameter) { + foreach ($missing as $parameter) { if (!array_key_exists($parameter, $this->options)) { throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter)); } diff --git a/src/Task/BuiltIn/FS/ChangeModeTask.php b/src/Task/BuiltIn/FS/ChangeModeTask.php index 883fce5..3ad05ba 100644 --- a/src/Task/BuiltIn/FS/ChangeModeTask.php +++ b/src/Task/BuiltIn/FS/ChangeModeTask.php @@ -50,4 +50,9 @@ class ChangeModeTask extends AbstractFileTask { return ['file', 'mode', 'flags']; } + + public function getDefaults() + { + return ['flags' => null]; + } }