PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Configuration/Source/YamlSource.php

https://github.com/christianjul/FLOW3-Composer
PHP | 87 lines | 42 code | 6 blank | 39 comment | 7 complexity | 69b1b9e8631a05879145d7c96a1b2bfb MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Configuration\Source;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * Configuration source based on YAML files
  15. *
  16. * @FLOW3\Scope("singleton")
  17. * @api
  18. */
  19. class YamlSource implements \TYPO3\FLOW3\Configuration\Source\SourceInterface {
  20. /**
  21. * Loads the specified configuration file and returns its content as an
  22. * array. If the file does not exist or could not be loaded, an empty
  23. * array is returned
  24. *
  25. * @param string $pathAndFilename Full path and filename of the file to load, excluding the file extension (ie. ".yaml")
  26. * @return array
  27. * @throws \TYPO3\FLOW3\Configuration\Exception\ParseErrorException
  28. */
  29. public function load($pathAndFilename) {
  30. if (file_exists($pathAndFilename . '.yaml')) {
  31. try {
  32. $configuration = \Symfony\Component\Yaml\Yaml::parse($pathAndFilename . '.yaml');
  33. if (!is_array($configuration)) {
  34. $configuration = array();
  35. }
  36. } catch (\TYPO3\FLOW3\Error\Exception $exception) {
  37. throw new \TYPO3\FLOW3\Configuration\Exception\ParseErrorException('A parse error occurred while parsing file "' . $pathAndFilename . '.yaml". Error message: ' . $exception->getMessage(), 1232014321);
  38. }
  39. } else {
  40. $configuration = array();
  41. }
  42. return $configuration;
  43. }
  44. /**
  45. * Save the specified configuration array to the given file in YAML format.
  46. *
  47. * @param string $pathAndFilename Full path and filename of the file to write to, excluding the dot and file extension (i.e. ".yaml")
  48. * @param array $configuration The configuration to save
  49. * @return void
  50. */
  51. public function save($pathAndFilename, array $configuration) {
  52. $header = '';
  53. if (file_exists($pathAndFilename . '.yaml')) {
  54. $header = $this->getHeaderFromFile($pathAndFilename . '.yaml');
  55. }
  56. $yaml = \Symfony\Component\Yaml\Yaml::dump($configuration, 99);
  57. file_put_contents($pathAndFilename . '.yaml', $header . chr(10) . $yaml);
  58. }
  59. /**
  60. * Read the header part from the given file. That means, every line
  61. * until the first non comment line is found.
  62. *
  63. * @param string $pathAndFilename
  64. * @return string The header of the given YAML file
  65. * @api
  66. */
  67. protected function getHeaderFromFile($pathAndFilename) {
  68. $header = '';
  69. $fileHandle = fopen($pathAndFilename, 'r');
  70. while ($line = fgets($fileHandle)) {
  71. if (preg_match('/^#/', $line)) {
  72. $header .= $line;
  73. } else {
  74. break;
  75. }
  76. }
  77. fclose($fileHandle);
  78. return $header;
  79. }
  80. }
  81. ?>