Stream.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_Log
  17. * @subpackage Writer
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Log_Writer_Abstract */
  23. require_once 'Zend/Log/Writer/Abstract.php';
  24. /** Zend_Log_Formatter_Simple */
  25. require_once 'Zend/Log/Formatter/Simple.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage Writer
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @version $Id$
  33. */
  34. class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract
  35. {
  36. /**
  37. * Holds the PHP stream to log to.
  38. * @var null|stream
  39. */
  40. protected $_stream = null;
  41. /**
  42. * Class Constructor
  43. *
  44. * @param streamOrUrl Stream or URL to open as a stream
  45. * @param mode Mode, only applicable if a URL is given
  46. */
  47. public function __construct($streamOrUrl, $mode = NULL)
  48. {
  49. // Setting the default
  50. if ($mode === NULL) {
  51. $mode = 'a';
  52. }
  53. if (is_resource($streamOrUrl)) {
  54. if (get_resource_type($streamOrUrl) != 'stream') {
  55. require_once 'Zend/Log/Exception.php';
  56. throw new Zend_Log_Exception('Resource is not a stream');
  57. }
  58. if ($mode != 'a') {
  59. require_once 'Zend/Log/Exception.php';
  60. throw new Zend_Log_Exception('Mode cannot be changed on existing streams');
  61. }
  62. $this->_stream = $streamOrUrl;
  63. } else {
  64. if (is_array($streamOrUrl) && isset($streamOrUrl['stream'])) {
  65. $streamOrUrl = $streamOrUrl['stream'];
  66. }
  67. if (! $this->_stream = @fopen($streamOrUrl, $mode, false)) {
  68. require_once 'Zend/Log/Exception.php';
  69. $msg = "\"$streamOrUrl\" cannot be opened with mode \"$mode\"";
  70. throw new Zend_Log_Exception($msg);
  71. }
  72. }
  73. $this->_formatter = new Zend_Log_Formatter_Simple();
  74. }
  75. /**
  76. * Create a new instance of Zend_Log_Writer_Mock
  77. *
  78. * @exception Zend_Log_Exception
  79. * @param mixed $config
  80. * @return Zend_Log_Writer_Mock
  81. */
  82. static public function factory($config)
  83. {
  84. $config = self::_parseConfig($config);
  85. $config = $config + array('stream'=>NULL, 'mode'=>NULL);
  86. $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream'];
  87. return new self(
  88. $streamOrUrl,
  89. $config['mode']
  90. );
  91. }
  92. /**
  93. * Close the stream resource.
  94. *
  95. * @return void
  96. */
  97. public function shutdown()
  98. {
  99. if (is_resource($this->_stream)) {
  100. fclose($this->_stream);
  101. }
  102. }
  103. /**
  104. * Write a message to the log.
  105. *
  106. * @param array $event event data
  107. * @return void
  108. */
  109. protected function _write($event)
  110. {
  111. $line = $this->_formatter->format($event);
  112. if (false === @fwrite($this->_stream, $line)) {
  113. require_once 'Zend/Log/Exception.php';
  114. throw new Zend_Log_Exception("Unable to write to stream");
  115. }
  116. }
  117. }