Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Handler\FingersCrossed;

use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Activation strategy that ignores 404s for certain URLs.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Fabien Potencier <fabien@symfony.com>
*/
class NotFoundActivationStrategy extends ErrorLevelActivationStrategy
{
private $blacklist;
private $requestStack;

public function __construct(RequestStack $requestStack, array $excludedUrls, $actionLevel)
{
parent::__construct($actionLevel);

$this->requestStack = $requestStack;
$this->blacklist = '{('.implode('|', $excludedUrls).')}i';
}

public function isHandlerActivated(array $record)
{
$isActivated = parent::isHandlerActivated($record);

if (
$isActivated
&& isset($record['context']['exception'])
&& $record['context']['exception'] instanceof HttpException
&& $record['context']['exception']->getStatusCode() == 404
&& ($request = $this->requestStack->getMasterRequest())
) {
return !preg_match($this->blacklist, $request->getPathInfo());
}

return $isActivated;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Tests\Handler\FingersCrossed;

use Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Monolog\Logger;

class NotFoundActivationStrategyTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider isActivatedProvider
*/
public function testIsActivated($url, $record, $expected)
{
$requestStack = new RequestStack();
$requestStack->push(Request::create($url));

$strategy = new NotFoundActivationStrategy($requestStack, array('^/foo', 'bar'), Logger::WARNING);

$this->assertEquals($expected, $strategy->isHandlerActivated($record));
}

public function isActivatedProvider()
{
return array(
array('/test', array('level' => Logger::DEBUG), false),
array('/foo', array('level' => Logger::DEBUG, 'context' => $this->getContextException(404)), false),
array('/baz/bar', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), false),
array('/foo', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),

array('/test', array('level' => Logger::ERROR), true),
array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(404)), true),
array('/baz', array('level' => Logger::ERROR, 'context' => $this->getContextException(500)), true),
);
}

protected function getContextException($code)
{
return array('exception' => new HttpException($code));
}
}