TYPO3 CMS  TYPO3_8-7
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 
31 
39 {
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  $exception
100  );
101  }
102  }
103 
104  $configuration = ArrayUtility::convertBooleanStringsToBooleanRecursive($configuration);
105  return $configuration;
106  }
107 
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 }
injectFilePersistenceSlot(FilePersistenceSlot $filePersistenceSlot)
Definition: YamlSource.php:48
save($fileToSave, array $configuration)
Definition: YamlSource.php:116
static convertBooleanStringsToBooleanRecursive(array $array)
buildCombinedIdentifier(FolderInterface $folder, string $fileName)
Definition: YamlSource.php:187
static getFileAbsFileName($filename, $_=null, $_2=null)