Json.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Config
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Config_Writer
  23. */
  24. #require_once 'Zend/Config/Writer/FileAbstract.php';
  25. /**
  26. * @see Zend_Config_Json
  27. */
  28. #require_once 'Zend/Config/Json.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Config
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Config_Writer_Json extends Zend_Config_Writer_FileAbstract
  36. {
  37. /**
  38. * If we need to pretty-print JSON data
  39. *
  40. * @var boolean
  41. */
  42. protected $_prettyPrint = false;
  43. /**
  44. * Get prettyPrint flag
  45. *
  46. * @return the prettyPrint flag
  47. */
  48. public function prettyPrint()
  49. {
  50. return $this->_prettyPrint;
  51. }
  52. /**
  53. * Set prettyPrint flag
  54. *
  55. * @param bool $prettyPrint PrettyPrint flag
  56. * @return Zend_Config_Writer_Json
  57. */
  58. public function setPrettyPrint($flag)
  59. {
  60. $this->_prettyPrint = (bool) $flag;
  61. return $this;
  62. }
  63. /**
  64. * Render a Zend_Config into a JSON config string.
  65. *
  66. * @since 1.10
  67. * @return string
  68. */
  69. public function render()
  70. {
  71. $data = $this->_config->toArray();
  72. $sectionName = $this->_config->getSectionName();
  73. $extends = $this->_config->getExtends();
  74. if (is_string($sectionName)) {
  75. $data = array($sectionName => $data);
  76. }
  77. foreach ($extends as $section => $parentSection) {
  78. $data[$section][Zend_Config_Json::EXTENDS_NAME] = $parentSection;
  79. }
  80. // Ensure that each "extends" section actually exists
  81. foreach ($data as $section => $sectionData) {
  82. if (is_array($sectionData) && isset($sectionData[Zend_Config_Json::EXTENDS_NAME])) {
  83. $sectionExtends = $sectionData[Zend_Config_Json::EXTENDS_NAME];
  84. if (!isset($data[$sectionExtends])) {
  85. // Remove "extends" declaration if section does not exist
  86. unset($data[$section][Zend_Config_Json::EXTENDS_NAME]);
  87. }
  88. }
  89. }
  90. $out = Zend_Json::encode($data);
  91. if ($this->prettyPrint()) {
  92. $out = Zend_Json::prettyPrint($out);
  93. }
  94. return $out;
  95. }
  96. }