Skip to content

Commit 5c1b13e

Browse files
Merge pull request #55 from cloud-solutions/zend-sentry-update
Zend sentry update
2 parents 7052179 + 34ca66e commit 5c1b13e

File tree

10 files changed

+172
-207
lines changed

10 files changed

+172
-207
lines changed

.travis.yml-prep

Lines changed: 0 additions & 12 deletions
This file was deleted.

Module.php

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

33
/**
4-
* cloud solutions ZendSentry
4+
* Bright Answer ZendSentry
55
*
6-
* This source file is part of the cloud solutions ZendSentry package
6+
* This source file is part of the Bright Answer ZendSentry package
77
*
88
* @package ZendSentry\Module
9-
* @license New BSD License {@link /docs/LICENSE}
10-
* @copyright Copyright (c) 2013, cloud solutions
9+
* @license MIT License {@link /docs/LICENSE}
10+
* @copyright Copyright (c) 2018, Bright Answer
1111
*/
1212

1313
namespace ZendSentry;
@@ -30,7 +30,7 @@ class Module
3030
/**
3131
* Translates Zend Framework log levels to Raven log levels.
3232
*/
33-
private $logLevels = array(
33+
private $logLevels = [
3434
7 => Raven::DEBUG,
3535
6 => Raven::INFO,
3636
5 => Raven::INFO,
@@ -39,7 +39,7 @@ class Module
3939
2 => Raven::FATAL,
4040
1 => Raven::FATAL,
4141
0 => Raven::FATAL,
42-
);
42+
];
4343

4444
/**
4545
* @var Raven $ravenClient
@@ -64,7 +64,7 @@ class Module
6464
/**
6565
* @param MvcEvent $event
6666
*/
67-
public function onBootstrap(MvcEvent $event)
67+
public function onBootstrap(MvcEvent $event): void
6868
{
6969
// Setup RavenClient (provided by Sentry) and Sentry (provided by this module)
7070
$this->config = $event->getApplication()->getServiceManager()->get('Config');
@@ -73,10 +73,10 @@ public function onBootstrap(MvcEvent $event)
7373
return;
7474
}
7575

76-
if (isset($this->config['zend-sentry']['raven-config']) && is_array($this->config['zend-sentry']['raven-config'])) {
76+
if (isset($this->config['zend-sentry']['raven-config']) && \is_array($this->config['zend-sentry']['raven-config'])) {
7777
$ravenConfig = $this->config['zend-sentry']['raven-config'];
7878
} else {
79-
$ravenConfig = array();
79+
$ravenConfig = [];
8080
}
8181

8282
$sentryApiKey = $this->config['zend-sentry']['sentry-api-key'];
@@ -103,7 +103,7 @@ public function onBootstrap(MvcEvent $event)
103103

104104
// If ZendSentry is configured to log errors, register it as error handler
105105
if ($this->config['zend-sentry']['handle-errors']) {
106-
$errorReportingLevel = (isset($this->config['zend-sentry']['error-reporting'])) ? $this->config['zend-sentry']['error-reporting'] : -1;
106+
$errorReportingLevel = $this->config['zend-sentry']['error-reporting'] ?? -1;
107107
$this->zendSentry->registerErrorHandler($this->config['zend-sentry']['call-existing-error-handler'], $errorReportingLevel);
108108
}
109109

@@ -121,11 +121,15 @@ public function onBootstrap(MvcEvent $event)
121121
/**
122122
* @return array
123123
*/
124-
public function getAutoloaderConfig()
124+
public function getAutoloaderConfig(): array
125125
{
126-
return array('Zend\Loader\StandardAutoloader' => array('namespaces' => array(
127-
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
128-
)));
126+
return [
127+
'Zend\Loader\StandardAutoloader' => [
128+
'namespaces' => [
129+
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
130+
]
131+
]
132+
];
129133
}
130134

131135
/**
@@ -134,8 +138,7 @@ public function getAutoloaderConfig()
134138
public function getConfig()
135139
{
136140
return include __DIR__.'/config/module.config.php';
137-
}/** @noinspection PhpUnusedParameterInspection */
138-
/** @noinspection PhpUnusedParameterInspection */
141+
}
139142

140143
/**
141144
* Gives us the possibility to write logs to Sentry from anywhere in the application
@@ -144,7 +147,7 @@ public function getConfig()
144147
*
145148
* @param MvcEvent $event
146149
*/
147-
protected function setupBasicLogging(MvcEvent $event)
150+
protected function setupBasicLogging(MvcEvent $event): void
148151
{
149152
// Get the shared event manager and attach a logging listener for the log event on application level
150153
$sharedManager = $this->eventManager->getSharedManager();
@@ -153,18 +156,18 @@ protected function setupBasicLogging(MvcEvent $event)
153156

154157
$sharedManager->attach('*', 'log', function($event) use ($raven, $logLevels) {
155158
/** @var $event MvcEvent */
156-
if (is_object($event->getTarget())) {
157-
$target = get_class($event->getTarget());
159+
if (\is_object($event->getTarget())) {
160+
$target = \get_class($event->getTarget());
158161
} else {
159162
$target = (string) $event->getTarget();
160163
}
161164
$message = $event->getParam('message', 'No message provided');
162165
$priority = (int) $event->getParam('priority', Logger::INFO);
163166
$message = sprintf('%s: %s', $target, $message);
164-
$tags = $event->getParam('tags', array());
165-
$extra = $event->getParam('extra', array());
166-
$eventID = $raven->captureMessage($message, array(), array('tags' => $tags, 'level' => $logLevels[$priority], 'extra' => $extra));
167-
167+
$tags = $event->getParam('tags', []);
168+
$extra = $event->getParam('extra', []);
169+
$eventID = $raven->captureMessage($message, [], ['tags' => $tags, 'level' => $logLevels[$priority], 'extra' => $extra]
170+
);
168171
return $eventID;
169172
}, 2);
170173
}
@@ -175,7 +178,7 @@ protected function setupBasicLogging(MvcEvent $event)
175178
*
176179
* @param MvcEvent $event
177180
*/
178-
protected function setupExceptionLogging(MvcEvent $event)
181+
protected function setupExceptionLogging(MvcEvent $event): void
179182
{
180183
// Register Sentry as exception handler for exception that bubble up to the top
181184
$this->zendSentry->registerExceptionHandler($this->config['zend-sentry']['call-existing-exception-handler']);
@@ -186,22 +189,23 @@ protected function setupExceptionLogging(MvcEvent $event)
186189
$exceptionStrategy = $event->getApplication()->getServiceManager()->get('HttpExceptionStrategy');
187190
$exceptionStrategy->detach($this->eventManager);
188191
}
189-
192+
190193
// Check if script is running in console
191-
$exceptionStrategy = (PHP_SAPI == 'cli') ? (new SentryConsoleStrategy()) : (new SentryHttpStrategy());
194+
$exceptionStrategy = (PHP_SAPI == 'cli') ? new SentryConsoleStrategy() : new SentryHttpStrategy();
192195
$exceptionStrategy->attach($this->eventManager);
193196
$exceptionStrategy->setDisplayExceptions($this->config['zend-sentry']['display-exceptions']);
194197
$exceptionStrategy->setDefaultExceptionMessage($this->config['zend-sentry'][(PHP_SAPI == 'cli') ? 'default-exception-console-message' : 'default-exception-message']);
195-
198+
if ($exceptionStrategy instanceof SentryHttpStrategy && isset($this->config['view_manager']['exception_template'])) {
199+
$exceptionStrategy->setExceptionTemplate($this->config['view_manager']['exception_template']);
200+
}
196201
$ravenClient = $this->ravenClient;
197202

198203
// Attach an exception listener for the ZendSentry exception strategy, can be triggered from anywhere else too
199204
$this->eventManager->getSharedManager()->attach('*', 'logException', function($event) use ($ravenClient) {
200205
/** @var $event MvcEvent */
201206
$exception = $event->getParam('exception');
202-
$tags = $event->getParam('tags', array());
203-
$eventID = $ravenClient->captureException($exception, array('tags' => $tags));
204-
return $eventID;
207+
$tags = $event->getParam('tags', []);
208+
return $ravenClient->captureException($exception, ['tags' => $tags]);
205209
});
206210
}
207211

@@ -210,30 +214,37 @@ protected function setupExceptionLogging(MvcEvent $event)
210214
*
211215
* @param MvcEvent $event
212216
*/
213-
protected function setupJavascriptLogging(MvcEvent $event)
217+
protected function setupJavascriptLogging(MvcEvent $event): void
214218
{
215-
$viewHelper = $event->getApplication()->getServiceManager()->get('viewhelpermanager')->get('headscript');
216-
/** @noinspection PhpUndefinedMethodInspection */
217-
$viewHelper->offsetSetFile(0, '//cdn.ravenjs.com/3.17.0/raven.min.js');
219+
$viewHelper = $event->getApplication()->getServiceManager()->get('ViewHelperManager')->get('headscript');
220+
$useRavenjsCDN = $this->config['zend-sentry']['use-ravenjs-cdn'];
221+
if (!isset($useRavenjsCDN) || $useRavenjsCDN) {
222+
/** @noinspection PhpUndefinedMethodInspection */
223+
$viewHelper->offsetSetFile(0, '//cdn.ravenjs.com/3.26.2/raven.min.js');
224+
}
218225
$publicApiKey = $this->convertKeyToPublic($this->config['zend-sentry']['sentry-api-key']);
226+
$ravenjsConfig = json_encode($this->config['zend-sentry']['ravenjs-config']);
219227
/** @noinspection PhpUndefinedMethodInspection */
220-
$viewHelper->offsetSetScript(1, sprintf("Raven.config('%s').install()", $publicApiKey));
228+
$viewHelper->offsetSetScript(1, sprintf("if (typeof Raven !== 'undefined') Raven.config('%s', %s).install()", $publicApiKey, $ravenjsConfig));
221229
}
222230

223231
/**
224232
* @param string $key
225233
* @return string $publicKey
226234
*/
227-
private function convertKeyToPublic($key)
235+
private function convertKeyToPublic($key): string
228236
{
229-
// Find private part
237+
// If new DSN is configured, no converting is needed
238+
if (substr_count($key, ':') == 1) {
239+
return $key;
240+
}
241+
// If legacy DSN with private part is configured...
242+
// ...find private part
230243
$start = strpos($key, ':', 6);
231244
$end = strpos($key, '@');
232245
$privatePart = substr($key, $start, $end - $start);
233246

234-
// Replace it with an empty string
235-
$publicKey = str_replace($privatePart, '', $key);
236-
237-
return $publicKey;
247+
// ... replace it with an empty string
248+
return str_replace($privatePart, '', $key);
238249
}
239250
}

README.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
# NB! We're deprecating the 2.* version branch for ZF 2.
2-
3-
4-
A Zend Framework module that lets you log exceptions, errors or whatever you wish to the Sentry service.
1+
A Zend Framework 3 module that lets you log exceptions, errors or whatever you wish to the Sentry.io service.
52

63
Scrutizier analysis: [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/?branch=master) [![Build Status](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/badges/build.png?b=master)](https://scrutinizer-ci.com/g/cloud-solutions/zend-sentry/build-status/master)
74

8-
ZendSentry is released under the New BSD License.
5+
ZendSentry is released under the MIT License.
96

10-
The current version of ZendSentry for ZF2 is `2.4.0`. It supports Zend Framework >= 2.5.3. For older versions see the legacy branch and tags in the `1.*` series. For ZF3 compatible versions, please install releases in the `3.*` branch.
7+
The current version of ZendSentry for ZF3 is `3.5.0`. It supports Zend Framework >= 3.0. For other versions see tags in the 1.* series as well as 2.* series. **NB!** We are not supporting the old branches anymore.
118

12-
# Important Changes
9+
# Recent Changes
10+
- 3.5.0: Add support for new Sentry DSN, deprecate old DSN for later removal
11+
- 3.4.0: Add possibility to switch off usage of raven-js CDN
1312
- 3.3.0: Add possibility to pass config options to ravenjs
14-
- 3.2.0 and 2.4.0: Upgrade dependencies to `sentry/sentry` 1.7.0 and `ravenjs` 3.17.0
15-
- 3.0.1: ViewHelper fix
16-
- 3.0.0: First ZF2 release with latest sentry SDK dependencies and ZF3 compatibility fixes
17-
- 2.2.1: Fix: Only detach HttpExceptionStrategy if it exists
18-
- 2.0.0: New major version for ZF >=2.5.3
19-
- 1.5.2: Configurable error messages
20-
- 1.4.0: Raven configuration can now be overwritten through ZendSentry configuration if needed
21-
- 1.2.0: supports tags, every logging action returns the Sentry event_id, Raven is registered as Service
22-
- 0.3.1: dedicated CLI ExceptionStrategy (credits to Mateusz Mirosławski)
2313

2414
# Introduction
2515

@@ -28,10 +18,11 @@ The current version of ZendSentry for ZF2 is `2.4.0`. It supports Zend Framework
2818
exceptions and errors. Sentry creates nice reports in real time and aggregates your logged data for you.
2919

3020
## What's ZendSentry
31-
It is a module that builds the bridge between your Zend Framework 2 application and the Sentry service. It's extremely
21+
It is a module that builds the bridge between your Zend Framework 3 application and the Sentry.io service. It's extremely
3222
easy to setup and does a lot of things out-of-the-box.
3323

34-
Current features:
24+
Features and capabilities:
25+
3526
* log uncatched PHP exceptions to Sentry automagically
3627
* log PHP errors to Sentry automagically
3728
* log uncatched Javascript errors to Sentry automagically
@@ -41,7 +32,8 @@ Current features:
4132
* log actions return the Sentry event_id
4233
* Raven is registered as a Service
4334
* override Raven config defaults
44-
* set ravenjs options via config
35+
* pass config options to ravenjs
36+
* configure error messages
4537

4638
# Installation
4739

@@ -50,7 +42,7 @@ In your project's `composer.json` use:
5042

5143
{
5244
"require": {
53-
"cloud-solutions/zend-sentry": "2.4.0"
45+
"cloud-solutions/zend-sentry": "3.5.0"
5446
}
5547

5648
Run `php composer.phar update` to download it into your vendor folder and setup autoloading.
@@ -150,11 +142,11 @@ You might want to do something like this e.g. in your `AbstractActionController:
150142
$ravenClient->tags_context(
151143
[
152144
'locale' => $this->translator()->getLocale(),
153-
...
154145
]
155146
);
156147
}
157148

149+
158150
# Configuration options
159151

160152
Just for the record, a copy of the actual global configuration options:
@@ -221,10 +213,22 @@ Just for the record, a copy of the actual global configuration options:
221213
'handle-javascript-errors' => true,
222214

223215
/**
224-
* Set raven config options here.
216+
* Should ZendSentry load raven-js via CDN?
217+
* If you set this to false you'll need to make sure to load raven-js some other way.
218+
*/
219+
'use-ravenjs-cdn' => true,
220+
221+
/**
222+
* Set raven config options for the getsentry/sentry-php package here.
225223
* Raven has sensible defaults set in Raven_Client, if you need to override them, this is where you can do it.
226224
*/
227225
'raven-config' => array(),
226+
227+
/**
228+
* Set ravenjs config options for the getsentry/raven-js package here.
229+
* This will be json encoded and passed to raven-js when doing Raven.install().
230+
*/
231+
'ravenjs-config' => array(),
228232

229233
# Try it
230234
A few ideas how to try the different features from a Controller or View:

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "cloud-solutions/zend-sentry",
3-
"description": "A Zend Framework module that lets you log to the Sentry service.",
3+
"description": "A Zend Framework 3 module that lets you log to the Sentry.io service.",
44
"keywords": ["log", "logging", "sentry", "raven", "zend-framework"],
55
"homepage": "https://github.com/cloud-solutions/zend-sentry",
66
"type": "library",
7-
"version": "2.4.0",
7+
"version": "3.5.0",
88
"license": "MIT",
99
"authors": [
1010
{
@@ -13,11 +13,11 @@
1313
}
1414
],
1515
"require": {
16-
"php": "^5.5 || ^7.0",
17-
"sentry/sentry": "1.7.0"
16+
"php": "^5.6 || ^7.0",
17+
"sentry/sentry": "^1.9.2"
1818
},
1919
"conflict": {
20-
"zendframework/zendframework": "<2.5.3"
20+
"zendframework/zendframework": "<3.0.0"
2121
},
2222
"autoload": {
2323
"psr-0": {

config/module.config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?php
2-
return array(
3-
);
2+
return [
3+
];

config/zend-sentry.global.php.dist

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,22 @@ $settings = array(
6868
'handle-javascript-errors' => true,
6969

7070
/**
71-
* Set raven config options here.
71+
* Should ZendSentry load raven-js via CDN?
72+
* If you set this to false you'll need to make sure to load raven-js some other way.
73+
*/
74+
'use-ravenjs-cdn' => true,
75+
76+
/**
77+
* Set raven config options for the getsentry/sentry-php package here.
7278
* Raven has sensible defaults set in Raven_Client, if you need to override them, this is where you can do it.
7379
*/
7480
'raven-config' => array(),
81+
82+
/**
83+
* Set ravenjs config options for the getsentry/raven-js package here.
84+
* This will be json encoded and passed to raven-js when doing Raven.install().
85+
*/
86+
'ravenjs-config' => array(),
7587
);
7688

7789
/**

0 commit comments

Comments
 (0)