Skip to content

Commit 9d9d965

Browse files
committed
Craft 4 update
1 parent 37116c9 commit 9d9d965

File tree

10 files changed

+55
-83
lines changed

10 files changed

+55
-83
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.0.0 - 2022-07-19
4+
5+
### Changed
6+
- Now requires PHP `8.0.2+`.
7+
- Now requires Craft `4.0.0+`.
8+
39
## 3.0.2 - 2022-07-19
410

511
### Added
@@ -55,7 +61,7 @@
5561
- Parse `browserApiKey` and `serverApiKey` for env variables and aliases
5662

5763
### Fixed
58-
- Fixed frontend Bugsnag asset to comply with latest version of JS client
64+
- Fixed frontend Bugsnag asset to comply with the latest version of JS client
5965

6066
## 2.0.5 - 2019-08-27
6167

@@ -66,7 +72,7 @@
6672
- The plugin can now capture early initialization errors (if manually setup in `app.php`)
6773

6874
### Fixed
69-
- Fixed error when no items was added to exceptions blacklist
75+
- Fixed error when no items were added to exceptions blacklist
7076

7177
## 2.0.4 - 2019-03-14
7278

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You can also add the package to your project using Composer.
2222

2323
## Configuring Bugsnag
2424
1. Copy the config.php configuration file into your `craft/config` folder as **bugsnag.php**.
25-
2. Update `serverApiKey` with a API key from your Bugsnag project.
25+
2. Update `serverApiKey` with an API key from your Bugsnag project.
2626
3. (Optionally) Set the `releaseStage` configuration setting to something. Defaults to `production`.
2727

2828
If you want to be able to capture early initialization errors, you need to add this plugin to your project's bootstrap configuration. To do this, in `config/app.php`, add the following:
@@ -58,7 +58,7 @@ return [
5858
```
5959

6060
## Using Bugsnag
61-
It will automatically log most exceptions/errors. If you want to log a exceptions/error from an custom plugin, you may use the service methods:
61+
It will automatically log most exceptions/errors. If you want to log an exceptions/error from a custom plugin, you may use the service methods:
6262

6363
- For exceptions: `Bugsnag::$plugin->getService()->handleException($exception);`
6464

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "verbb/bugsnag",
33
"description": "Log Craft errors/exceptions to Bugsnag.",
44
"type": "craft-plugin",
5-
"version": "3.0.2",
5+
"version": "4.0.0",
66
"keywords": [
77
"craft",
88
"cms",
@@ -29,8 +29,9 @@
2929
}
3030
],
3131
"require": {
32-
"craftcms/cms": "^3.7.0",
33-
"verbb/base": "^1.0.2",
32+
"php": "^8.0.2",
33+
"craftcms/cms": "^4.0.0",
34+
"verbb/base": "^2.0.0",
3435
"bugsnag/bugsnag": "^3.16.0"
3536
},
3637
"autoload": {
@@ -41,7 +42,7 @@
4142
"extra": {
4243
"name": "Bugsnag",
4344
"handle": "bugsnag",
44-
"changelogUrl": "https://raw.githubusercontent.com/verbb/bugsnag/craft-3/CHANGELOG.md",
45+
"changelogUrl": "https://raw.githubusercontent.com/verbb/bugsnag/craft-4/CHANGELOG.md",
4546
"class": "verbb\\bugsnag\\Bugsnag"
4647
}
4748
}

src/Bugsnag.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,21 @@
88
use Craft;
99
use craft\base\Plugin;
1010
use craft\events\ExceptionEvent;
11-
use craft\events\PluginEvent;
1211
use craft\events\RegisterUrlRulesEvent;
1312
use craft\helpers\UrlHelper;
14-
use craft\services\Plugins;
1513
use craft\web\ErrorHandler;
1614
use craft\web\UrlManager;
1715
use craft\web\twig\variables\CraftVariable;
1816

1917
use yii\base\Event;
20-
use yii\base\InvalidConfigException;
2118

2219
class Bugsnag extends Plugin
2320
{
2421
// Properties
2522
// =========================================================================
2623

27-
public $schemaVersion = '2.0.0';
28-
public $hasCpSettings = true;
24+
public string $schemaVersion = '2.0.0';
25+
public bool $hasCpSettings = true;
2926

3027

3128
// Traits
@@ -55,9 +52,9 @@ public function getPluginName(): string
5552
return Craft::t('bugsnag', 'Bugsnag');
5653
}
5754

58-
public function getSettingsResponse()
55+
public function getSettingsResponse(): mixed
5956
{
60-
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('bugsnag/settings'));
57+
return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('bugsnag/settings'));
6158
}
6259

6360

@@ -73,14 +70,14 @@ protected function createSettingsModel(): Settings
7370
// Private Methods
7471
// =========================================================================
7572

76-
private function _registerVariables()
73+
private function _registerVariables(): void
7774
{
7875
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
7976
$event->sender->set('bugsnag', BugsnagVariable::class);
8077
});
8178
}
8279

83-
private function _registerCpRoutes()
80+
private function _registerCpRoutes(): void
8481
{
8582
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
8683
$event->rules = array_merge($event->rules, [
@@ -89,23 +86,21 @@ private function _registerCpRoutes()
8986
});
9087
}
9188

92-
private function _registerCraftEventListeners()
89+
private function _registerCraftEventListeners(): void
9390
{
9491
Event::on(ErrorHandler::class, ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, function(ExceptionEvent $event) {
9592
$settings = $this->getSettings();
9693

97-
if (is_array($settings->blacklist)) {
98-
foreach ($settings->blacklist as $config) {
99-
if (isset($config['class'])) {
100-
if (is_callable($config['class'])) {
101-
$result = $config['class']($event->exception);
94+
foreach ($settings->blacklist as $config) {
95+
if (isset($config['class'])) {
96+
if (is_callable($config['class'])) {
97+
$result = $config['class']($event->exception);
10298

103-
if (!$result) {
104-
return;
105-
}
106-
} else if ($event->exception instanceof $config['class']) {
99+
if (!$result) {
107100
return;
108101
}
102+
} else if ($event->exception instanceof $config['class']) {
103+
return;
109104
}
110105
}
111106
}

src/assetbundles/bugsnag/BugsnagAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class BugsnagAsset extends AssetBundle
1111
// Public Methods
1212
// =========================================================================
1313

14-
public function init()
14+
public function init(): void
1515
{
1616
$this->depends = [
1717
VerbbCpAsset::class,

src/base/PluginTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace verbb\bugsnag\base;
33

4+
use verbb\bugsnag\Bugsnag;
45
use verbb\bugsnag\services\Service;
56

67
use Craft;
@@ -14,7 +15,7 @@ trait PluginTrait
1415
// Static Properties
1516
// =========================================================================
1617

17-
public static $plugin;
18+
public static Bugsnag $plugin;
1819

1920

2021
// Public Methods

src/models/Settings.php

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,36 @@
55

66
use Craft;
77
use craft\base\Model;
8-
use craft\behaviors\EnvAttributeParserBehavior;
98
use craft\helpers\App;
109

11-
use yii\web\NotFoundHttpException;
12-
13-
use function is_callable;
14-
1510
class Settings extends Model
1611
{
1712
// Properties
1813
// =========================================================================
1914

20-
public $enabled = true;
21-
public $serverApiKey = '';
22-
public $browserApiKey = '';
23-
public $releaseStage = 'production';
24-
public $appVersion = '';
25-
public $notifyReleaseStages = ['production'];
26-
public $filters = ['password'];
27-
public $blacklist = [];
28-
public $metaData = [];
15+
public bool $enabled = true;
16+
public string $serverApiKey = '';
17+
public string $browserApiKey = '';
18+
public string $releaseStage = 'production';
19+
public string $appVersion = '';
20+
public array $notifyReleaseStages = ['production'];
21+
public array $filters = ['password'];
22+
public array $blacklist = [];
23+
public array $metaData = [];
2924

3025

3126
// Public Methods
3227
// =========================================================================
3328

3429
public function getBlacklist(): array
3530
{
36-
if (!is_array($this->blacklist)) {
37-
return [];
38-
}
39-
40-
$blacklist = array_map(function($row) {
31+
return array_filter(array_map(function($row) {
4132
if (isset($row['class']) && is_callable($row['class'])) {
4233
$row['class'] = 'Advanced check set through config file';
4334
}
4435

4536
return $row;
46-
}, $this->blacklist);
47-
48-
return array_filter($blacklist);
37+
}, $this->blacklist));
4938
}
5039

5140
public function isValidException($exception): bool
@@ -61,25 +50,6 @@ public function isValidException($exception): bool
6150
return $isValid;
6251
}
6352

64-
public function behaviors(): array
65-
{
66-
return [
67-
'parser' => [
68-
'class' => EnvAttributeParserBehavior::class,
69-
'attributes' => ['serverApiKey'],
70-
],
71-
];
72-
}
73-
74-
public function defineRules(): array
75-
{
76-
$rules = parent::defineRules();
77-
78-
$rules[] = [['serverApiKey'], 'required'];
79-
80-
return $rules;
81-
}
82-
8353
public function getBrowserConfig(): array
8454
{
8555
$data = [
@@ -109,22 +79,22 @@ public function getBrowserConfig(): array
10979
return $data;
11080
}
11181

112-
public function getEnabled()
82+
public function getEnabled(): bool|string|null
11383
{
11484
return App::parseEnv($this->enabled);
11585
}
11686

117-
public function getServerApiKey()
87+
public function getServerApiKey(): bool|string|null
11888
{
11989
return App::parseEnv($this->serverApiKey);
12090
}
12191

122-
public function getBrowserApiKey()
92+
public function getBrowserApiKey(): bool|string|null
12393
{
12494
return App::parseEnv($this->browserApiKey);
12595
}
12696

127-
public function getReleaseStage()
97+
public function getReleaseStage(): bool|string|null
12898
{
12999
return App::parseEnv($this->releaseStage);
130100
}

src/services/Service.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99

1010
use Bugsnag\Breadcrumbs\Breadcrumb;
1111
use Bugsnag\Client;
12-
use Bugsnag\Report;
1312

1413
class Service extends Component
1514
{
1615
// Properties
1716
// =========================================================================
1817

19-
/** @var Settings */
20-
private $settings;
18+
public array $metadata = [];
2119

22-
/** @var Client */
23-
private $bugsnag;
24-
25-
public $metadata = [];
20+
private ?Settings $settings = null;
21+
private ?Client $bugsnag = null;
2622

2723

2824
// Public Methods
@@ -40,7 +36,7 @@ public function init(): void
4036
$this->bugsnag->setNotifyReleaseStages($this->settings->notifyReleaseStages);
4137

4238
if (!empty($this->settings->filters)) {
43-
$this->bugsnag->setFilters($this->settings->filters);
39+
$this->bugsnag->setRedactedKeys($this->settings->filters);
4440
}
4541

4642
$this->bugsnag->registerCallback(function($report) {

src/translations/en/bugsnag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
'Release stage' => 'Release stage',
1111
'Blacklist' => 'Blacklist',
1212
'Disable certain types of exceptions. Class have to be fully namespaced. (i.e. \\yii\\web\\NotFoundHttpException)' => 'Disable certain types of exceptions. Class have to be fully namespaced. (i.e. \\yii\\web\\NotFoundHttpException)',
13+
'Enable exception logging' => 'Enable exception logging',
1314
];

src/variables/BugsnagVariable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use craft\helpers\Json;
88
use craft\helpers\Template;
99

10+
use Twig\Markup;
11+
1012
class BugsnagVariable
1113
{
1214
// Public Methods
@@ -22,7 +24,7 @@ public function metadata(array $data = []): Service
2224
return Bugsnag::$plugin->getService()->metadata($data);
2325
}
2426

25-
public function getBrowserConfig($asJson = true)
27+
public function getBrowserConfig($asJson = true): Markup|array
2628
{
2729
$config = Bugsnag::$plugin->getSettings()->getBrowserConfig();
2830

0 commit comments

Comments
 (0)