FileAbstract.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * @package Writer
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. #require_once "Zend/Config/Writer.php";
  22. /**
  23. * Abstract File Writer
  24. *
  25. * @category Zend
  26. * @package Zend_package
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id$
  30. */
  31. class Zend_Config_Writer_FileAbstract 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. * Write configuration to file.
  69. *
  70. * @param string $filename
  71. * @param Zend_Config $config
  72. * @param bool $exclusiveLock
  73. * @return void
  74. */
  75. public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
  76. {
  77. if ($filename !== null) {
  78. $this->setFilename($filename);
  79. }
  80. if ($config !== null) {
  81. $this->setConfig($config);
  82. }
  83. if ($exclusiveLock !== null) {
  84. $this->setExclusiveLock($exclusiveLock);
  85. }
  86. if ($this->_filename === null) {
  87. #require_once 'Zend/Config/Exception.php';
  88. throw new Zend_Config_Exception('No filename was set');
  89. }
  90. if ($this->_config === null) {
  91. #require_once 'Zend/Config/Exception.php';
  92. throw new Zend_Config_Exception('No config was set');
  93. }
  94. $configString = $this->render();
  95. $flags = 0;
  96. if ($this->_exclusiveLock) {
  97. $flags |= LOCK_EX;
  98. }
  99. $result = @file_put_contents($this->_filename, $configString, $flags);
  100. if ($result === false) {
  101. #require_once 'Zend/Config/Exception.php';
  102. throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
  103. }
  104. }
  105. /**
  106. * Render a Zend_Config into a config file string.
  107. *
  108. * @since 1.10
  109. * @todo For 2.0 this should be redone into an abstract method.
  110. * @return string
  111. */
  112. public function render()
  113. {
  114. return "";
  115. }
  116. }