Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #51 from Elao/symfony3
Browse files Browse the repository at this point in the history
allow this bundle to work with symfony3
  • Loading branch information
benji07 committed Dec 21, 2015
2 parents a312af0 + 568aded commit b851c7c
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 70 deletions.
30 changes: 30 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

$header = <<<EOF
This file is part of the Elao ErrorNotifier Bundle
Copyright (C) Elao
@author Elao <[email protected]>
EOF;

Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);

return Symfony\CS\Config\Config::create()
// use default SYMFONY_LEVEL and extra fixers:
->fixers([
'-concat_without_spaces',
'-phpdoc_short_description',
'-pre_increment',
'-unalign_double_arrow',
'-unalign_equals',
'align_double_arrow',
'align_equals',
'concat_with_spaces',
'header_comment',
'ordered_use',
'phpdoc_order',
])
->setUsingCache(true)
->finder(Symfony\CS\Finder\DefaultFinder::create()->in(__DIR__))
;
13 changes: 11 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/*
* This file is part of the Elao ErrorNotifier Bundle
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\ErrorNotifierBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand All @@ -10,7 +18,6 @@
*/
class Configuration implements ConfigurationInterface
{

/**
* Get config tree
*
Expand All @@ -27,7 +34,9 @@ public function getConfigTreeBuilder()
->arrayNode('to')
->beforeNormalization()
->ifString()
->then(function($value) { return array($value); })
->then(function ($value) {
return array($value);
})
->end()
->isRequired()
->cannotBeEmpty()
Expand Down
21 changes: 13 additions & 8 deletions DependencyInjection/ElaoErrorNotifierExtension.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
<?php

/*
* This file is part of the Elao ErrorNotifier Bundle
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\ErrorNotifierBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* ElaoErrorNotifier Extension
*/
class ElaoErrorNotifierExtension extends Extension
{

/**
* load configuration
*
* @param array $configs configs
* @param ContainerBuilder $container container
*
* @return void
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -32,11 +36,12 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('elao.error_notifier.config', $config);

$loader = new XmlFileLoader($container, new FileLocator(array(__DIR__.'/../Resources/config/')));
$loader = new XmlFileLoader($container, new FileLocator(array(__DIR__ . '/../Resources/config/')));
$loader->load('services.xml');

if ($config['mailer'] != 'mailer') {
$container->getDefinition('elao.error_notifier.listener')->replaceArgument(0, new Reference($config['mailer']));
$definition = $container->getDefinition('elao.error_notifier.listener');
$definition->replaceArgument(0, new Reference($config['mailer']));
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions ElaoErrorNotifierBundle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/*
* This file is part of the Elao ErrorNotifier Bundle
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\ErrorNotifierBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand Down
9 changes: 9 additions & 0 deletions Exception/InvokerException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<?php

/*
* This file is part of the Elao ErrorNotifier Bundle
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\ErrorNotifierBundle\Exception;

/**
* Exception which is thrown when a method cannot be invoked
*
* @see Elao\ErrornotifierBundle\Twig\Dumpy
*/
class InvokerException extends \Exception
Expand Down
64 changes: 36 additions & 28 deletions Listener/Notifier.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
<?php

/*
* This file is part of the Elao ErrorNotifier Bundle
*
* Copyright (C) Elao
*
* @author Elao <[email protected]>
*/

namespace Elao\ErrorNotifierBundle\Listener;

use \Swift_Mailer;
use Swift_Mailer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Templating\EngineInterface;

/**
* Notifier
*/
class Notifier
{

/**
* @var Swift_Mailer $mailer
* @var Swift_Mailer
*/
private $mailer;

/**
* @var EngineInterface $templating
* @var EngineInterface
*/
private $templating;

Expand Down Expand Up @@ -105,13 +112,13 @@ public function onKernelException(GetResponseForExceptionEvent $event)
}

if (strlen($this->ignoredAgentsPattern)) {
if (preg_match('#'.$this->ignoredAgentsPattern.'#', $event->getRequest()->headers->get('User-Agent'))) {
if (preg_match('#' . $this->ignoredAgentsPattern . '#', $event->getRequest()->headers->get('User-Agent'))) {
return;
}
}

if (strlen($this->ignoredUrlsPattern)) {
if (preg_match('#'.$this->ignoredUrlsPattern.'#', $event->getRequest()->getUri())) {
if (preg_match('#' . $this->ignoredUrlsPattern . '#', $event->getRequest()->getUri())) {
return;
}
}
Expand Down Expand Up @@ -172,7 +179,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event)
{
$this->request = null;

$this->command = $event->getCommand();
$this->command = $event->getCommand();
$this->commandInput = $event->getInput();

if ($this->reportErrors || $this->reportWarnings) {
Expand All @@ -198,10 +205,10 @@ protected function setErrorHandlers()
/**
* @see http://php.net/set_error_handler
*
* @param integer $level
* @param string $message
* @param string $file
* @param integer $line
* @param int $level
* @param string $message
* @param string $file
* @param int $line
*
* @throws ErrorException
*/
Expand All @@ -220,7 +227,7 @@ public function handlePhpError($level, $message, $file, $line, $errcontext)
return false;
}

if(in_array($message, $this->ignoredPhpErrors)) {
if (in_array($message, $this->ignoredPhpErrors)) {
return false;
}

Expand Down Expand Up @@ -257,15 +264,16 @@ public function handlePhpFatalErrorAndWarnings()
}

if (in_array($lastError['type'], $errors) && !in_array(@$lastError['message'], $this->ignoredPhpErrors)) {
$exception = new \ErrorException(sprintf('%s: %s in %s line %d', @$this->getErrorString(@$lastError['type']), @$lastError['message'], @$lastError['file'], @$lastError['line']), @$lastError['type'], @$lastError['type'], @$lastError['file'], @$lastError['line']);
$exception = new \ErrorException(sprintf('%s: %s in %s line %d', @$this->getErrorString(@$lastError['type']), @$lastError['message'], @$lastError['file'], @$lastError['line']), @$lastError['type'], @$lastError['type'], @$lastError['file'], @$lastError['line']);
$this->createMailAndSend($exception, $this->request, null, $this->command, $this->commandInput);
}
}

/**
* Convert the error code to a readable format
*
* @param integer $errorNo
* @param int $errorNo
*
* @return string
*/
public function getErrorString($errorNo)
Expand Down Expand Up @@ -314,12 +322,12 @@ public function createMailAndSend($exception, Request $request = null, $context
'status_code' => $exception->getCode(),
'context' => $context,
'command' => $command,
'command_input' => $commandInput
'command_input' => $commandInput,
));

if($this->request) {
if ($this->request) {
$subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage();
} elseif($this->command) {
} elseif ($this->command) {
$subject = '[' . $this->command->getName() . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage();
} else {
$subject = 'Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage();
Expand All @@ -344,13 +352,14 @@ public function createMailAndSend($exception, Request $request = null, $context
/**
* Check last send time
*
* @param FlattenException $exception
* @param FlattenException $exception
*
* @return bool
*/
private function checkRepeat(FlattenException $exception)
{
$key = md5($exception->getMessage().':'.$exception->getLine().':'.$exception->getFile());
$file = $this->errorsDir.'/'.$key;
$key = md5($exception->getMessage() . ':' . $exception->getLine() . ':' . $exception->getFile());
$file = $this->errorsDir . '/' . $key;
$time = is_file($file) ? file_get_contents($file) : 0;
if ($time < time()) {
file_put_contents($file, time() + $this->repeatTimeout);
Expand All @@ -373,5 +382,4 @@ protected static function _freeMemory()
{
self::$tmpBuffer = '';
}

}
Loading

0 comments on commit b851c7c

Please sign in to comment.