From 9d9d965adb2e3c33d55b1f312d523340706c91db Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Tue, 19 Jul 2022 23:33:32 +1000 Subject: [PATCH] Craft 4 update --- CHANGELOG.md | 10 +++- README.md | 4 +- composer.json | 9 ++-- src/Bugsnag.php | 33 ++++++------- src/assetbundles/bugsnag/BugsnagAsset.php | 2 +- src/base/PluginTrait.php | 3 +- src/models/Settings.php | 60 ++++++----------------- src/services/Service.php | 12 ++--- src/translations/en/bugsnag.php | 1 + src/variables/BugsnagVariable.php | 4 +- 10 files changed, 55 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e08ac0..e1d09d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 4.0.0 - 2022-07-19 + +### Changed +- Now requires PHP `8.0.2+`. +- Now requires Craft `4.0.0+`. + ## 3.0.2 - 2022-07-19 ### Added @@ -55,7 +61,7 @@ - Parse `browserApiKey` and `serverApiKey` for env variables and aliases ### Fixed -- Fixed frontend Bugsnag asset to comply with latest version of JS client +- Fixed frontend Bugsnag asset to comply with the latest version of JS client ## 2.0.5 - 2019-08-27 @@ -66,7 +72,7 @@ - The plugin can now capture early initialization errors (if manually setup in `app.php`) ### Fixed -- Fixed error when no items was added to exceptions blacklist +- Fixed error when no items were added to exceptions blacklist ## 2.0.4 - 2019-03-14 diff --git a/README.md b/README.md index 63aa0be..114a133 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ You can also add the package to your project using Composer. ## Configuring Bugsnag 1. Copy the config.php configuration file into your `craft/config` folder as **bugsnag.php**. -2. Update `serverApiKey` with a API key from your Bugsnag project. +2. Update `serverApiKey` with an API key from your Bugsnag project. 3. (Optionally) Set the `releaseStage` configuration setting to something. Defaults to `production`. 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 [ ``` ## Using Bugsnag -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: +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: - For exceptions: `Bugsnag::$plugin->getService()->handleException($exception);` diff --git a/composer.json b/composer.json index 96a761c..7a49ff2 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "verbb/bugsnag", "description": "Log Craft errors/exceptions to Bugsnag.", "type": "craft-plugin", - "version": "3.0.2", + "version": "4.0.0", "keywords": [ "craft", "cms", @@ -29,8 +29,9 @@ } ], "require": { - "craftcms/cms": "^3.7.0", - "verbb/base": "^1.0.2", + "php": "^8.0.2", + "craftcms/cms": "^4.0.0", + "verbb/base": "^2.0.0", "bugsnag/bugsnag": "^3.16.0" }, "autoload": { @@ -41,7 +42,7 @@ "extra": { "name": "Bugsnag", "handle": "bugsnag", - "changelogUrl": "https://raw.githubusercontent.com/verbb/bugsnag/craft-3/CHANGELOG.md", + "changelogUrl": "https://raw.githubusercontent.com/verbb/bugsnag/craft-4/CHANGELOG.md", "class": "verbb\\bugsnag\\Bugsnag" } } diff --git a/src/Bugsnag.php b/src/Bugsnag.php index 9d787f2..1c4d6d6 100644 --- a/src/Bugsnag.php +++ b/src/Bugsnag.php @@ -8,24 +8,21 @@ use Craft; use craft\base\Plugin; use craft\events\ExceptionEvent; -use craft\events\PluginEvent; use craft\events\RegisterUrlRulesEvent; use craft\helpers\UrlHelper; -use craft\services\Plugins; use craft\web\ErrorHandler; use craft\web\UrlManager; use craft\web\twig\variables\CraftVariable; use yii\base\Event; -use yii\base\InvalidConfigException; class Bugsnag extends Plugin { // Properties // ========================================================================= - public $schemaVersion = '2.0.0'; - public $hasCpSettings = true; + public string $schemaVersion = '2.0.0'; + public bool $hasCpSettings = true; // Traits @@ -55,9 +52,9 @@ public function getPluginName(): string return Craft::t('bugsnag', 'Bugsnag'); } - public function getSettingsResponse() + public function getSettingsResponse(): mixed { - Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('bugsnag/settings')); + return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('bugsnag/settings')); } @@ -73,14 +70,14 @@ protected function createSettingsModel(): Settings // Private Methods // ========================================================================= - private function _registerVariables() + private function _registerVariables(): void { Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) { $event->sender->set('bugsnag', BugsnagVariable::class); }); } - private function _registerCpRoutes() + private function _registerCpRoutes(): void { Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) { $event->rules = array_merge($event->rules, [ @@ -89,23 +86,21 @@ private function _registerCpRoutes() }); } - private function _registerCraftEventListeners() + private function _registerCraftEventListeners(): void { Event::on(ErrorHandler::class, ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, function(ExceptionEvent $event) { $settings = $this->getSettings(); - if (is_array($settings->blacklist)) { - foreach ($settings->blacklist as $config) { - if (isset($config['class'])) { - if (is_callable($config['class'])) { - $result = $config['class']($event->exception); + foreach ($settings->blacklist as $config) { + if (isset($config['class'])) { + if (is_callable($config['class'])) { + $result = $config['class']($event->exception); - if (!$result) { - return; - } - } else if ($event->exception instanceof $config['class']) { + if (!$result) { return; } + } else if ($event->exception instanceof $config['class']) { + return; } } } diff --git a/src/assetbundles/bugsnag/BugsnagAsset.php b/src/assetbundles/bugsnag/BugsnagAsset.php index 9e2a927..5458bf6 100644 --- a/src/assetbundles/bugsnag/BugsnagAsset.php +++ b/src/assetbundles/bugsnag/BugsnagAsset.php @@ -11,7 +11,7 @@ class BugsnagAsset extends AssetBundle // Public Methods // ========================================================================= - public function init() + public function init(): void { $this->depends = [ VerbbCpAsset::class, diff --git a/src/base/PluginTrait.php b/src/base/PluginTrait.php index d1703ad..163bb38 100644 --- a/src/base/PluginTrait.php +++ b/src/base/PluginTrait.php @@ -1,6 +1,7 @@ blacklist)) { - return []; - } - - $blacklist = array_map(function($row) { + return array_filter(array_map(function($row) { if (isset($row['class']) && is_callable($row['class'])) { $row['class'] = 'Advanced check set through config file'; } return $row; - }, $this->blacklist); - - return array_filter($blacklist); + }, $this->blacklist)); } public function isValidException($exception): bool @@ -61,25 +50,6 @@ public function isValidException($exception): bool return $isValid; } - public function behaviors(): array - { - return [ - 'parser' => [ - 'class' => EnvAttributeParserBehavior::class, - 'attributes' => ['serverApiKey'], - ], - ]; - } - - public function defineRules(): array - { - $rules = parent::defineRules(); - - $rules[] = [['serverApiKey'], 'required']; - - return $rules; - } - public function getBrowserConfig(): array { $data = [ @@ -109,22 +79,22 @@ public function getBrowserConfig(): array return $data; } - public function getEnabled() + public function getEnabled(): bool|string|null { return App::parseEnv($this->enabled); } - public function getServerApiKey() + public function getServerApiKey(): bool|string|null { return App::parseEnv($this->serverApiKey); } - public function getBrowserApiKey() + public function getBrowserApiKey(): bool|string|null { return App::parseEnv($this->browserApiKey); } - public function getReleaseStage() + public function getReleaseStage(): bool|string|null { return App::parseEnv($this->releaseStage); } diff --git a/src/services/Service.php b/src/services/Service.php index 9eb60e9..3aacdcf 100644 --- a/src/services/Service.php +++ b/src/services/Service.php @@ -9,20 +9,16 @@ use Bugsnag\Breadcrumbs\Breadcrumb; use Bugsnag\Client; -use Bugsnag\Report; class Service extends Component { // Properties // ========================================================================= - /** @var Settings */ - private $settings; + public array $metadata = []; - /** @var Client */ - private $bugsnag; - - public $metadata = []; + private ?Settings $settings = null; + private ?Client $bugsnag = null; // Public Methods @@ -40,7 +36,7 @@ public function init(): void $this->bugsnag->setNotifyReleaseStages($this->settings->notifyReleaseStages); if (!empty($this->settings->filters)) { - $this->bugsnag->setFilters($this->settings->filters); + $this->bugsnag->setRedactedKeys($this->settings->filters); } $this->bugsnag->registerCallback(function($report) { diff --git a/src/translations/en/bugsnag.php b/src/translations/en/bugsnag.php index 245fc6e..5b42b26 100644 --- a/src/translations/en/bugsnag.php +++ b/src/translations/en/bugsnag.php @@ -10,4 +10,5 @@ 'Release stage' => 'Release stage', 'Blacklist' => 'Blacklist', '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)', + 'Enable exception logging' => 'Enable exception logging', ]; diff --git a/src/variables/BugsnagVariable.php b/src/variables/BugsnagVariable.php index a097bc4..1e0267f 100644 --- a/src/variables/BugsnagVariable.php +++ b/src/variables/BugsnagVariable.php @@ -7,6 +7,8 @@ use craft\helpers\Json; use craft\helpers\Template; +use Twig\Markup; + class BugsnagVariable { // Public Methods @@ -22,7 +24,7 @@ public function metadata(array $data = []): Service return Bugsnag::$plugin->getService()->metadata($data); } - public function getBrowserConfig($asJson = true) + public function getBrowserConfig($asJson = true): Markup|array { $config = Bugsnag::$plugin->getSettings()->getBrowserConfig();