Skip to content

Commit

Permalink
Merge branch 'symfony4'
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturMoczulski committed Aug 18, 2018
2 parents d47dcdb + e2a6260 commit 9bfab91
Show file tree
Hide file tree
Showing 27 changed files with 527 additions and 323 deletions.
32 changes: 28 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,38 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2

env:
- SYMFONY_VERSION=3.4.*
- SYMFONY_VERSION=4.0.*
- SYMFONY_VERSION=4.1.*

sudo: false
dist: trusty

cache:
directories:
- $HOME/.composer/cache/files

matrix:
exclude:
- php: 5.6
env: SYMFONY_VERSION=4.0.*
- php: 5.6
env: SYMFONY_VERSION=4.1.*
- php: 7.0
env: SYMFONY_VERSION=4.0.*
- php: 7.0
env: SYMFONY_VERSION=4.1.*

before_install:
- curl -s https://getcomposer.org/installer | php
- php composer.phar install
- composer self-update
- composer require -n --prefer-dist "symfony/symfony:${SYMFONY_VERSION}"

script:
- php -v
- php composer.phar test
- composer test

after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)
66 changes: 34 additions & 32 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,61 @@

namespace Rollbar\Symfony\RollbarBundle\DependencyInjection;

use Rollbar\Config;
use Rollbar\Defaults;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Class Configuration
*
* @link https://rollbar.com/docs/notifier/rollbar-php/#configuration-reference
*
* @package Rollbar\Symfony\RollbarBundle\DependencyInjection
*/
class Configuration implements ConfigurationInterface
{
/**
* @inheritdoc
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rollbarConfigNode = $treeBuilder->root(RollbarExtension::ALIAS);
$rollbarConfigNodeChildren = $rollbarConfigNode->children();

// the intendation in this method reflects the structure of the rootNode
// for convenience

foreach (\Rollbar\Config::listOptions() as $option) {
// TODO: this is duplicated code from
// https://github.com/rollbar/rollbar-php-wordpress/blob/master/src/Plugin.php#L359-L366
// It needs to get replaced with a native rollbar/rollbar-php method
// as pointed out here https://github.com/rollbar/rollbar-php/issues/344
$method = lcfirst(str_replace('_', '', ucwords($option, '_')));

// Handle the "branch" exception
switch ($method) {
case "branch":
$method = "gitBranch";
break;
case "includeErrorCodeContext":
$method = 'includeCodeContext';
$configOptions = Config::listOptions();
$rollbarDefaults = Defaults::get();

foreach ($configOptions as $option) {
switch ($option) {
case 'branch':
$method = 'gitBranch';
break;
case "includeExceptionCodeContext":
$method = 'includeExcCodeContext';
default:
$method = $option;
break;
}

$default = method_exists(\Rollbar\Defaults::get(), $method) ?
\Rollbar\Defaults::get()->$method() :
null;

$rollbarConfigNode
->children()
->scalarNode($option)
->defaultValue($default)
->end();

try {
$default = $rollbarDefaults->fromSnakeCase($method);
} catch (\Exception $e) {
$default = null;
}

if (is_array($default)) {
$rollbarConfigNodeChildren
->arrayNode($option)
->scalarPrototype()->end()
->defaultValue($default)
->end();
} else {
$rollbarConfigNodeChildren
->scalarNode($option)
->defaultValue($default)
->end();
}
}

$rollbarConfigNode->end();

return $treeBuilder;
}
Expand Down
17 changes: 4 additions & 13 deletions DependencyInjection/RollbarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,17 @@
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

use Rollbar\Rollbar;
use Rollbar\Monolog\Handler\RollbarHandler;
use Monolog\Logger;
use Psr\Logger\LoggerInterface;

/**
* Class Extension
* Class RollbarExtension
*
* @package Rollbar\Symfony\RollbarBundle\DependencyInjection
*/
class RollbarExtension extends Extension
{
const ALIAS = 'rollbar';

/**
* Loads a specific configuration.
*
* @param array $configs An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
Expand All @@ -42,7 +33,7 @@ public function load(array $configs, ContainerBuilder $container)
}

/**
* @return string
* {@inheritdoc}
*/
public function getAlias()
{
Expand Down
42 changes: 27 additions & 15 deletions EventListener/AbstractListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,65 @@

namespace Rollbar\Symfony\RollbarBundle\EventListener;

use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Rollbar\Symfony\RollbarBundle\Payload\Generator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Monolog\Logger;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Rollbar\Symfony\RollbarBundle\DependencyInjection\SymfonyRollbarExtension;
use Rollbar\Symfony\RollbarBundle\Payload\Generator;
use Psr\Log\LoggerInterface;

/**
* Class AbstractListener
*
* @package Rollbar\Symfony\RollbarBundle\EventListener
*/
abstract class AbstractListener implements EventSubscriberInterface
{
/**
* @var \Monolog\Logger
* @var Logger
*/
protected $logger;

/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
* @var ContainerInterface
*/
protected $container;

/**
* @var \Rollbar\Symfony\RollbarBundle\Payload\Generator
* @var Generator
*/
protected $generator;

/**
* AbstractListener constructor.
*
* @param ContainerInterface $container
* @param LoggerInterface $logger
* @param Generator $generator
*/
public function __construct(
ContainerInterface $container,
LoggerInterface $logger,
Generator $generator
) {
/**
* @var \Rollbar\Symfony\RollbarBundle\Provider\RollbarHandler $rbProvider
*/
$this->container = $container;
$this->logger = $logger;
$this->generator = $generator;
}

/**
* @return \Monolog\Logger
* Get logger.
*
* @return Logger
*/
public function getLogger()
{
return $this->logger;
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
Expand All @@ -64,23 +70,29 @@ public static function getSubscribedEvents()
}

/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* On kernel exception event handler.
*
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
// dummy
}

/**
* @return \Symfony\Component\DependencyInjection\ContainerInterface
* Get container.
*
* @return ContainerInterface
*/
public function getContainer()
{
return $this->container;
}

/**
* @return \Rollbar\Symfony\RollbarBundle\Payload\Generator
* Get generator.
*
* @return Generator
*/
public function getGenerator()
{
Expand Down
22 changes: 14 additions & 8 deletions EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace Rollbar\Symfony\RollbarBundle\EventListener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Rollbar\Symfony\RollbarBundle\DependencyInjection\RollbarExtension;
use Psr\Log\LoggerInterface;
use Rollbar\Symfony\RollbarBundle\DependencyInjection\RollbarExtension;
use Rollbar\Symfony\RollbarBundle\Payload\Generator;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class ErrorListener
*
* @package Rollbar\Symfony\RollbarBundle\EventListener
*/
class ErrorListener extends AbstractListener
{
/**
* ErrorListener constructor.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \Psr\Log\LoggerInterface $logger
* @param \Rollbar\Symfony\RollbarBundle\Payload\Generator $generator
* @param ContainerInterface $container
* @param LoggerInterface $logger
* @param Generator $generator
*/
public function __construct(
ContainerInterface $container,
Expand Down Expand Up @@ -73,9 +77,11 @@ public function handleFatalError()

/**
* Wrap php error_get_last() to get more testable code
*
* @link: http://php.net/manual/en/function.error-get-last.php
*
* @return array|null
*
* @codeCoverageIgnore
*/
protected function getLastError()
Expand All @@ -86,13 +92,13 @@ protected function getLastError()
/**
* Check do we need to report error or skip
*
* @param $code
* @param mixed $code
*
* @return int
*/
protected function isReportable($code)
{
$code = (int)$code;
$code = (int) $code;
$config = $this->getContainer()->getParameter(RollbarExtension::ALIAS . '.config');

return true
Expand Down
15 changes: 9 additions & 6 deletions EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

namespace Rollbar\Symfony\RollbarBundle\EventListener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Psr\Log\LoggerInterface;
use Rollbar\Symfony\RollbarBundle\Payload\Generator;

/**
* Class ExceptionListener
*
* @package Rollbar\Symfony\RollbarBundle\EventListener
*/
class ExceptionListener extends AbstractListener
{
/**
* Process exception
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
Expand All @@ -28,15 +30,16 @@ public function onKernelException(GetResponseForExceptionEvent $event)
/**
* Handle provided exception
*
* @param $exception
* @param mixed $exception
*/
public function handleException($exception)
{
// generate payload and log data
list($message, $payload) = $this->getGenerator()->getExceptionPayload($exception);

$this->getLogger()->error($message, [
'payload' => $payload,
'exception' => $exception,
]);
}
}
Loading

0 comments on commit 9bfab91

Please sign in to comment.