9aea9f22298924822b6776b4379e925f320e37bc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Configuration / Loader / FalYamlFileLoader.php
1 <?php
2 namespace TYPO3\CMS\Core\Configuration\Loader;
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 TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\Configuration;
18 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * YAML loader for FAL files
24 */
25 class FalYamlFileLoader extends YamlFileLoader
26 {
27 /**
28 * @var \TYPO3\CMS\Core\Resource\ResourceFactory
29 */
30 protected $resourceFactory;
31
32 /**
33 * @param Configuration|null $configuration
34 */
35 public function __construct(Configuration $configuration = null, ResourceFactory $resourceFactory = null)
36 {
37 parent::__construct($configuration);
38 $this->resourceFactory = $resourceFactory ?: GeneralUtility::makeInstance(ResourceFactory::class);
39 }
40
41 /**
42 * Loads and parses a YAML file, returns an array with the data found
43 *
44 * @param File|string $fileName either relative to PATH_site or prefixed with EXT:... or File object
45 * @return array the configuration as array
46 */
47 public function load($fileName): array
48 {
49 return $this->loadFromContent($this->getFileContents($fileName));
50 }
51
52 /**
53 * @param File|string $fileName either relative to PATH_site or prefixed with EXT:... or File object
54 * @return string the contents of the file
55 * @throws \RuntimeException when the file was not accessible
56 */
57 protected function getFileContents($fileName): string
58 {
59 $file = null;
60
61 if (is_string($fileName)) {
62 $file = $this->resourceFactory->retrieveFileOrFolderObject($fileName);
63 } elseif (is_object($fileName)) {
64 $file = $fileName;
65 }
66
67 if ($file instanceof File) {
68 $content = $file->getContents();
69
70 if (!$content) {
71 throw new \RuntimeException('YAML file "' . $file->getIdentifier() . '" could not be loaded', 1512561127);
72 }
73
74 return $content;
75 }
76
77 return parent::getFileContents($fileName);
78 }
79 }