Skip to content

Instantly share code, notes, and snippets.

@b-durand
Created December 13, 2011 22:00
Show Gist options
  • Save b-durand/1474081 to your computer and use it in GitHub Desktop.
Save b-durand/1474081 to your computer and use it in GitHub Desktop.
Log FilterChain
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Log
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @namespace
*/
namespace Zend\Log;
/**
* Custom implementation of filter chain for logging
*
* @category Zend
* @package Zend_Log
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class FilterChain
{
/**
* Filter chain
*
* @var array
*/
protected $filters = array();
/**
* @param Filter|closure $callback
* @return FilterChain
* @throws Exception\InvalidArgumentException
*/
public function attach($callback)
{
if ($callback instanceof Filter) {
$callback = array($callback, 'filter');
}
if (!is_callable($callback)) {
throw new Exception\InvalidArgumentException(sprintf(
'$callback must be valid; received "%s"',
is_object($callback) ? get_class($callback) : gettype($callback)
));
}
$this->filters[] = $callback;
return $this;
}
/**
* @param array $context
* @return boolean
*/
public function run(array $context)
{
if (0 == count($this->filters)) {
return true;
}
foreach ($this->filters as $filter) {
if (!call_user_func($filter, $context)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment