[FEATURE] Option to globally enable redirect hit count
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Configuration / Writer / YamlFileWriter.php
1 <?php
2 namespace TYPO3\CMS\Core\Configuration\Writer;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Symfony\Component\Yaml\Yaml;
18 use TYPO3\CMS\Core\Configuration\Writer\Exception\FileWriteException;
19 use TYPO3\CMS\Core\Resource\File;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * A YAML file writer that allows to write YAML files, based on the Symfony/Yaml component
24 */
25 class YamlFileWriter
26 {
27
28 /**
29 * Write a YAML file
30 *
31 * @param FILE|string $fileName either relative to PATH_site or prefixed with EXT:... or FILE object
32 * @param array $content The content
33 * @param int $inlineLevel The level where you switch to inline YAML
34 * @param int $indent The amount of spaces to use for indentation of nested nodes
35 * @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
36 * @throws FileWriteException if the file could not be written
37 */
38 public function save(
39 $fileName,
40 array $content,
41 int $inlineLevel = 99,
42 int $indent = 2,
43 int $flags = 0
44 ) {
45 $content = Yaml::dump($content, $inlineLevel, $indent, $flags);
46
47 if ($fileName instanceof File) {
48 try {
49 $fileName->setContents($content);
50 } catch (\RuntimeException $e) {
51 throw new FileWriteException($e->getMessage(), 1512582753, $e);
52 }
53 } else {
54 $streamlinedFileName = GeneralUtility::getFileAbsFileName($fileName);
55 if (!$streamlinedFileName) {
56 throw new \FileWriteException('YAML File "' . $fileName . '" could not be loaded', 1485784248);
57 }
58 if (!GeneralUtility::writeFile($streamlinedFileName, $content)) {
59 $error = error_get_last();
60 throw new FileWriteException($error['message'], 1512582929);
61 }
62 }
63 }
64 }