PhpArray.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config\Writer;
  10. use Zend\Config\Exception;
  11. class PhpArray extends AbstractWriter
  12. {
  13. /**
  14. * @var string
  15. */
  16. const INDENT_STRING = ' ';
  17. /**
  18. * @var bool
  19. */
  20. protected $useBracketArraySyntax = false;
  21. /**
  22. * processConfig(): defined by AbstractWriter.
  23. *
  24. * @param array $config
  25. * @return string
  26. */
  27. public function processConfig(array $config)
  28. {
  29. $arraySyntax = array(
  30. 'open' => $this->useBracketArraySyntax ? '[' : 'array(',
  31. 'close' => $this->useBracketArraySyntax ? ']' : ')'
  32. );
  33. return "<?php\n" .
  34. "return " . $arraySyntax['open'] . "\n" . $this->processIndented($config, $arraySyntax) .
  35. $arraySyntax['close'] . ";\n";
  36. }
  37. /**
  38. * Sets whether or not to use the PHP 5.4+ "[]" array syntax.
  39. *
  40. * @param bool $value
  41. * @return self
  42. */
  43. public function setUseBracketArraySyntax($value)
  44. {
  45. $this->useBracketArraySyntax = $value;
  46. return $this;
  47. }
  48. /**
  49. * toFile(): defined by Writer interface.
  50. *
  51. * @see WriterInterface::toFile()
  52. * @param string $filename
  53. * @param mixed $config
  54. * @param bool $exclusiveLock
  55. * @return void
  56. * @throws Exception\InvalidArgumentException
  57. * @throws Exception\RuntimeException
  58. */
  59. public function toFile($filename, $config, $exclusiveLock = true)
  60. {
  61. if (empty($filename)) {
  62. throw new Exception\InvalidArgumentException('No file name specified');
  63. }
  64. $flags = 0;
  65. if ($exclusiveLock) {
  66. $flags |= LOCK_EX;
  67. }
  68. set_error_handler(
  69. function ($error, $message = '', $file = '', $line = 0) use ($filename) {
  70. throw new Exception\RuntimeException(
  71. sprintf('Error writing to "%s": %s', $filename, $message),
  72. $error
  73. );
  74. },
  75. E_WARNING
  76. );
  77. try {
  78. // for Windows, paths are escaped.
  79. $dirname = str_replace('\\', '\\\\', dirname($filename));
  80. $string = $this->toString($config);
  81. $string = str_replace("'" . $dirname, "__DIR__ . '", $string);
  82. file_put_contents($filename, $string, $flags);
  83. } catch (\Exception $e) {
  84. restore_error_handler();
  85. throw $e;
  86. }
  87. restore_error_handler();
  88. }
  89. /**
  90. * Recursively processes a PHP config array structure into a readable format.
  91. *
  92. * @param array $config
  93. * @param array $arraySyntax
  94. * @param int $indentLevel
  95. * @return string
  96. */
  97. protected function processIndented(array $config, array $arraySyntax, &$indentLevel = 1)
  98. {
  99. $arrayString = "";
  100. foreach ($config as $key => $value) {
  101. $arrayString .= str_repeat(self::INDENT_STRING, $indentLevel);
  102. $arrayString .= (is_int($key) ? $key : "'" . addslashes($key) . "'") . ' => ';
  103. if (is_array($value)) {
  104. if ($value === array()) {
  105. $arrayString .= $arraySyntax['open'] . $arraySyntax['close'] . ",\n";
  106. } else {
  107. $indentLevel++;
  108. $arrayString .= $arraySyntax['open'] . "\n"
  109. . $this->processIndented($value, $arraySyntax, $indentLevel)
  110. . str_repeat(self::INDENT_STRING, --$indentLevel) . $arraySyntax['close'] . ",\n";
  111. }
  112. } elseif (is_object($value) || is_string($value)) {
  113. $arrayString .= var_export($value, true) . ",\n";
  114. } elseif (is_bool($value)) {
  115. $arrayString .= ($value ? 'true' : 'false') . ",\n";
  116. } elseif ($value === null) {
  117. $arrayString .= "null,\n";
  118. } else {
  119. $arrayString .= $value . ",\n";
  120. }
  121. }
  122. return $arrayString;
  123. }
  124. }