Skip to content

Commit

Permalink
Merge branch 'release/4.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Oct 27, 2024
2 parents c00cf7b + 7af9a6a commit 0a47dc6
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
.env
/node_modules
/vendor
composer.lock
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Release Notes for Flare

## 4.0.0
- Initial release
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 - <br>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
<?php

return [
'*' => [
'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
````
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -33,7 +34,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,
Expand Down
88 changes: 88 additions & 0 deletions src/Flare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace studioespresso\flare;

use Craft;
use craft\base\Model;
use craft\base\Plugin;
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;

/**
* Flare plugin
*
* @method static Flare getInstance()
* @method Settings getSettings()
* @method FlareService flare
* @author Studio Espresso <support@studioespresso.co>
* @copyright Studio Espresso
* @license MIT
*/
class Flare extends Plugin
{
public string $schemaVersion = '1.0.0';
public bool $hasCpSettings = true;
/**
* @var mixed|object|null
*/

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) {
// $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);
}
);
}
});
}

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)
}
}
27 changes: 27 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @author studioespresso
* @package craft-flare
* @since 1.0.0
*/

/**
* Flare config.php
*
* This file exists only as a template for the Flare settings.
* It does nothing on its own.
*
* Don't edit this file, instead copy it to 'craft/config' as 'flare.php'
* and make your changes there to override default settings.
*
* Once copied to 'craft/config', this file will be multi-environment aware as
* well, so you can have different settings groups for each environment, just as
* you do for 'general.php'
*/

return [
'enabled' => true,
'apiKey' => '',
'excludedExceptions' => [],
];
35 changes: 35 additions & 0 deletions src/console/controllers/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace studioespresso\flare\console\controllers;

use craft\console\Controller;
use craft\helpers\Console;
use studioespresso\flare\Flare;
use yii\base\Exception;

/**
* Testing your Flare integration
*/
class TestController extends Controller
{
/**
* Once you have the Flare plugin installed & configured, use this function to throw an simulated exception to test if it shows on in Flare.
*/
public function actionIndex(): void
{
if (!Flare::getInstance()->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);
}
}
}
30 changes: 30 additions & 0 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace studioespresso\flare\models;

use craft\base\Model;

/**
* Flare settings
*/
class Settings extends Model
{
public bool $enabled = true;

public ?string $apiKey = null;

public array $excludedExceptions = [];
}
41 changes: 41 additions & 0 deletions src/services/FlareService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace studioespressp\flare\services;

use Craft;
use craft\base\Component;
use Spatie\FlareClient\Flare as FlareClient;
use studioespresso\flare\Flare;

class FlareService extends Component
{
public ?FlareClient $client = null;

public function init(): void
{
if (
Flare::getInstance()->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);
}
}
}
36 changes: 36 additions & 0 deletions src/templates/_settings.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{# @var plugin \studioespresso\flare\Flare #}
{# @var settings \studioespresso\flare\models\Settings #}

{% import '_includes/forms.twig' as forms %}
{% import _self as self %}


{{ forms.autosuggestfield({
label: 'API key'|t('flare'),
id: 'apiKey',
name: 'apiKey',
suggestEnvVars: true,
value: settings['apiKey'],
warning: self.configWarning('apiKey', 'flare')
}) }}

{{ forms.lightswitchField({
label: "Enabled"|t,
id: 'enabled',
name: 'enabled',
on: settings['enabled'],
warning: self.configWarning('enabled', 'flare')
}) }}



{% macro configWarning(setting, file) -%}
{%- set configArray = craft.app.config.getConfigFromFile(file) -%}
{%- if configArray[setting] is defined -%}
{{ "This is being overridden by the `{setting}` config setting."|raw|t('flare', {
setting: setting
}) }}
{%- else -%}
{{ false }}
{%- endif -%}
{%- endmacro %}

0 comments on commit 0a47dc6

Please sign in to comment.