PageRenderTime 25ms CodeModel.GetById 19ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Log/Writer/Stream.php

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