From ef5b5bee1eb85c3d11d6f6ec424c7828ef0a6dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20L=C3=A9v=C3=AAque?= Date: Tue, 1 Dec 2015 15:17:25 +0100 Subject: [PATCH 1/2] allow this bundle to work with symfony3 --- DependencyInjection/Configuration.php | 4 ++- .../ElaoErrorNotifierExtension.php | 7 ++--- Listener/Notifier.php | 30 +++++++++---------- Twig/DumpyTwigFilter.php | 15 +++++----- composer.json | 4 +-- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 361ec5f..de7afde 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -27,7 +27,9 @@ public function getConfigTreeBuilder() ->arrayNode('to') ->beforeNormalization() ->ifString() - ->then(function($value) { return array($value); }) + ->then(function ($value) { + return array($value); + }) ->end() ->isRequired() ->cannotBeEmpty() diff --git a/DependencyInjection/ElaoErrorNotifierExtension.php b/DependencyInjection/ElaoErrorNotifierExtension.php index 30ae8b8..44de9e7 100644 --- a/DependencyInjection/ElaoErrorNotifierExtension.php +++ b/DependencyInjection/ElaoErrorNotifierExtension.php @@ -3,7 +3,6 @@ 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; @@ -14,14 +13,11 @@ */ class ElaoErrorNotifierExtension extends Extension { - /** * load configuration * * @param array $configs configs * @param ContainerBuilder $container container - * - * @return void */ public function load(array $configs, ContainerBuilder $container) { @@ -36,7 +32,8 @@ public function load(array $configs, ContainerBuilder $container) $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'])); } } } diff --git a/Listener/Notifier.php b/Listener/Notifier.php index 4103ccb..fee17df 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -2,7 +2,7 @@ namespace Elao\ErrorNotifierBundle\Listener; -use \Swift_Mailer; +use Swift_Mailer; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\HttpFoundation\Request; @@ -20,14 +20,13 @@ */ class Notifier { - /** - * @var Swift_Mailer $mailer + * @var Swift_Mailer */ private $mailer; /** - * @var EngineInterface $templating + * @var EngineInterface */ private $templating; @@ -198,10 +197,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 */ @@ -220,7 +219,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; } @@ -257,7 +256,7 @@ 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); } } @@ -317,12 +316,12 @@ public function createMailAndSend($exception, Request $request = null, $context 'command_input' => $commandInput )); - if($this->request) { - $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); - } elseif($this->command) { - $subject = '[' . $this->command->getName() . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); + if ($this->request) { + $subject = '['.$request->headers->get('host').'] Error '.$exception->getStatusCode().': '.$exception->getMessage(); + } elseif ($this->command) { + $subject = '['.$this->command->getName().'] Error '.$exception->getStatusCode().': '.$exception->getMessage(); } else { - $subject = 'Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); + $subject = 'Error '.$exception->getStatusCode().': '.$exception->getMessage(); } if (function_exists('mb_substr')) { @@ -373,5 +372,4 @@ protected static function _freeMemory() { self::$tmpBuffer = ''; } - } diff --git a/Twig/DumpyTwigFilter.php b/Twig/DumpyTwigFilter.php index 163b8f7..4f8d6f5 100755 --- a/Twig/DumpyTwigFilter.php +++ b/Twig/DumpyTwigFilter.php @@ -105,14 +105,14 @@ public function yamlDump($value, $depth = self::MAX_DEPTH) * @param int $recursionDepth The depth of recursion (used internally) * @return array|string */ - public function sanitize ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) + public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) { if (is_resource($value)) { return 'Resource'; } if (is_array($value)) { - return $this->sanitizeIterateable ($value, $maxRecursionDepth, $recursionDepth); + return $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth); } if ($value instanceof InvokerException) { @@ -148,7 +148,7 @@ public function sanitize ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursi $data = array(); $data['class'] = '' . $class->getShortName() . ''; if ($class->isIterateable()) { - $data['iterateable'] = $this->sanitizeIterateable ($value, $maxRecursionDepth, $recursionDepth); + $data['iterateable'] = $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth); } else { $data['accessors'] = array(); foreach ($class->getMethods() as $method) { @@ -205,7 +205,7 @@ public function sanitize ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursi return $value; } - public function sanitizeIterateable ($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) + public function sanitizeIterateable($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) { if ($recursionDepth < $maxRecursionDepth) { $r = array (); @@ -215,15 +215,16 @@ public function sanitizeIterateable ($value, $maxRecursionDepth = self::MAX_DEPT foreach ($value as $k => $v) { $r[$k] = $this->sanitize($v, $maxRecursionDepth, $recursionDepth + 1); $count++; - if($count >= 20) { + if ($count >= 20) { $r[] = sprintf('... and %s more ...', ($arrayCount - $count)); break; } } - + return $r; } else { - $c = count($value); $t = gettype($value); + $c = count($value); + $t = gettype($value); return $c ? "$t of $c" : "empty $t"; } diff --git a/composer.json b/composer.json index b3b5189..2b4e2b8 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ "require": { "php": ">=5.3.2", - "symfony/framework-bundle": "~2.3", - "symfony/console": "~2.3", + "symfony/framework-bundle": "~2.3|~3.0", + "symfony/console": "~2.3|~3.0", "swiftmailer/swiftmailer": "~5.0" }, From 568adedb703787930dcf5428e2cd5f7dd47eccee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20L=C3=A9v=C3=AAque?= Date: Tue, 1 Dec 2015 15:47:33 +0100 Subject: [PATCH 2/2] respect our convention --- .php_cs | 30 +++++++++++ DependencyInjection/Configuration.php | 9 +++- .../ElaoErrorNotifierExtension.php | 14 +++-- ElaoErrorNotifierBundle.php | 8 +++ Exception/InvokerException.php | 9 ++++ Listener/Notifier.php | 46 +++++++++------- Twig/DumpyTwigFilter.php | 53 +++++++++++-------- 7 files changed, 124 insertions(+), 45 deletions(-) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..c5f822a --- /dev/null +++ b/.php_cs @@ -0,0 +1,30 @@ + +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__)) +; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index de7afde..d3b769c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -1,5 +1,13 @@ + */ + namespace Elao\ErrorNotifierBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; @@ -10,7 +18,6 @@ */ class Configuration implements ConfigurationInterface { - /** * Get config tree * diff --git a/DependencyInjection/ElaoErrorNotifierExtension.php b/DependencyInjection/ElaoErrorNotifierExtension.php index 44de9e7..aa59846 100644 --- a/DependencyInjection/ElaoErrorNotifierExtension.php +++ b/DependencyInjection/ElaoErrorNotifierExtension.php @@ -1,12 +1,20 @@ + */ + namespace Elao\ErrorNotifierBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; -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 @@ -28,7 +36,7 @@ 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') { diff --git a/ElaoErrorNotifierBundle.php b/ElaoErrorNotifierBundle.php index fe15d4d..e7a5370 100644 --- a/ElaoErrorNotifierBundle.php +++ b/ElaoErrorNotifierBundle.php @@ -1,5 +1,13 @@ + */ + namespace Elao\ErrorNotifierBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; diff --git a/Exception/InvokerException.php b/Exception/InvokerException.php index 8fde21d..6a22521 100755 --- a/Exception/InvokerException.php +++ b/Exception/InvokerException.php @@ -1,9 +1,18 @@ + */ + namespace Elao\ErrorNotifierBundle\Exception; /** * Exception which is thrown when a method cannot be invoked + * * @see Elao\ErrornotifierBundle\Twig\Dumpy */ class InvokerException extends \Exception diff --git a/Listener/Notifier.php b/Listener/Notifier.php index fee17df..05413c1 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -1,19 +1,27 @@ + */ + namespace Elao\ErrorNotifierBundle\Listener; 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 @@ -104,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; } } @@ -171,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) { @@ -264,7 +272,8 @@ public function handlePhpFatalErrorAndWarnings() /** * Convert the error code to a readable format * - * @param integer $errorNo + * @param int $errorNo + * * @return string */ public function getErrorString($errorNo) @@ -313,15 +322,15 @@ 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) { - $subject = '['.$request->headers->get('host').'] Error '.$exception->getStatusCode().': '.$exception->getMessage(); + $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } elseif ($this->command) { - $subject = '['.$this->command->getName().'] Error '.$exception->getStatusCode().': '.$exception->getMessage(); + $subject = '[' . $this->command->getName() . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } else { - $subject = 'Error '.$exception->getStatusCode().': '.$exception->getMessage(); + $subject = 'Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); } if (function_exists('mb_substr')) { @@ -343,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); diff --git a/Twig/DumpyTwigFilter.php b/Twig/DumpyTwigFilter.php index 4f8d6f5..5221ea3 100755 --- a/Twig/DumpyTwigFilter.php +++ b/Twig/DumpyTwigFilter.php @@ -1,5 +1,17 @@ + */ +namespace Elao\ErrorNotifierBundle\Twig; + +use Elao\ErrorNotifierBundle\Exception\InvokerException; +use Symfony\Component\Yaml\Dumper as YamlDumper; + /** * Extends Twig with * {{ "my string, whatever" | pre }} --> wraps with
@@ -21,14 +33,9 @@
  *   Default value is 1. (MAX_DEPTH const)
  *
  * @see https://gist.github.com/1747036
+ *
  * @author Goutte
  */
-
-namespace Elao\ErrorNotifierBundle\Twig;
-
-use Symfony\Component\Yaml\Dumper as YamlDumper;
-use Elao\ErrorNotifierBundle\Exception\InvokerException;
-
 class DumpyTwigFilter extends \Twig_Extension
 {
     /** @const INLINE : default value for the inline parameter of the YAML dumper aka the expanding-level */
@@ -52,7 +59,7 @@ public function getFilters()
 
     public function pre($stringable)
     {
-        return "
" . (string) $stringable . "
"; + return '
' . (string) $stringable . '
'; } public function preDump($values) @@ -69,7 +76,8 @@ public function preYamlDump($values, $depth = self::MAX_DEPTH) * Encodes as YAML the passed $input * * @param $input - * @param int $inline + * @param int $inline + * * @return mixed */ public function encode($input, $inline = self::INLINE) @@ -86,8 +94,9 @@ public function encode($input, $inline = self::INLINE) /** * Returns a templating-helper dump of depth-sanitized var as yaml string * - * @param mixed $value What to dump - * @param int $depth Recursion max depth + * @param mixed $value What to dump + * @param int $depth Recursion max depth + * * @return string */ public function yamlDump($value, $depth = self::MAX_DEPTH) @@ -101,8 +110,9 @@ public function yamlDump($value, $depth = self::MAX_DEPTH) * A bit dirty as this should be in another Class, but hey * * @param $value - * @param int $maxRecursionDepth The maximum depth of recursion - * @param int $recursionDepth The depth of recursion (used internally) + * @param int $maxRecursionDepth The maximum depth of recursion + * @param int $recursionDepth The depth of recursion (used internally) + * * @return array|string */ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) @@ -143,10 +153,9 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio } return $classInfo; - } else { // Get all accessors and their values - $data = array(); - $data['class'] = '' . $class->getShortName() . ''; + $data = array(); + $data['class'] = '' . $class->getShortName() . ''; if ($class->isIterateable()) { $data['iterateable'] = $this->sanitizeIterateable($value, $maxRecursionDepth, $recursionDepth); } else { @@ -160,18 +169,16 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio $methodInfo = ($method->getNumberOfParameters() ? substr($methodInfo, 0, -2) : $methodInfo) . ')'; if (!$method->getNumberOfRequiredParameters()) { // Get the value, we don't need params try { - $methodValue = $method->invoke($value); + $methodValue = $method->invoke($value); $data['accessors'][$methodInfo] = $this->sanitize($methodValue, $maxRecursionDepth, $recursionDepth + 1); } catch (\Exception $e) { $data['accessors'][$methodInfo] = $this->sanitize(new InvokerException('Couldn\'t invoke method: Exception "' . get_class($e) . '" with message "' . $e->getMessage() . '"'), $maxRecursionDepth, $recursionDepth + 1); } - } else { // Get only method name and its params $data['accessors'][] = $methodInfo; } } } - } return $data; @@ -179,15 +186,15 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio } if (is_string($value)) { - $value = '(string) '.$value; + $value = '(string) ' . $value; } if (is_int($value)) { - $value = '(int) '.$value; + $value = '(int) ' . $value; } if (is_float($value)) { - $value = '(float) '.$value; + $value = '(float) ' . $value; } if (is_null($value)) { @@ -208,9 +215,9 @@ public function sanitize($value, $maxRecursionDepth = self::MAX_DEPTH, $recursio public function sanitizeIterateable($value, $maxRecursionDepth = self::MAX_DEPTH, $recursionDepth = 0) { if ($recursionDepth < $maxRecursionDepth) { - $r = array (); + $r = array(); $arrayCount = count($value); - $count = 0; + $count = 0; foreach ($value as $k => $v) { $r[$k] = $this->sanitize($v, $maxRecursionDepth, $recursionDepth + 1);