├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── UPGRADE.md
├── composer.json
├── config
├── bootstrap.php
├── di.php
├── events-console.php
└── params.php
└── src
├── SentryConsoleHandler.php
└── SentryMiddleware.php
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Yii Sentry Change Log
2 |
3 | ## 3.0.0 under development
4 |
5 | - Chg #40: Add support for PHP 8.4 (@batyrmastyr)
6 | - Chg #40: Bump `sentry/sentry` version to `^4.0` (@batyrmastyr)
7 |
8 | ## 2.0.1 July 15, 2024
9 |
10 | - Enh #35: Add support for `psr/http-message` version `^2.0` (@bautrukevich)
11 |
12 | ## 2.0.0 February 15, 2023
13 |
14 | - Chg #25: Adapt configuration group names to Yii conventions (@vjik)
15 | - Enh #22: Explicitly add transitive dependencies (@xepozz)
16 |
17 | ## 1.0.0 July 22, 2022
18 |
19 | - Initial release.
20 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright © 2008 by Yii Software ()
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions
6 | are met:
7 |
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in
12 | the documentation and/or other materials provided with the
13 | distribution.
14 | * Neither the name of Yii Software nor the names of its
15 | contributors may be used to endorse or promote products derived
16 | from this software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 | POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Yii Sentry
9 |
10 |
11 |
12 | [](https://packagist.org/packages/yiisoft/yii-sentry)
13 | [](https://packagist.org/packages/yiisoft/yii-sentry)
14 | [](https://github.com/yiisoft/yii-sentry/actions?query=workflow%3Abuild)
15 | [](https://codecov.io/gh/yiisoft/yii-sentry)
16 | [](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/yii-sentry/master)
17 | [](https://github.com/yiisoft/yii-sentry/actions?query=workflow%3A%22static+analysis%22)
18 |
19 | The package provides [Sentry](https://sentry.io/) integration for [Yii Framework](https://www.yiiframework.com/).
20 |
21 | ## Requirements
22 |
23 | - PHP 8.0 or higher.
24 |
25 | ## Installation
26 |
27 | The package could be installed with [Composer](https://getcomposer.org).
28 |
29 | The package needs PSR-compatible HTTP client and factories so require it additionally to this package:
30 |
31 | ```shell
32 | composer install httpsoft/http-message
33 | composer install php-http/guzzle7-adapter
34 | composer install yiisoft/yii-sentry
35 | ```
36 |
37 | The first two can be replaced to other packages of your choice.
38 |
39 | For handling console errors `yii-console` and `yii-event` packages are required additionally:
40 |
41 | ```shell
42 | composer install yiisoft/yii-console
43 | composer install yiisoft/yii-event
44 | ```
45 |
46 | Add `SentryMiddleware` to main application middleware set and configure DSN in `config/params.php`. Console errors
47 | are captured by default, there is no need to configure anything.
48 |
49 | ```php
50 | return [
51 | // ...
52 | 'middlewares' => [
53 | ErrorCatcher::class,
54 | SentryMiddleware::class, // <-- here
55 | SessionMiddleware::class,
56 | CookieMiddleware::class,
57 | CookieLoginMiddleware::class,
58 | LocaleMiddleware::class,
59 | Router::class,
60 | ],
61 | // ...
62 | 'yiisoft/yii-sentry' => [
63 | 'handleConsoleErrors' => false, // Add to disable console errors.
64 | 'options' => [
65 | // Set to `null` to disable error sending (note that in case of web application errors it only prevents
66 | // sending them via HTTP). To disable interactions with Sentry SDK completely, remove middleware and the
67 | // rest of the config.
68 | 'dsn' => $_ENV['SENTRY_DSN'] ?? null,
69 | 'environment' => $_ENV['YII_ENV'] ?? null, // Add to separate "production" / "staging" environment errors.
70 | ],
71 | ],
72 | // ...
73 | ]
74 | ```
75 |
76 | Note that fatal errors are handled too.
77 |
78 | In `options` you can also pass additional Sentry configuration. See
79 | [official Sentry docs](https://docs.sentry.io/platforms/php/configuration/options/) for keys and values.
80 |
81 | ## Documentation
82 |
83 | - [Internals](docs/internals.md)
84 |
85 | If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
86 | You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
87 |
88 | ## License
89 |
90 | The Yii Sentry is free software. It is released under the terms of the BSD License.
91 | Please see [`LICENSE`](./LICENSE.md) for more information.
92 |
93 | Maintained by [Yii Software](https://www.yiiframework.com/).
94 |
95 | ## Support the project
96 |
97 | [](https://opencollective.com/yiisoft)
98 |
99 | ## Follow updates
100 |
101 | [](https://www.yiiframework.com/)
102 | [](https://twitter.com/yiiframework)
103 | [](https://t.me/yii3en)
104 | [](https://www.facebook.com/groups/yiitalk)
105 | [](https://yiiframework.com/go/slack)
106 |
--------------------------------------------------------------------------------
/UPGRADE.md:
--------------------------------------------------------------------------------
1 | Upgrading Instructions for Yii Sentry
2 | =====================================
3 |
4 | Upgrade from 2.0
5 | -----------------------
6 | * Consult Sentry 4.x upgrade guide https://github.com/getsentry/sentry-php/blob/4.0.0/UPGRADE-4.0.md. Some notable changes are:
7 | * Configuration options (`['yiisoft/yii-sentry']['options']`) changes: `send_attempts`, `ignore_errors`, `logger` and `enable_compression`.
8 | * In case of custom DI configuration of HTTP client, `TransportFactoryInterface` or logger you may need to update your DI configuration like this:
9 | ```php di/sentry.php
10 | Options::class => [
11 | 'class' => Options::class,
12 | '__construct()' => [
13 | $params['yiisoft/yii-sentry']['options'],
14 | ],
15 | 'setTransport()' => Reference::to(CustomTransportInterfaceImplementation::class),
16 | 'setHttpClient()' => Reference::to(CustomHttpClient::class),
17 | 'setLogger()' => Reference::to(CustomLoggerInterfaceImplementation::class),
18 | ],
19 | ```
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "yiisoft/yii-sentry",
3 | "type": "library",
4 | "description": "A Sentry integration for Yii Framework",
5 | "keywords": [
6 | "yii",
7 | "sentry",
8 | "sentry-sdk"
9 | ],
10 | "homepage": "https://www.yiiframework.com/",
11 | "license": "BSD-3-Clause",
12 | "support": {
13 | "issues": "https://github.com/yiisoft/yii-sentry/issues?state=open",
14 | "source": "https://github.com/yiisoft/yii-sentry",
15 | "forum": "https://www.yiiframework.com/forum/",
16 | "wiki": "https://www.yiiframework.com/wiki/",
17 | "irc": "ircs://irc.libera.chat:6697/yii",
18 | "chat": "https://t.me/yii3en"
19 | },
20 | "funding": [
21 | {
22 | "type": "opencollective",
23 | "url": "https://opencollective.com/yiisoft"
24 | },
25 | {
26 | "type": "github",
27 | "url": "https://github.com/sponsors/yiisoft"
28 | }
29 | ],
30 | "require": {
31 | "php": "^8.0",
32 | "psr/http-message": "^1.0|^2.0",
33 | "psr/http-server-handler": "^1.0",
34 | "psr/http-server-middleware": "^1.0",
35 | "sentry/sentry": "^4.0",
36 | "symfony/console": "^5.4|^6.0",
37 | "yiisoft/di": "^1.0"
38 | },
39 | "require-dev": {
40 | "guzzlehttp/guzzle": "^7.3",
41 | "httpsoft/http-message": "^1.0.9",
42 | "maglnet/composer-require-checker": "^4.2",
43 | "php-http/guzzle7-adapter": "^1.0",
44 | "phpunit/phpunit": "^9.5",
45 | "rector/rector": "^2.0.10",
46 | "roave/infection-static-analysis-plugin": "^1.16",
47 | "spatie/phpunit-watcher": "^1.23",
48 | "vimeo/psalm": "^4.30|^5.6",
49 | "yiisoft/error-handler": "^2.1",
50 | "yiisoft/yii-console": "^1.0|^2.0",
51 | "yiisoft/yii-event": "^1.0|^2.0"
52 | },
53 | "suggest": {
54 | "yiisoft/yii-console": "Add error catching to console application",
55 | "yiisoft/yii-event": "Add error catching to console application"
56 | },
57 | "autoload": {
58 | "psr-4": {
59 | "Yiisoft\\Yii\\Sentry\\": "src"
60 | }
61 | },
62 | "autoload-dev": {
63 | "psr-4": {
64 | "Yiisoft\\Yii\\Sentry\\Tests\\": "tests"
65 | }
66 | },
67 | "extra": {
68 | "config-plugin-options": {
69 | "source-directory": "config"
70 | },
71 | "config-plugin": {
72 | "params": "params.php",
73 | "bootstrap": "bootstrap.php",
74 | "di": "di.php",
75 | "events-console": "events-console.php"
76 | }
77 | },
78 | "config": {
79 | "sort-packages": true,
80 | "allow-plugins": {
81 | "infection/extension-installer": true,
82 | "composer/package-versions-deprecated": true
83 | }
84 | },
85 | "scripts": {
86 | "test": "phpunit --testdox --no-interaction",
87 | "test-watch": "phpunit-watcher watch"
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/config/bootstrap.php:
--------------------------------------------------------------------------------
1 | get(Options::class);
15 |
16 | $clientBuilder = new ClientBuilder($options);
17 | if ($options->getLogger() === null && $container->has(LoggerInterface::class)) {
18 | $clientBuilder->setLogger($container->get(LoggerInterface::class));
19 | }
20 |
21 | $client = $clientBuilder->getClient();
22 |
23 | $hub = $container->get(HubInterface::class);
24 | $hub->bindClient($client);
25 |
26 | SentrySdk::setCurrentHub($hub);
27 | },
28 | ];
29 |
--------------------------------------------------------------------------------
/config/di.php:
--------------------------------------------------------------------------------
1 | [
15 | 'class' => Options::class,
16 | '__construct()' => [
17 | $params['yiisoft/yii-sentry']['options'],
18 | ],
19 | ],
20 | HubInterface::class => Hub::class,
21 | ];
22 |
--------------------------------------------------------------------------------
/config/events-console.php:
--------------------------------------------------------------------------------
1 | [
24 | [SentryConsoleHandler::class, 'handle'],
25 | ],
26 | ];
27 |
--------------------------------------------------------------------------------
/config/params.php:
--------------------------------------------------------------------------------
1 | [
9 | 'handleConsoleErrors' => true,
10 | 'options' => [
11 | 'dsn' => null,
12 | 'before_send' => static function (Event $event): ?Event {
13 | foreach ($event->getExceptions() as $exception) {
14 | if ($exception->getType() === 'Yiisoft\ErrorHandler\Exception\ErrorException') {
15 | return null;
16 | }
17 | }
18 |
19 | return $event;
20 | },
21 | ],
22 | ],
23 | ];
24 |
--------------------------------------------------------------------------------
/src/SentryConsoleHandler.php:
--------------------------------------------------------------------------------
1 | hub->captureException($event->getError());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/SentryMiddleware.php:
--------------------------------------------------------------------------------
1 | handle($request);
28 | } catch (Throwable $e) {
29 | $this->hub->captureException($e);
30 | throw $e;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------