Skip to content

Commit

Permalink
Craft 4 update
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Jul 19, 2022
1 parent 37116c9 commit 9d9d965
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 83 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);`

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -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"
}
}
33 changes: 14 additions & 19 deletions src/Bugsnag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'));
}


Expand All @@ -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, [
Expand All @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/bugsnag/BugsnagAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BugsnagAsset extends AssetBundle
// Public Methods
// =========================================================================

public function init()
public function init(): void
{
$this->depends = [
VerbbCpAsset::class,
Expand Down
3 changes: 2 additions & 1 deletion src/base/PluginTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace verbb\bugsnag\base;

use verbb\bugsnag\Bugsnag;
use verbb\bugsnag\services\Service;

use Craft;
Expand All @@ -14,7 +15,7 @@ trait PluginTrait
// Static Properties
// =========================================================================

public static $plugin;
public static Bugsnag $plugin;


// Public Methods
Expand Down
60 changes: 15 additions & 45 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,36 @@

use Craft;
use craft\base\Model;
use craft\behaviors\EnvAttributeParserBehavior;
use craft\helpers\App;

use yii\web\NotFoundHttpException;

use function is_callable;

class Settings extends Model
{
// Properties
// =========================================================================

public $enabled = true;
public $serverApiKey = '';
public $browserApiKey = '';
public $releaseStage = 'production';
public $appVersion = '';
public $notifyReleaseStages = ['production'];
public $filters = ['password'];
public $blacklist = [];
public $metaData = [];
public bool $enabled = true;
public string $serverApiKey = '';
public string $browserApiKey = '';
public string $releaseStage = 'production';
public string $appVersion = '';
public array $notifyReleaseStages = ['production'];
public array $filters = ['password'];
public array $blacklist = [];
public array $metaData = [];


// Public Methods
// =========================================================================

public function getBlacklist(): array
{
if (!is_array($this->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
Expand All @@ -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 = [
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 4 additions & 8 deletions src/services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en/bugsnag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
4 changes: 3 additions & 1 deletion src/variables/BugsnagVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use craft\helpers\Json;
use craft\helpers\Template;

use Twig\Markup;

class BugsnagVariable
{
// Public Methods
Expand All @@ -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();

Expand Down

0 comments on commit 9d9d965

Please sign in to comment.