2
0

Array.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Config
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Config_Writer_Array extends Zend_Config_Writer
  32. {
  33. /**
  34. * Filename to write to
  35. *
  36. * @var string
  37. */
  38. protected $_filename = null;
  39. /**
  40. * Wether to exclusively lock the file or not
  41. *
  42. * @var boolean
  43. */
  44. protected $_exclusiveLock = false;
  45. /**
  46. * Set the target filename
  47. *
  48. * @param string $filename
  49. * @return Zend_Config_Writer_Array
  50. */
  51. public function setFilename($filename)
  52. {
  53. $this->_filename = $filename;
  54. return $this;
  55. }
  56. /**
  57. * Set wether to exclusively lock the file or not
  58. *
  59. * @param boolean $exclusiveLock
  60. * @return Zend_Config_Writer_Array
  61. */
  62. public function setExclusiveLock($exclusiveLock)
  63. {
  64. $this->_exclusiveLock = $exclusiveLock;
  65. return $this;
  66. }
  67. /**
  68. * Defined by Zend_Config_Writer
  69. *
  70. * @param string $filename
  71. * @param Zend_Config $config
  72. * @param boolean $exclusiveLock
  73. * @throws Zend_Config_Exception When filename was not set
  74. * @throws Zend_Config_Exception When filename is not writable
  75. * @return void
  76. */
  77. public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
  78. {
  79. if ($filename !== null) {
  80. $this->setFilename($filename);
  81. }
  82. if ($config !== null) {
  83. $this->setConfig($config);
  84. }
  85. if ($exclusiveLock !== null) {
  86. $this->setExclusiveLock($exclusiveLock);
  87. }
  88. if ($this->_filename === null) {
  89. require_once 'Zend/Config/Exception.php';
  90. throw new Zend_Config_Exception('No filename was set');
  91. }
  92. if ($this->_config === null) {
  93. require_once 'Zend/Config/Exception.php';
  94. throw new Zend_Config_Exception('No config was set');
  95. }
  96. $data = $this->_config->toArray();
  97. $sectionName = $this->_config->getSectionName();
  98. if (is_string($sectionName)) {
  99. $data = array($sectionName => $data);
  100. }
  101. $arrayString = "<?php\n"
  102. . "return " . var_export($data, true) . ";\n";
  103. $flags = 0;
  104. if ($this->_exclusiveLock) {
  105. $flags |= LOCK_EX;
  106. }
  107. $result = @file_put_contents($this->_filename, $arrayString, $flags);
  108. if ($result === false) {
  109. require_once 'Zend/Config/Exception.php';
  110. throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
  111. }
  112. }
  113. }