Allow to define default options in the tasks (eg: flags)

This commit is contained in:
Andrés Montañez 2017-02-10 23:57:34 -03:00
parent 5f4236c208
commit 83f047aab0
3 changed files with 19 additions and 2 deletions

View file

@ -61,7 +61,8 @@ abstract class AbstractTask
if (!is_array($options)) { if (!is_array($options)) {
$options = []; $options = [];
} }
$this->options = $options;
$this->options = array_merge($options, $this->getDefaults());
return $this; return $this;
} }
@ -76,4 +77,13 @@ abstract class AbstractTask
$this->runtime = $runtime; $this->runtime = $runtime;
return $this; return $this;
} }
/**
* Return Default options
* @return array
*/
public function getDefaults()
{
return [];
}
} }

View file

@ -29,8 +29,10 @@ abstract class AbstractFileTask extends AbstractTask
protected function getOptions() protected function getOptions()
{ {
$mandatory = $this->getParameters(); $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)) { if (!array_key_exists($parameter, $this->options)) {
throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter)); throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter));
} }

View file

@ -50,4 +50,9 @@ class ChangeModeTask extends AbstractFileTask
{ {
return ['file', 'mode', 'flags']; return ['file', 'mode', 'flags'];
} }
public function getDefaults()
{
return ['flags' => null];
}
} }