Skip to content

Commit

Permalink
Merge pull request #10 from webrgp:feature/dx-improvements
Browse files Browse the repository at this point in the history
Enhance Ignition module with improved middleware and documentation
  • Loading branch information
webrgp authored Jan 3, 2025
2 parents 89d7316 + 291e451 commit aeaa867
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ includes:

parameters:
level: 5
editorUrl: 'vscode://file/%%file%%:%%line%%'
editorUrlTitle: '%%relFile%%:%%line%%'
reportUnmatchedIgnoredErrors: false
paths:
- src
2 changes: 1 addition & 1 deletion src/Ignition.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct($id = self::ID, $parent = null, $config = [])
public function bootstrap($app)
{
// Only bootstrap if this is a CraftWebApp
if (!($app instanceof CraftWebApp || $app instanceof CraftConsoleApp)) {
if (!($app instanceof CraftWebApp)) {
return;
}

Expand Down
32 changes: 24 additions & 8 deletions src/middleware/AddCraftInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@

namespace webrgp\ignition\middleware;

use Closure;
use Composer\InstalledVersions;
use Craft;
use craft\base\PluginInterface;
use craft\helpers\App;
use OutOfBoundsException;
use Spatie\FlareClient\FlareMiddleware\FlareMiddleware;
use Spatie\FlareClient\Report;
use yii\base\Module;

class AddCraftInfo
class AddCraftInfo implements FlareMiddleware
{
protected array $info = [];

public function __construct()
{
$this->info = [
$info = collect([
'appInfo' => self::_appInfo(),
'plugins' => self::_craftPlugins(),
'modules' => self::_craftModules(),
];
])
->filter(function($value) {
return !empty($value);
})
->toArray();

$this->info = $info;
}

public function handle(Report $report, $next)
public function handle(Report $report, Closure $next)
{
$report->setApplicationVersion($this->info["appInfo"]["Craft edition & version"]);
// Craft::dd($report);
Expand All @@ -41,16 +49,24 @@ public function handle(Report $report, $next)
*/
private static function _appInfo(): array
{
$craftEdition = version_compare(Craft::$app->getVersion(), '5.0.0', '<')
? App::editionName(Craft::$app->getEdition())
: Craft::$app->edition->name;
$craftEdition = Craft::$app->edition;
$craftVersion = Craft::$app->getVersion();

if (version_compare($craftVersion, '5.0.0', '>=')) {
// @phpstan-ignore property.nonObject
$craftEdition = $craftEdition->name;
}

if (version_compare($craftVersion, '5.0.0', '<')) {
$craftEdition = App::editionName((int) $craftEdition);
}

$info = [
'PHP version' => App::phpVersion(),
'OS version' => PHP_OS . ' ' . php_uname('r'),
'Database driver & version' => self::_dbDriver(),
'Image driver & version' => self::_imageDriver(),
'Craft edition & version' => sprintf('Craft %s %s', $craftEdition, Craft::$app->getVersion()),
'Craft edition & version' => sprintf('Craft %s %s', $craftEdition, $craftVersion),
];

if (!class_exists(InstalledVersions::class, false)) {
Expand Down
6 changes: 4 additions & 2 deletions src/middleware/CraftSensitiveKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace webrgp\ignition\middleware;

use Closure;
use Craft;
use Spatie\FlareClient\FlareMiddleware\FlareMiddleware;
use Spatie\FlareClient\Report;

class CraftSensitiveKeywords
class CraftSensitiveKeywords implements FlareMiddleware
{
public function handle(Report $report, $next)
public function handle(Report $report, Closure $next)
{
$context = $report->allContext();

Expand Down
27 changes: 16 additions & 11 deletions src/services/IgnitionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use craft\helpers\App;
use Spatie\FlareClient\Flare;
use Spatie\FlareClient\FlareMiddleware\CensorRequestHeaders;
use Spatie\FlareClient\FlareMiddleware\FlareMiddleware;
use Spatie\Ignition\Config\IgnitionConfig;
use Spatie\Ignition\Ignition as SpatieIgnition;
use Throwable;
use webrgp\ignition\Ignition;
use webrgp\ignition\middleware\AddCraftInfo;
use webrgp\ignition\middleware\CraftSensitiveKeywords;
use webrgp\ignition\models\IgnitionSettings;
Expand All @@ -22,7 +22,7 @@ class IgnitionRenderer extends Component
// Public Methods
// =========================================================================

public array $ignitionConfig = [];
public IgnitionConfig $ignitionConfig;

public ?string $applicationPath = null;

Expand All @@ -38,20 +38,18 @@ public function init(): void
parent::init();
}

public function handleException(Throwable $exception): void
public function handleException(Throwable $throwable): void
{
$this->ignition->handleException($exception);
$this->ignition->renderException($throwable);
}

/**
* Retrieves the Ignition configuration settings.
*
* This method collects various configuration settings for Ignition from the class properties
* or environment variables. It filters out any null values and returns the resulting array.
*
* @return array The Ignition configuration settings.
*/
private function getIgnitionConfig(): array
private function getIgnitionConfig(): IgnitionConfig
{
$config = new IgnitionSettings([
'editor' => App::env('CRAFT_IGNITION_EDITOR') ?? null,
Expand All @@ -66,7 +64,7 @@ private function getIgnitionConfig(): array

$config = array_filter($config->toArray(), fn($value) => $value !== null);

return $config;
return new IgnitionConfig($config);
}

/**
Expand All @@ -76,13 +74,11 @@ private function getIgnitionConfig(): array
* and applies the configuration if available. It also sets the application path,
* determines whether exceptions should be displayed based on the development mode,
* and specifies that the application is not running in a production environment.
*
* @return SpatieIgnition The configured Ignition instance.
*/
private function initIgnition(): SpatieIgnition
{
$ignition = SpatieIgnition::make();
$ignition->setConfig(new IgnitionConfig($this->ignitionConfig));
$ignition->setConfig($this->ignitionConfig);

$middlewares = self::getFlareMiddlewares();

Expand All @@ -95,6 +91,15 @@ private function initIgnition(): SpatieIgnition
});
}

/**
* Returns an array of Flare middlewares.
*
* This method returns an array of Flare middlewares that are used to modify the
* data that is sent to Flare. The middlewares are used to censor sensitive data
* and add Craft-specific information to the report.
*
* @return array<FlareMiddleware>
*/
private static function getFlareMiddlewares(): array
{
return [
Expand Down

0 comments on commit aeaa867

Please sign in to comment.