From cc0813b080ed9cc660e22dc7ae2a601658db199c Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 24 Oct 2024 10:38:58 +0200 Subject: [PATCH 1/6] Added license --- LICENSE.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..30c3235 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Pixel & Tonic, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 4973b4c3add598dbc91fe6e5b6f6ac3e789382a2 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Thu, 24 Oct 2024 20:12:45 +0200 Subject: [PATCH 2/6] Settings template & config example --- src/config.php | 27 +++++++++++++++++++++++++++ src/models/Settings.php | 17 +++++++++++++++++ src/templates/_settings.twig | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/config.php create mode 100644 src/models/Settings.php create mode 100644 src/templates/_settings.twig diff --git a/src/config.php b/src/config.php new file mode 100644 index 0000000..9ccae41 --- /dev/null +++ b/src/config.php @@ -0,0 +1,27 @@ + true, + 'apiKey' => '', + 'excludedExceptions' => [], +]; diff --git a/src/models/Settings.php b/src/models/Settings.php new file mode 100644 index 0000000..1c0695f --- /dev/null +++ b/src/models/Settings.php @@ -0,0 +1,17 @@ + Date: Sat, 26 Oct 2024 08:28:21 +0200 Subject: [PATCH 3/6] Main plugin class --- src/Flare.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/Flare.php diff --git a/src/Flare.php b/src/Flare.php new file mode 100644 index 0000000..79ae276 --- /dev/null +++ b/src/Flare.php @@ -0,0 +1,82 @@ + + * @copyright Studio Espresso + * @license MIT + */ +class Flare extends Plugin +{ + public string $schemaVersion = '1.0.0'; + public bool $hasCpSettings = true; + + public static function config(): array + { + return [ + 'components' => [ + 'flare' => FlareService::class, + ], + ]; + } + + public function init(): void + { + parent::init(); + + if (Craft::$app instanceof ConsoleApplication) { + $this->controllerNamespace = 'studioespresso\flare\console\controllers'; + } + + $this->attachEventHandlers(); + + // Any code that creates an element query or loads Twig should be deferred until + // after Craft is fully initialized, to avoid conflicts with other plugins/modules + Craft::$app->onInit(function() { + if ($this->getSettings()->enabled) { + Event::on( + ErrorHandler::className(), + ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, + function(ExceptionEvent $event) { + $this->flare->reportException($event->exception); + } + ); + } + }); + } + + protected function createSettingsModel(): ?Model + { + return Craft::createObject(Settings::class); + } + + protected function settingsHtml(): ?string + { + return Craft::$app->view->renderTemplate('flare/_settings.twig', [ + 'plugin' => $this, + 'settings' => $this->getSettings(), + ]); + } + + private function attachEventHandlers(): void + { + // Register event handlers here ... + // (see https://craftcms.com/docs/4.x/extend/events.html to get started) + } +} From 2150290705da4160730db2400004a1711aca1e02 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Sun, 27 Oct 2024 11:40:12 +0100 Subject: [PATCH 4/6] Flare service --- .gitignore | 1 + composer.json | 3 +- src/Flare.php | 6 ++++ src/console/controllers/TestController.php | 35 ++++++++++++++++++ src/services/FlareService.php | 41 ++++++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/console/controllers/TestController.php create mode 100644 src/services/FlareService.php diff --git a/.gitignore b/.gitignore index 8423dac..0945b50 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ .env /node_modules /vendor +composer.lock diff --git a/composer.json b/composer.json index c48e82d..b4ea3fe 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "scripts": { "check-cs": "ecs check --ansi", "fix-cs": "ecs check --ansi --fix", - "phpstan": "phpstan --memory-limit=1G" + "phpstan": "phpstan --memory-limit=1G", + "ci": "ecs check --ansi --fix && phpstan --memory-limit=1G" }, "config": { "sort-packages": true, diff --git a/src/Flare.php b/src/Flare.php index 79ae276..b80861b 100644 --- a/src/Flare.php +++ b/src/Flare.php @@ -8,6 +8,7 @@ use craft\console\Application as ConsoleApplication; use craft\events\ExceptionEvent; use craft\web\ErrorHandler; +use Spatie\FlareClient\Flare as FlareClient; use studioespresso\flare\models\Settings; use studioespressp\flare\services\FlareService; use yii\base\Event; @@ -26,6 +27,9 @@ class Flare extends Plugin { public string $schemaVersion = '1.0.0'; public bool $hasCpSettings = true; + /** + * @var mixed|object|null + */ public static function config(): array { @@ -50,10 +54,12 @@ public function init(): void // after Craft is fully initialized, to avoid conflicts with other plugins/modules Craft::$app->onInit(function() { if ($this->getSettings()->enabled) { +// $client = FlareClient::make($this->getSettings()->apiKey)->registerFlareHandlers(); Event::on( ErrorHandler::className(), ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, function(ExceptionEvent $event) { +// $client->report($event->exception); $this->flare->reportException($event->exception); } ); diff --git a/src/console/controllers/TestController.php b/src/console/controllers/TestController.php new file mode 100644 index 0000000..a1d4ca8 --- /dev/null +++ b/src/console/controllers/TestController.php @@ -0,0 +1,35 @@ +getSettings()->enabled) { + Console::stdout("Flare is not enabled on this environment." . PHP_EOL . "Check your settings or run this command where Flare is enabled." . PHP_EOL, Console::FG_YELLOW); + return; + } + if (!Flare::getInstance()->getSettings()->apiKey) { + Console::stdout("Flare API key missing." . PHP_EOL . "Check your settings or run this command where Flare is correctly configured" . PHP_EOL, Console::FG_RED); + return; + } + try { + throw new Exception("This is an exception throws while testing the Flare plugin for Craft CMS", 418); + } catch (\Throwable $e) { + Flare::getInstance()->flare->reportException($e); + Console::stdout("Exception reported to flareapp.io, check your project there to see if it shows up" . PHP_EOL); + } + } +} diff --git a/src/services/FlareService.php b/src/services/FlareService.php new file mode 100644 index 0000000..0e5d5b0 --- /dev/null +++ b/src/services/FlareService.php @@ -0,0 +1,41 @@ +getSettings()->apiKey && + Flare::getInstance()->getSettings()->enabled + ) { + $this->client = FlareClient::make(Flare::getInstance()->getSettings()->apiKey)->registerFlareHandlers(); + } + parent::init(); + } + + public function reportException(\Throwable $exception) + { + $settings = Flare::getInstance()->getSettings(); + if (in_array(get_class($exception), $settings->excludedExceptions)) { + Craft::info('Exception class excluded from being reported to Flare.', Flare::getInstance()->handle); + return; + } + + if ($this->client) { + $this->client->context('CMS', 'Craft CMS'); + $this->client->context('System name', Craft::$app->getSystemName()); + $this->client->context('Craft version', Craft::$app->getInfo()->version); + $this->client->context('Yii version', Craft::$app->getYiiVersion()); + $this->client->report($exception); + } + } +} From e35b60fbb4d3de9c77308c63c4c1da008f9842ad Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Sun, 27 Oct 2024 11:52:41 +0100 Subject: [PATCH 5/6] Icon update --- src/icon.svg | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/icon.svg diff --git a/src/icon.svg b/src/icon.svg new file mode 100644 index 0000000..29237c6 --- /dev/null +++ b/src/icon.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 7af9a6af003bf26081d6fca8b435ab92515b4e98 Mon Sep 17 00:00:00 2001 From: Jan Henckens Date: Sun, 27 Oct 2024 17:14:08 +0100 Subject: [PATCH 6/6] Changelog & readme --- CHANGELOG.md | 4 ++++ README.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 1 + 3 files changed, 61 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 README.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6e550ac --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +# Release Notes for Flare + +## 4.0.0 +- Initial release diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d8726c --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# Flare + +Flareapp.io integration for Craft CMS + +## Requirements + +This plugin requires Craft CMS 4.12.0 or later, and PHP 8.1 or later -
and an account at [flareapp.io](https://flareapp.io/?via=studioespresso) *(Affiliate link, you're supporting further maintenance of this plugin by signing up through that link - thanks!)* + +## Installation + +You can install this plugin from the Plugin Store or with Composer. + +#### From the Plugin Store + +Go to the Plugin Store in your project’s Control Panel and search for “Flare”. Then press “Install”. + +#### With Composer + +Open your terminal and run the following commands: + +```shell +cd /path/to/my-project.test +(ddev/php) composer require studioespresso/craft-flare +(ddev/php) craft plugin/install flare +``` + +## Configuration +Create a project in your Flare account, copy the API key and add it in the plugin settings. + +Alternatively, you can use the ``flare.php`` configuration file to manage this. This file is environment-aware, like any other Craft CMS configuration. You can find an example below. + +````php + [ + 'enabled' => false, + 'apiKey' => \craft\helpers\App::env('FLARE_API_KEY'), + 'excludedExceptions' => [ + ErrorException::class, + ] + ], + 'production' => [ + 'enabled' => true, + ] +]; +```` + +## Testing +Once you've added the API key for your Flare project, you can use a built-in console command to send a test exception to Flare: + +````shell +(ddev/php) craft flare/test/index +# That should result on the following message: +Exception reported to flareapp.io, check your project there to see if it shows up +```` diff --git a/composer.json b/composer.json index b4ea3fe..e3dd8b6 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "Flare.io integration for Craft CMS", "type": "craft-plugin", "license": "mit", + "version": "4.0.0", "support": { "email": "support@studioespresso.co", "issues": "https://github.com/studioespresso/craft-flare/issues?state=open",