‪TYPO3CMS  ‪main
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 
51  {
52  $this->filePersistenceSlot = ‪$filePersistenceSlot;
53  }
54 
61  public function ‪load(array $filesToLoad): array
62  {
63  $configuration = [];
64 
65  foreach ($filesToLoad as $fileToLoad) {
66  if ($fileToLoad instanceof ‪File) {
67  $loadedConfiguration = $this->‪loadFromFile($fileToLoad);
68  } else {
69  $loadedConfiguration = $this->‪loadFromFilePath($fileToLoad);
70  if (isset($loadedConfiguration['TYPO3']['CMS']['Form'])) {
71  $namespacedConfiguration = $loadedConfiguration['TYPO3']['CMS']['Form'];
72  unset($loadedConfiguration['TYPO3']);
73  $loadedConfiguration = array_replace_recursive($namespacedConfiguration, $loadedConfiguration);
74  }
75  }
76 
77  if (is_array($loadedConfiguration)) {
78  $configuration = array_replace_recursive($configuration, $loadedConfiguration);
79  }
80  }
81 
82  $configuration = ‪ArrayUtility::convertBooleanStringsToBooleanRecursive($configuration);
83 
84  return $configuration;
85  }
86 
95  public function ‪save($fileToSave, array $configuration)
96  {
97  try {
98  $header = $this->‪getHeaderFromFile($fileToSave);
99  } catch (InsufficientFileAccessPermissionsException $e) {
100  throw new FileWriteException($e->getMessage(), 1512584488, $e);
101  }
102 
103  $yaml = Yaml::dump($configuration, 99, 2);
104 
105  if ($fileToSave instanceof File) {
106  try {
107  $this->filePersistenceSlot->allowInvocation(
110  $fileToSave->getParentFolder(),
111  $fileToSave->getName()
112  ),
113  $this->filePersistenceSlot->getContentSignature(
114  $header . LF . $yaml
115  )
116  );
117  $fileToSave->setContents($header . LF . $yaml);
118  } catch (InsufficientFileAccessPermissionsException $e) {
119  throw new FileWriteException($e->getMessage(), 1512582753, $e);
120  }
121  } else {
122  $byteCount = @file_put_contents($fileToSave, $header . LF . $yaml);
123 
124  if ($byteCount === false) {
125  $error = error_get_last();
126  throw new FileWriteException($error['message'], 1512582929);
127  }
128  }
129  }
130 
136  protected function ‪loadFromFilePath(string $filePath): array
137  {
138  $loader = GeneralUtility::makeInstance(YamlFileLoader::class);
139 
140  try {
141  $loadedConfiguration = $loader->load($filePath);
142  } catch (\RuntimeException $e) {
143  throw new ParseErrorException(
144  sprintf('An error occurred while parsing file "%s": %s', $filePath, $e->getMessage()),
145  1480195405,
146  $e
147  );
148  }
149 
150  return $loadedConfiguration;
151  }
152 
159  protected function ‪loadFromFile(File $file): array
160  {
161  $fileIdentifier = $file->getIdentifier();
162  $rawYamlContent = $file->getContents();
163 
164  if ($rawYamlContent === false) {
165  throw new NoSuchFileException(
166  sprintf('The file "%s" does not exist', $fileIdentifier),
167  1498802253
168  );
169  }
170 
171  try {
172  $loadedConfiguration = Yaml::parse($rawYamlContent);
173  } catch (ParseException $e) {
174  throw new ParseErrorException(
175  sprintf('An error occurred while parsing file "%s": %s', $fileIdentifier, $e->getMessage()),
176  1574422322,
177  $e
178  );
179  }
180 
181  return $loadedConfiguration;
182  }
183 
191  protected function ‪getHeaderFromFile($file): string
192  {
193  $header = '';
194  if ($file instanceof File) {
195  $fileLines = explode(LF, $file->getContents());
196  } elseif (is_file($file)) {
197  $fileLines = file($file);
198  } else {
199  return '';
200  }
201 
202  foreach ($fileLines as $line) {
203  if (preg_match('/^#/', $line)) {
204  $header .= $line;
205  } else {
206  break;
207  }
208  }
209  return $header;
210  }
211 
212  protected function ‪buildCombinedIdentifier(‪FolderInterface $folder, string $fileName): string
213  {
214  return sprintf(
215  '%d:%s%s',
216  $folder->‪getStorage()->getUid(),
217  $folder->‪getIdentifier(),
218  $fileName
219  );
220  }
221 }
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\injectFilePersistenceSlot
‪injectFilePersistenceSlot(FilePersistenceSlot $filePersistenceSlot)
Definition: YamlSource.php:49
‪TYPO3\CMS\Form\Mvc\Configuration
Definition: ConfigurationManager.php:18
‪TYPO3\CMS\Core\Resource\ResourceInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\FileWriteException
Definition: FileWriteException.php:24
‪TYPO3\CMS\Core\Resource\File\getContents
‪getContents()
Definition: File.php:104
‪TYPO3\CMS\Core\Resource\ResourceInterface\getStorage
‪getStorage()
‪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:94
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\buildCombinedIdentifier
‪buildCombinedIdentifier(FolderInterface $folder, string $fileName)
Definition: YamlSource.php:211
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\load
‪load(array $filesToLoad)
Definition: YamlSource.php:60
‪TYPO3\CMS\Form\Slot\FilePersistenceSlot
Definition: FilePersistenceSlot.php:38
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\getHeaderFromFile
‪string getHeaderFromFile($file)
Definition: YamlSource.php:190
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\loadFromFile
‪loadFromFile(File $file)
Definition: YamlSource.php:158
‪TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader
Definition: YamlFileLoader.php:47
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource
Definition: YamlSource.php:44
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\ParseErrorException
Definition: ParseErrorException.php:24
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:24
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\loadFromFilePath
‪loadFromFilePath(string $filePath)
Definition: YamlSource.php:135
‪TYPO3\CMS\Core\Utility\ArrayUtility\convertBooleanStringsToBooleanRecursive
‪static convertBooleanStringsToBooleanRecursive(array $array)
Definition: ArrayUtility.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\AbstractFile\getIdentifier
‪getIdentifier()
Definition: AbstractFile.php:144
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\$filePersistenceSlot
‪FilePersistenceSlot $filePersistenceSlot
Definition: YamlSource.php:47
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\NoSuchFileException
Definition: NoSuchFileException.php:24