TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
YamlSource.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Mvc\Configuration;
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Symfony\Component\Yaml\Exception\ParseException;
19 use Symfony\Component\Yaml\Yaml;
25 
33 {
41  protected $usePhpYamlExtension = false;
42 
47  public function __construct()
48  {
49  if (extension_loaded('yaml')) {
50  $this->usePhpYamlExtension = true;
51  }
52  }
53 
64  public function load(array $filesToLoad): array
65  {
66  $configuration = [];
67  foreach ($filesToLoad as $fileToLoad) {
68  if ($fileToLoad instanceof File) {
69  $fileIdentifier = $fileToLoad->getIdentifier();
70  $rawYamlContent = $fileToLoad->getContents();
71  } else {
72  $fileIdentifier = $fileToLoad;
73  $fileToLoad = GeneralUtility::getFileAbsFileName($fileToLoad);
74  if (is_file($fileToLoad)) {
75  $rawYamlContent = file_get_contents($fileToLoad);
76  } else {
77  throw new NoSuchFileException(
78  'The file "' . $fileToLoad . '" does not exist.',
79  1471473378
80  );
81  }
82  }
83 
84  try {
85  if ($this->usePhpYamlExtension) {
86  $loadedConfiguration = @yaml_parse($rawYamlContent);
87  if ($loadedConfiguration === false) {
88  throw new ParseErrorException(
89  'A parse error occurred while parsing file "' . $fileIdentifier . '".',
90  1391894094
91  );
92  }
93  } else {
94  $loadedConfiguration = Yaml::parse($rawYamlContent);
95  }
96 
97  if (is_array($loadedConfiguration)) {
98  ArrayUtility::mergeRecursiveWithOverrule($configuration, $loadedConfiguration);
99  }
100  } catch (ParseException $exception) {
101  throw new ParseErrorException(
102  'A parse error occurred while parsing file "' . $fileIdentifier . '". Error message: ' . $exception->getMessage(),
103  1480195405
104  );
105  }
106  }
107 
108  $configuration = ArrayUtility::convertBooleanStringsToBooleanRecursive($configuration);
109  return $configuration;
110  }
111 
120  public function save($fileToSave, array $configuration)
121  {
122  $header = $this->getHeaderFromFile($fileToSave);
123  $yaml = Yaml::dump($configuration, 99, 2);
124  if ($fileToSave instanceof File) {
125  $fileToSave->setContents($header . LF . $yaml);
126  } else {
127  @file_put_contents($fileToSave, $header . LF . $yaml);
128  }
129  }
130 
138  protected function getHeaderFromFile($file): string
139  {
140  $header = '';
141  if ($file instanceof File) {
142  $fileLines = explode(LF, $file->getContents());
143  } else {
144  $fileLines = file($file);
145  }
146  foreach ($fileLines as $line) {
147  if (preg_match('/^#/', $line)) {
148  $header .= $line;
149  } else {
150  break;
151  }
152  }
153  return $header;
154  }
155 }
save($fileToSave, array $configuration)
Definition: YamlSource.php:120
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
static getFileAbsFileName($filename, $_=null, $_2=null)