Browse Source

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

pull/1/head
Andrés Montañez 8 years ago
parent
commit
83f047aab0
  1. 12
      src/Task/AbstractTask.php
  2. 4
      src/Task/BuiltIn/FS/AbstractFileTask.php
  3. 5
      src/Task/BuiltIn/FS/ChangeModeTask.php

12
src/Task/AbstractTask.php

@ -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 [];
}
} }

4
src/Task/BuiltIn/FS/AbstractFileTask.php

@ -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));
} }

5
src/Task/BuiltIn/FS/ChangeModeTask.php

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

Loading…
Cancel
Save