-
Notifications
You must be signed in to change notification settings - Fork 164
/
AbstractFileTask.php
79 lines (69 loc) · 1.94 KB
/
AbstractFileTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\FS;
use Mage\Task\Exception\ErrorException;
use Mage\Task\AbstractTask;
/**
* File System Task - Abstract Base class for File Operations
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
abstract class AbstractFileTask extends AbstractTask
{
/**
* Returns the Task options
*
* @return array
* @throws ErrorException
*/
protected function getOptions()
{
$mandatory = $this->getParameters();
$defaults = array_keys($this->getDefaults());
$missing = array_diff($mandatory, $defaults);
foreach ($missing as $parameter) {
if (!array_key_exists($parameter, $this->options)) {
throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter));
}
}
return $this->options;
}
/**
* Returns the mandatory parameters
*
* @return array
*/
abstract protected function getParameters();
/**
* Returns a file with the placeholders replaced
*
* @param string $file
* @return string
* @throws ErrorException
*/
protected function getFile($file)
{
$mapping = [
'%environment%' => $this->runtime->getEnvironment(),
];
if ($this->runtime->getHostName() !== null) {
$mapping['%host%'] = $this->runtime->getHostName();
}
if ($this->runtime->getReleaseId() !== null) {
$mapping['%release%'] = $this->runtime->getReleaseId();
}
$options = $this->getOptions();
return str_replace(
array_keys($mapping),
array_values($mapping),
$options[$file]
);
}
}