Skip to content

Commit 61c8ba4

Browse files
author
Andrii Afanasiev
committed
prepe for RollbarJS
1 parent 0500f93 commit 61c8ba4

File tree

11 files changed

+228
-13
lines changed

11 files changed

+228
-13
lines changed

DependencyInjection/Configuration.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class Configuration implements ConfigurationInterface
2424
const ENVIRONMENT = 'production';
2525
const TIMEOUT = 3;
2626

27+
const JS_ITEMS_PER_MINUTE = 60;
28+
const JS_MAX_ITEMS = 0;
29+
const JS_UNCAUGHT_LEVEL = "error";
30+
2731
public static $scrubFieldsDefault = [
2832
'passwd',
2933
'password',
@@ -34,6 +38,14 @@ class Configuration implements ConfigurationInterface
3438
'csrf_token',
3539
];
3640

41+
public static $autoInstrument = [
42+
'network' => true,
43+
'log' => true,
44+
'dom' => true,
45+
'navigation' => true,
46+
'connectivit' => true,
47+
];
48+
3749
/**
3850
* List of classes that should be excluded
3951
*
@@ -117,6 +129,40 @@ public function getConfigTreeBuilder()
117129
->scalarNode('send_message_trace')->defaultFalse()->end()
118130
->scalarNode('include_raw_request_body')->defaultFalse()->end()
119131
->scalarNode('local_vars_dump')->defaultFalse()->end()
132+
->end()
133+
->end()
134+
->arrayNode('rollbar_js')->children()
135+
->scalarNode('enabled')->defaultTrue()->end()
136+
->scalarNode('accessToken')->defaultValue('')->end()
137+
->scalarNode('captureUncaught')->defaultTrue()->end()
138+
->scalarNode('uncaughtErrorLevel')->defaultValue(static::JS_UNCAUGHT_LEVEL)->end()
139+
->scalarNode('captureUnhandledRejections')->defaultTrue()->end()
140+
->arrayNode('payload')
141+
->treatNullLike([])
142+
->prototype('scalar')->end()
143+
->defaultValue(["environment" => static::ENVIRONMENT])
144+
->end()
145+
->arrayNode('ignoredMessages')
146+
->treatNullLike([])
147+
->prototype('scalar')->end()
148+
->defaultValue([])
149+
->end()
150+
->scalarNode('verbose')->defaultFalse()->end()
151+
->scalarNode('async')->defaultTrue()->end()
152+
->scalarNode('autoInstrument')->defaultTrue()->end()
153+
->arrayNode('autoInstrument')
154+
->treatNullLike([])
155+
->prototype('scalar')->end()
156+
->defaultValue(static::$autoInstrument)
157+
->end()
158+
->scalarNode('itemsPerMinute')->defaultValue(static::JS_ITEMS_PER_MINUTE)->end()
159+
->scalarNode('maxItems')->defaultValue(static::JS_MAX_ITEMS)->end()
160+
->arrayNode('scrubFields')
161+
->treatNullLike([])
162+
->prototype('scalar')->end()
163+
->defaultValue(static::$scrubFieldsDefault)
164+
->end()
165+
->end()
120166
->end()
121167
->end();
122168

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@ Find all documentation here [here](https://github.com/OxCom/symfony3-rollbar-bun
2525
access_token: 'some-secret-token-here'
2626
```
2727

28-
# TODO
29-
- Add integration for [Rollbar.JS](https://rollbar.com/docs/notifier/rollbar.js/)
30-
- More docs
31-
3228
# Bugs and Issues
33-
Please, if You found a bug or something, that is not working properly, contact me and tell what's wrong. It's nice to have an example how to reproduce a bug, or any idea how to fix it in Your request. I'll take care about it ASAP.
29+
Please, if You found a bug or something, that is not working properly, contact me and tell what's wrong. It's nice to have an example how to reproduce a bug, or any idea how to fix it in Your request. I'll take care about it ASAP.

Resources/config/services.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ parameters:
44
symfony_rollbar.provider.rollbar_handler.class: SymfonyRollbarBundle\Provider\RollbarHandler
55
symfony_rollbar.provider.api_client.class: SymfonyRollbarBundle\Provider\ApiClient
66
symfony_rollbar.command.deploy.class: SymfonyRollbarBundle\Command\DeployCommand
7+
symfony_rollbar.twig.extension.class: SymfonyRollbarBundle\Twig\RollbarExtension
78

89
services:
910
symfony_rollbar.event_listener.exception_listener:
@@ -31,6 +32,12 @@ services:
3132
arguments: ["@service_container"]
3233

3334
symfony_rollbar.command.deploy:
34-
class: "%symfony_rollbar.command.deploy.class%"
35-
tags:
36-
- {name: console.command, command: "rollbar:deploy"}
35+
class: "%symfony_rollbar.command.deploy.class%"
36+
tags:
37+
- {name: console.command, command: "rollbar:deploy"}
38+
39+
symfony_rollbar.twig.extension:
40+
class: "%symfony_rollbar.twig.extension.class%"
41+
arguments: ["@service_container"]
42+
tags:
43+
- { name: twig.runtime }

Tests/Fixtures/app/config/config_test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ symfony_rollbar:
1313
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1414
environment: '%kernel.environment%'
1515
person_fn: \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider
16+
17+
rollbar_js:
18+
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
19+
payload:
20+
environment: '%kernel.environment%'

Tests/Fixtures/app/config/config_test_drb.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ symfony_rollbar:
1111
rollbar:
1212
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1313
environment: '%kernel.environment%'
14+
15+
rollbar_js:
16+
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
17+
payload:
18+
environment: '%kernel.environment%'

Tests/Fixtures/app/config/config_test_pf.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ symfony_rollbar:
1212
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1313
environment: '%kernel.environment%'
1414
person_fn: "get_awesome_person"
15+
16+
rollbar_js:
17+
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
18+
payload:
19+
environment: '%kernel.environment%'

Tests/Fixtures/app/config/config_test_ps.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ symfony_rollbar:
1313
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
1414
environment: '%kernel.environment%'
1515
person_fn: "symfony_rollbar.fixture.person_provider"
16+
17+
rollbar_js:
18+
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
19+
payload:
20+
environment: '%kernel.environment%'

Tests/SymfonyRollbarBundle/DependencyInjection/ConfigurationTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* Class ConfigurationTest
11+
*
1112
* @package SymfonyRollbarBundle\Tests\DependencyInjection
1213
*/
1314
class ConfigurationTest extends KernelTestCase
@@ -20,15 +21,15 @@ public function testParameters()
2021
$config = $container->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
2122
$defaultErrorMask = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
2223

23-
$exclude = Configuration::$exclude;
24+
$exclude = Configuration::$exclude;
2425
$exclude[] = '\SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException';
2526
$exclude[] = '\ParseError';
2627
$exclude[] = '\Symfony\Component\HttpKernel\Exception\HttpExceptionInterface';
2728

2829
$default = [
29-
'enable' => true,
30-
'exclude' => $exclude,
31-
'rollbar' => [
30+
'enable' => true,
31+
'exclude' => $exclude,
32+
'rollbar' => [
3233
'access_token' => 'SOME_ROLLBAR_ACCESS_TOKEN_123456',
3334
'agent_log_location' => static::$kernel->getLogDir() . '/rollbar.log',
3435
'base_api_url' => 'https://api.rollbar.com/api/1/',
@@ -66,6 +67,21 @@ public function testParameters()
6667
'include_raw_request_body' => false,
6768
'local_vars_dump' => false,
6869
],
70+
'rollbar_js' => [
71+
'accessToken' => 'SOME_ROLLBAR_ACCESS_TOKEN_654321',
72+
'payload' => ['environment' => static::$kernel->getEnvironment()],
73+
'enabled' => true,
74+
'captureUncaught' => true,
75+
'uncaughtErrorLevel' => Configuration::JS_UNCAUGHT_LEVEL,
76+
'captureUnhandledRejections' => true,
77+
'ignoredMessages' => [],
78+
'verbose' => false,
79+
'async' => true,
80+
'autoInstrument' => Configuration::$autoInstrument,
81+
'itemsPerMinute' => Configuration::JS_ITEMS_PER_MINUTE,
82+
'maxItems' => Configuration::JS_MAX_ITEMS,
83+
'scrubFields' => Configuration::$scrubFieldsDefault,
84+
],
6985
];
7086

7187
$this->assertNotEmpty($config);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Tests\Twig;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6+
use SymfonyRollbarBundle\Twig\RollbarExtension;
7+
8+
/**
9+
* Class RollbarExtensionTest
10+
*
11+
* @package SymfonyRollbarBundle\Tests\Provider
12+
*/
13+
class RollbarExtensionTest extends KernelTestCase
14+
{
15+
/**
16+
* @dataProvider generatorRollbarEnv
17+
*
18+
* @param $env
19+
* @param $isDisabled
20+
*
21+
* @throws \ReflectionException
22+
*/
23+
public function testRollbarJs($env, $isDisabled)
24+
{
25+
static::bootKernel(['environment' => $env]);
26+
27+
$container = static::$kernel->getContainer();
28+
$rollbarTwig = new RollbarExtension($container);
29+
30+
$property = new \ReflectionProperty($rollbarTwig, 'config');
31+
$property->setAccessible(true);
32+
33+
$config = $property->getValue($rollbarTwig);
34+
$list = $rollbarTwig->getFunctions();
35+
36+
if ($isDisabled) {
37+
$this->assertNull($config);
38+
$this->assertEmpty($list);
39+
} else {
40+
$this->assertNotNull($config);
41+
$this->assertNotEmpty($list);
42+
43+
$function = $list[0];
44+
$this->assertInstanceOf(\Twig_Function::class, $function);
45+
46+
$output = $rollbarTwig->rollbarJs();
47+
$this->assertContains('_rollbarConfig', $output);
48+
$this->assertContains('SOME_ROLLBAR_ACCESS_TOKEN_654321', $output);
49+
}
50+
}
51+
52+
/**
53+
* @return array
54+
*/
55+
public function generatorRollbarEnv()
56+
{
57+
return [
58+
// env, is-empty-functions
59+
['test', false],
60+
// ['test_drb', true],
61+
];
62+
}
63+
}

Twig/RollbarExtension.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Twig;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use SymfonyRollbarBundle\DependencyInjection\SymfonyRollbarExtension;
7+
8+
class RollbarExtension extends \Twig_Extension
9+
{
10+
/**
11+
* @var \Symfony\Component\DependencyInjection\ContainerInterface
12+
*/
13+
protected $container;
14+
15+
/**
16+
* @var array
17+
*/
18+
protected $config;
19+
20+
/**
21+
* RollbarExtension constructor.
22+
*
23+
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
24+
*/
25+
public function __construct(ContainerInterface $container)
26+
{
27+
$this->container = $container;
28+
29+
try {
30+
$this->config = $container->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
31+
} catch (\Exception $e) {
32+
return null;
33+
}
34+
}
35+
36+
/**
37+
* @return array|\Twig_Function[]
38+
*/
39+
public function getFunctions()
40+
{
41+
if (empty($this->config)) {
42+
return [];
43+
}
44+
45+
return [
46+
new \Twig_Function('rollbarJs', [$this, 'rollbarJs']),
47+
];
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function rollbarJs()
54+
{
55+
$js = ''; // /vendor/rollbar/rollbar/data/rollbar.snippet.js
56+
$script = "<script>var _rollbarConfig = {{config}};\n{{rollbar-snippet}}</script>";
57+
$config = $this->config['rollbar_js'];
58+
59+
$script = strtr($script, [
60+
'{{config}}' => json_encode($config),
61+
'{{rollbar-snippet}}' => $js,
62+
]);
63+
64+
return $script;
65+
}
66+
}

0 commit comments

Comments
 (0)