PhpFormatter.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\DeploymentConfig\Writer;
  7. /**
  8. * A formatter for deployment configuration that presents it as a PHP-file that returns data
  9. */
  10. class PhpFormatter implements FormatterInterface
  11. {
  12. /**
  13. * 4 space indentation for array formatting.
  14. */
  15. const INDENT = ' ';
  16. /**
  17. * Format deployment configuration.
  18. * If $comments is present, each item will be added
  19. * as comment to the corresponding section
  20. *
  21. * {@inheritdoc}
  22. */
  23. public function format($data, array $comments = [])
  24. {
  25. if (!empty($comments) && is_array($data)) {
  26. return "<?php\nreturn [\n" . $this->formatData($data, $comments) . "\n];\n";
  27. }
  28. return "<?php\nreturn " . $this->varExportShort($data, true) . ";\n";
  29. }
  30. /**
  31. * Format supplied data
  32. *
  33. * @param string[] $data
  34. * @param string[] $comments
  35. * @param string $prefix
  36. * @return string
  37. */
  38. private function formatData($data, $comments = [], $prefix = ' ')
  39. {
  40. $elements = [];
  41. if (is_array($data)) {
  42. foreach ($data as $key => $value) {
  43. if (!empty($comments[$key])) {
  44. $elements[] = $prefix . '/**';
  45. $elements[] = $prefix . ' * For the section: ' . $key;
  46. foreach (explode("\n", $comments[$key]) as $commentLine) {
  47. $elements[] = $prefix . ' * ' . $commentLine;
  48. }
  49. $elements[] = $prefix . " */";
  50. }
  51. if (is_array($value)) {
  52. $elements[] = $prefix . $this->varExportShort($key) . ' => [';
  53. $elements[] = $this->formatData($value, [], ' ' . $prefix);
  54. $elements[] = $prefix . '],';
  55. } else {
  56. $elements[] = $prefix . $this->varExportShort($key) . ' => ' . $this->varExportShort($value) . ',';
  57. }
  58. }
  59. return implode("\n", $elements);
  60. }
  61. return var_export($data, true);
  62. }
  63. /**
  64. * If variable to export is an array, format with the php >= 5.4 short array syntax. Otherwise use
  65. * default var_export functionality.
  66. *
  67. * @param mixed $var
  68. * @param integer $depth
  69. * @return string
  70. */
  71. private function varExportShort($var, int $depth = 0)
  72. {
  73. if (!is_array($var)) {
  74. return var_export($var, true);
  75. }
  76. $indexed = array_keys($var) === range(0, count($var) - 1);
  77. $expanded = [];
  78. foreach ($var as $key => $value) {
  79. $expanded[] = str_repeat(self::INDENT, $depth)
  80. . ($indexed ? '' : $this->varExportShort($key) . ' => ')
  81. . $this->varExportShort($value, $depth + 1);
  82. }
  83. return sprintf("[\n%s\n%s]", implode(",\n", $expanded), str_repeat(self::INDENT, $depth - 1));
  84. }
  85. }