‪TYPO3CMS  10.4
YamlSource.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 /*
19  * Inspired by and partially taken from the Neos.Form package (www.neos.io)
20  */
21 
23 
24 use Symfony\Component\Yaml\Exception\ParseException;
25 use Symfony\Component\Yaml\Yaml;
36 
44 {
48  protected ‪$filePersistenceSlot;
49 
54  {
55  $this->filePersistenceSlot = ‪$filePersistenceSlot;
56  }
57 
64  public function ‪load(array $filesToLoad): array
65  {
66  $configuration = [];
67 
68  foreach ($filesToLoad as $fileToLoad) {
69  if ($fileToLoad instanceof ‪File) {
70  $loadedConfiguration = $this->‪loadFromFile($fileToLoad);
71  } else {
72  $loadedConfiguration = $this->‪loadFromFilePath($fileToLoad);
73  }
74 
75  if (is_array($loadedConfiguration)) {
76  $configuration = array_replace_recursive($configuration, $loadedConfiguration);
77  }
78  }
79 
80  $configuration = ‪ArrayUtility::convertBooleanStringsToBooleanRecursive($configuration);
81 
82  return $configuration;
83  }
84 
93  public function ‪save($fileToSave, array $configuration)
94  {
95  try {
96  $header = $this->‪getHeaderFromFile($fileToSave);
98  throw new ‪FileWriteException($e->getMessage(), 1512584488, $e);
99  }
100 
101  $yaml = Yaml::dump($configuration, 99, 2);
102 
103  if ($fileToSave instanceof ‪File) {
104  try {
105  $this->filePersistenceSlot->allowInvocation(
108  $fileToSave->getParentFolder(),
109  $fileToSave->getName()
110  ),
111  $this->filePersistenceSlot->getContentSignature(
112  $header . LF . $yaml
113  )
114  );
115  $fileToSave->setContents($header . LF . $yaml);
117  throw new ‪FileWriteException($e->getMessage(), 1512582753, $e);
118  }
119  } else {
120  $byteCount = @file_put_contents($fileToSave, $header . LF . $yaml);
121 
122  if ($byteCount === false) {
123  $error = error_get_last();
124  throw new ‪FileWriteException($error['message'], 1512582929);
125  }
126  }
127  }
128 
134  protected function ‪loadFromFilePath(string $filePath): array
135  {
136  $loader = GeneralUtility::makeInstance(YamlFileLoader::class);
137 
138  try {
139  $loadedConfiguration = $loader->load($filePath);
140  } catch (\RuntimeException $e) {
141  throw new ‪ParseErrorException(
142  sprintf('An error occurred while parsing file "%s": %s', $filePath, $e->getMessage()),
143  1480195405,
144  $e
145  );
146  }
147 
148  return $loadedConfiguration;
149  }
150 
157  protected function ‪loadFromFile(‪File $file): array
158  {
159  $fileIdentifier = $file->‪getIdentifier();
160  $rawYamlContent = $file->‪getContents();
161 
162  if ($rawYamlContent === false) {
163  throw new ‪NoSuchFileException(
164  sprintf('The file "%s" does not exist', $fileIdentifier),
165  1498802253
166  );
167  }
168 
169  try {
170  $loadedConfiguration = Yaml::parse($rawYamlContent);
171  } catch (ParseException $e) {
172  throw new ‪ParseErrorException(
173  sprintf('An error occurred while parsing file "%s": %s', $fileIdentifier, $e->getMessage()),
174  1574422322,
175  $e
176  );
177  }
178 
179  return $loadedConfiguration;
180  }
181 
189  protected function ‪getHeaderFromFile($file): string
190  {
191  $header = '';
192  if ($file instanceof ‪File) {
193  $fileLines = explode(LF, $file->‪getContents());
194  } elseif (is_file($file)) {
195  $fileLines = file($file);
196  } else {
197  return '';
198  }
199 
200  foreach ($fileLines as $line) {
201  if (preg_match('/^#/', $line)) {
202  $header .= $line;
203  } else {
204  break;
205  }
206  }
207  return $header;
208  }
209 
215  protected function ‪buildCombinedIdentifier(‪FolderInterface $folder, string $fileName): string
216  {
217  return sprintf(
218  '%d:%s%s',
219  $folder->‪getStorage()->‪getUid(),
220  $folder->‪getIdentifier(),
221  $fileName
222  );
223  }
224 }
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\injectFilePersistenceSlot
‪injectFilePersistenceSlot(FilePersistenceSlot $filePersistenceSlot)
Definition: YamlSource.php:52
‪TYPO3\CMS\Form\Mvc\Configuration
Definition: ConfigurationManager.php:18
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\buildCombinedIdentifier
‪string buildCombinedIdentifier(FolderInterface $folder, string $fileName)
Definition: YamlSource.php:214
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:321
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\FileWriteException
Definition: FileWriteException.php:25
‪TYPO3\CMS\Core\Resource\AbstractFile\getIdentifier
‪string getIdentifier()
Definition: AbstractFile.php:141
‪TYPO3\CMS\Form\Slot\FilePersistenceSlot\COMMAND_FILE_SET_CONTENTS
‪const COMMAND_FILE_SET_CONTENTS
Definition: FilePersistenceSlot.php:44
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\save
‪save($fileToSave, array $configuration)
Definition: YamlSource.php:92
‪TYPO3\CMS\Core\Resource\ResourceInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Resource\ResourceInterface\getStorage
‪ResourceStorage getStorage()
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:24
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\load
‪load(array $filesToLoad)
Definition: YamlSource.php:63
‪TYPO3\CMS\Form\Slot\FilePersistenceSlot
Definition: FilePersistenceSlot.php:38
‪TYPO3\CMS\Core\Resource\File\getContents
‪string getContents()
Definition: File.php:128
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\getHeaderFromFile
‪string getHeaderFromFile($file)
Definition: YamlSource.php:188
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\loadFromFile
‪loadFromFile(File $file)
Definition: YamlSource.php:156
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:48
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource
Definition: YamlSource.php:44
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\ParseErrorException
Definition: ParseErrorException.php:25
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:24
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:22
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\loadFromFilePath
‪loadFromFilePath(string $filePath)
Definition: YamlSource.php:133
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\$filePersistenceSlot
‪FilePersistenceSlot $filePersistenceSlot
Definition: YamlSource.php:47
‪TYPO3\CMS\Core\Utility\ArrayUtility\convertBooleanStringsToBooleanRecursive
‪static array convertBooleanStringsToBooleanRecursive(array $array)
Definition: ArrayUtility.php:54
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\NoSuchFileException
Definition: NoSuchFileException.php:25