gordonbanderson /
php-travis-enhancer
| 1 | <?php declare(strict_types = 1); |
||
| 2 | |||
| 3 | namespace Suilven\PHPTravisEnhancer\Helper; |
||
| 4 | |||
| 5 | use Symfony\Component\Yaml\Yaml; |
||
| 6 | |||
| 7 | class TravisYMLHelper |
||
| 8 | { |
||
| 9 | |||
| 10 | /** @var string */ |
||
| 11 | private $travisFileName = '.travis.yml'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * TravisYMLHelper constructor. |
||
| 15 | * |
||
| 16 | * @param string $travisFileName The name of the travis config file, by default .travis.yml, but injectable for |
||
| 17 | * testing purposes |
||
| 18 | */ |
||
| 19 | public function __construct(string $travisFileName = '.travis.yml') |
||
| 20 | { |
||
| 21 | $this->travisFileName = $travisFileName; |
||
| 22 | } |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * Ensure that a hierarchy exists in a PHP array. Pass the hierarchy in the form matrix/include, this will populate |
||
| 27 | * the path an empty leaf array |
||
| 28 | * |
||
| 29 | * @param array<string, string|array> $yamlAsArray YAML file converted into an array. Can of course be any |
||
| 30 | * associative array |
||
| 31 | * @param string $path The hierarchy required to exist, in the form matrix/include (forward slash separated) |
||
| 32 | */ |
||
| 33 | public function ensurePathExistsInYaml(array &$yamlAsArray, string $path): void |
||
| 34 | { |
||
| 35 | $pathParts = \explode('/', $path); |
||
| 36 | $part = \array_shift($pathParts); |
||
| 37 | if (!isset($yamlAsArray[$part])) { |
||
| 38 | $yamlAsArray[$part] = []; |
||
| 39 | } |
||
| 40 | $remainingPath = \implode('/', $pathParts); |
||
| 41 | if (\sizeof($pathParts) === 0) { |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->ensurePathExistsInYaml($yamlAsArray[$part], $remainingPath); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 46 | } |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Check for an existing entry of the bash variable in the Travis matrix |
||
| 51 | * |
||
| 52 | * @todo What is the correct annotation to prevent this error? |
||
| 53 | * @psalm-suppress PossiblyInvalidArrayOffset |
||
| 54 | * @param array<string, string|array> $yamlAsArray Yaml parsed into an array |
||
| 55 | * @param string $flag a bash variable flag, such as DUPLICATE_CHECK |
||
| 56 | * @return bool true if an existing environment setting exists for this variable |
||
| 57 | */ |
||
| 58 | public function checkForExistingInEnv(array $yamlAsArray, string $flag): bool |
||
| 59 | { |
||
| 60 | $foundExisting = false; |
||
| 61 | foreach ($yamlAsArray['matrix']['include'] as $entry) { |
||
| 62 | $env = $entry['env']; |
||
| 63 | if ($env !== ($flag . '=1')) { |
||
| 64 | continue; |
||
| 65 | } |
||
| 66 | |||
| 67 | $foundExisting = true; |
||
| 68 | } |
||
| 69 | |||
| 70 | return $foundExisting; |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Load the Travis file, or default to an empty array if it does not exist |
||
| 76 | * |
||
| 77 | * @todo Change this behavior? |
||
| 78 | * @return array<string, string|array> The Travis YAML file as an array (note Symfony returns mixed) |
||
| 79 | */ |
||
| 80 | public function loadTravis(): array |
||
| 81 | { |
||
| 82 | $result = []; |
||
| 83 | $path = \getcwd() . '/' . $this->travisFileName; |
||
| 84 | if (\file_exists($this->travisFileName)) { |
||
| 85 | $result = Yaml::parseFile($path); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $result; |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Save a travis file, default .travis.yml, in the root of a project |
||
| 94 | * |
||
| 95 | * @todo How does one specifiy this as an associative array? |
||
| 96 | * @param array<string, string|array> $yamlArray an array that ought to have been formed from a YAML file |
||
| 97 | */ |
||
| 98 | public function saveTravis(array $yamlArray): void |
||
| 99 | { |
||
| 100 | $yaml = Yaml::dump($yamlArray, Yaml::DUMP_OBJECT_AS_MAP); |
||
| 101 | \file_put_contents($this->travisFileName, $yaml); |
||
| 102 | } |
||
| 103 | } |
||
| 104 |