‪TYPO3CMS  9.5
YamlSource.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It originated from the Neos.Form package (www.neos.io)
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
20 use Symfony\Component\Yaml\Exception\ParseException;
21 use Symfony\Component\Yaml\Yaml;
31 
39 {
43  protected ‪$filePersistenceSlot;
44 
49  {
50  $this->filePersistenceSlot = ‪$filePersistenceSlot;
51  }
52 
63  public function ‪load(array $filesToLoad): array
64  {
65  $configuration = [];
66  foreach ($filesToLoad as $fileToLoad) {
67  if ($fileToLoad instanceof ‪File) {
68  $fileIdentifier = $fileToLoad->getIdentifier();
69  $rawYamlContent = $fileToLoad->getContents();
70  if ($rawYamlContent === false) {
71  throw new ‪NoSuchFileException(
72  'The file "' . $fileIdentifier . '" does not exist.',
73  1498802253
74  );
75  }
76  } else {
77  $fileIdentifier = $fileToLoad;
78  $fileToLoad = GeneralUtility::getFileAbsFileName($fileToLoad);
79  if (is_file($fileToLoad)) {
80  $rawYamlContent = file_get_contents($fileToLoad);
81  } else {
82  throw new ‪NoSuchFileException(
83  'The file "' . $fileToLoad . '" does not exist.',
84  1471473378
85  );
86  }
87  }
88 
89  try {
90  $loadedConfiguration = Yaml::parse($rawYamlContent);
91 
92  if (is_array($loadedConfiguration)) {
93  $configuration = array_replace_recursive($configuration, $loadedConfiguration);
94  }
95  } catch (ParseException $exception) {
96  throw new ‪ParseErrorException(
97  'An error occurred while parsing file "' . $fileIdentifier . '": ' . $exception->getMessage(),
98  1480195405
99  );
100  }
101  }
102 
103  $configuration = ‪ArrayUtility::convertBooleanStringsToBooleanRecursive($configuration);
104  return $configuration;
105  }
106 
116  public function ‪save($fileToSave, array $configuration)
117  {
118  try {
119  $header = $this->‪getHeaderFromFile($fileToSave);
121  throw new ‪FileWriteException($e->getMessage(), 1512584488, $e);
122  }
123 
124  $yaml = Yaml::dump($configuration, 99, 2);
125 
126  if ($fileToSave instanceof ‪File) {
127  try {
128  $this->filePersistenceSlot->allowInvocation(
131  $fileToSave->getParentFolder(),
132  $fileToSave->getName()
133  ),
134  $this->filePersistenceSlot->getContentSignature(
135  $header . LF . $yaml
136  )
137  );
138  $fileToSave->setContents($header . LF . $yaml);
140  throw new ‪FileWriteException($e->getMessage(), 1512582753, $e);
141  }
142  } else {
143  $byteCount = @file_put_contents($fileToSave, $header . LF . $yaml);
144 
145  if ($byteCount === false) {
146  $error = error_get_last();
147  throw new ‪FileWriteException($error['message'], 1512582929);
148  }
149  }
150 
151  return $return;
152  }
153 
161  protected function ‪getHeaderFromFile($file): string
162  {
163  $header = '';
164  if ($file instanceof ‪File) {
165  $fileLines = explode(LF, $file->getContents());
166  } elseif (is_file($file)) {
167  $fileLines = file($file);
168  } else {
169  return '';
170  }
171 
172  foreach ($fileLines as $line) {
173  if (preg_match('/^#/', $line)) {
174  $header .= $line;
175  } else {
176  break;
177  }
178  }
179  return $header;
180  }
181 
187  protected function ‪buildCombinedIdentifier(‪FolderInterface $folder, string $fileName): string
188  {
189  return sprintf(
190  '%d:%s%s',
191  $folder->‪getStorage()->‪getUid(),
192  $folder->‪getIdentifier(),
193  $fileName
194  );
195  }
196 }
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\injectFilePersistenceSlot
‪injectFilePersistenceSlot(FilePersistenceSlot $filePersistenceSlot)
Definition: YamlSource.php:47
‪TYPO3\CMS\Form\Mvc\Configuration
Definition: ConfigurationManager.php:3
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\buildCombinedIdentifier
‪string buildCombinedIdentifier(FolderInterface $folder, string $fileName)
Definition: YamlSource.php:186
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:271
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\FileWriteException
Definition: FileWriteException.php:24
‪TYPO3\CMS\Form\Slot\FilePersistenceSlot\COMMAND_FILE_SET_CONTENTS
‪const COMMAND_FILE_SET_CONTENTS
Definition: FilePersistenceSlot.php:35
‪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:21
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\save
‪mixed save($fileToSave, array $configuration)
Definition: YamlSource.php:115
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Form\Slot\FilePersistenceSlot
Definition: FilePersistenceSlot.php:29
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\getHeaderFromFile
‪string getHeaderFromFile($file)
Definition: YamlSource.php:160
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource
Definition: YamlSource.php:39
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\ParseErrorException
Definition: ParseErrorException.php:24
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\load
‪array load(array $filesToLoad)
Definition: YamlSource.php:62
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Form\Mvc\Configuration\YamlSource\$filePersistenceSlot
‪FilePersistenceSlot $filePersistenceSlot
Definition: YamlSource.php:42
‪TYPO3\CMS\Core\Utility\ArrayUtility\convertBooleanStringsToBooleanRecursive
‪static array convertBooleanStringsToBooleanRecursive(array $array)
Definition: ArrayUtility.php:53
‪TYPO3\CMS\Form\Mvc\Configuration\Exception\NoSuchFileException
Definition: NoSuchFileException.php:24