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

79 lines
2 KiB
PHP
Raw Normal View History

<?php
2022-04-10 06:20:03 +02:00
/*
* 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;
2017-01-07 05:53:57 +01:00
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
{
2017-01-08 06:25:40 +01:00
/**
* Returns the Task options
*
2022-04-10 06:20:03 +02:00
* @return array<string, string|int|null>
2017-01-08 06:25:40 +01:00
* @throws ErrorException
*/
2022-04-10 06:20:03 +02:00
protected function getOptions(): array
{
$mandatory = $this->getParameters();
$defaults = array_keys($this->getDefaults());
$missing = array_diff($mandatory, $defaults);
foreach ($missing as $parameter) {
if (!array_key_exists($parameter, $this->options)) {
2017-01-07 05:53:57 +01:00
throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter));
}
}
return $this->options;
}
2017-01-08 06:25:40 +01:00
/**
* Returns the mandatory parameters
*
2022-04-10 06:20:03 +02:00
* @return string[]
2017-01-08 06:25:40 +01:00
*/
2022-04-10 06:20:03 +02:00
abstract protected function getParameters(): array;
2017-01-08 06:25:40 +01:00
/**
* Returns a file with the placeholders replaced
*
* @throws ErrorException
*/
2022-04-10 06:20:03 +02:00
protected function getFile(string $file): string
{
$mapping = [
'%environment%' => $this->runtime->getEnvironment(),
];
if ($this->runtime->getHostName() !== null) {
$mapping['%host%'] = $this->runtime->getHostName();
}
2017-01-24 21:28:37 +01:00
if ($this->runtime->getReleaseId() !== null) {
$mapping['%release%'] = $this->runtime->getReleaseId();
}
$options = $this->getOptions();
return str_replace(
array_keys($mapping),
array_values($mapping),
2022-04-10 06:20:03 +02:00
strval($options[$file])
);
}
}