Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,168 @@
<?php
namespace Facade\Ignition\Actions;
use Exception;
use Facade\FlareClient\Http\Client;
use Facade\FlareClient\Truncation\ReportTrimmer;
use Facade\Ignition\Exceptions\UnableToShareErrorException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class ShareReportAction
{
/** @var array */
protected $tabs;
/** @var \Facade\FlareClient\Http\Client */
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function handle(array $report, array $tabs, ?string $lineSelection = null)
{
$this->tabs = $tabs;
$report = $this->filterReport($report);
try {
return $this->client->post('public-reports', [
'report' => $this->trimReport($report),
'tabs' => $tabs,
'lineSelection' => $lineSelection,
]);
} catch (Exception $exception) {
throw new UnableToShareErrorException($exception->getMessage());
}
}
public function filterReport(array $report): array
{
if (! $this->hasTab('stackTraceTab')) {
$report['stacktrace'] = array_slice($report['stacktrace'], 0, 1);
}
if (! $this->hasTab('debugTab')) {
$report['glows'] = [];
}
$report['context'] = $this->filterContextItems($report['context']);
return $report;
}
protected function hasTab(string $tab): bool
{
return in_array($tab, $this->tabs);
}
protected function filterContextItems(array $contextItems): array
{
if (! $this->hasTab('requestTab')) {
$contextItems = $this->removeRequestInformation($contextItems);
}
if (! $this->hasTab('appTab')) {
$contextItems = $this->removeAppInformation($contextItems);
}
if (! $this->hasTab('userTab')) {
$contextItems = $this->removeUserInformation($contextItems);
}
if (! $this->hasTab('contextTab')) {
$contextItems = $this->removeContextInformation($contextItems);
}
if (! $this->hasTab('debugTab')) {
$contextItems = $this->removeDebugInformation($contextItems);
}
return $contextItems;
}
protected function removeRequestInformation(array $contextItems): array
{
Arr::forget($contextItems, 'request');
Arr::forget($contextItems, 'request_data');
Arr::forget($contextItems, 'headers');
Arr::forget($contextItems, 'session');
Arr::forget($contextItems, 'cookies');
return $contextItems;
}
protected function removeAppInformation(array $contextItems): array
{
Arr::forget($contextItems, 'view');
Arr::forget($contextItems, 'route');
return $contextItems;
}
protected function removeUserInformation(array $contextItems): array
{
Arr::forget($contextItems, 'user');
Arr::forget($contextItems, 'request.ip');
Arr::forget($contextItems, 'request.useragent');
return $contextItems;
}
protected function removeContextInformation(array $contextItems): array
{
Arr::forget($contextItems, 'env');
Arr::forget($contextItems, 'git');
Arr::forget($contextItems, 'context');
Arr::forget($contextItems, $this->getCustomContextGroups($contextItems));
return $contextItems;
}
protected function removeDebugInformation(array $contextItems): array
{
Arr::forget($contextItems, 'dumps');
Arr::forget($contextItems, 'glows');
Arr::forget($contextItems, 'logs');
Arr::forget($contextItems, 'queries');
return $contextItems;
}
protected function getCustomContextGroups(array $contextItems): array
{
$predefinedContextItemGroups = [
'request',
'request_data',
'headers',
'session',
'cookies',
'view',
'queries',
'route',
'user',
'env',
'git',
'context',
'logs',
'dumps',
'exception',
];
return Collection::make($contextItems)
->reject(function ($_value, $group) use ($predefinedContextItemGroups) {
return in_array($group, $predefinedContextItemGroups);
})
->keys()
->toArray();
}
protected function trimReport(array $report): array
{
return (new ReportTrimmer())->trim($report);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Facade\Ignition\Commands;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
class SolutionMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'ignition:make-solution';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new custom Ignition solution class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Solution';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->option('runnable')
? __DIR__.'/stubs/runnable-solution.stub'
: __DIR__.'/stubs/solution.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Solutions';
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['runnable', null, InputOption::VALUE_NONE, 'Create runnable solution'],
];
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Facade\Ignition\Commands;
use Illuminate\Console\GeneratorCommand;
class SolutionProviderMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'ignition:make-solution-provider';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new custom Ignition solution provider class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Solution Provider';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/solution-provider.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\SolutionProviders';
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Facade\Ignition\Commands;
use Composer\InstalledVersions;
use Exception;
use Facade\FlareClient\Flare;
use Facade\FlareClient\Http\Exceptions\BadResponseCode;
use Illuminate\Config\Repository;
use Illuminate\Console\Command;
use Illuminate\Log\LogManager;
class TestCommand extends Command
{
protected $signature = 'flare:test';
protected $description = 'Send a test notification to Flare';
/** @var \Illuminate\Config\Repository */
protected $config;
public function handle(Repository $config)
{
$this->config = $config;
$this->checkFlareKey();
if (app()->make('log') instanceof LogManager) {
$this->checkFlareLogger();
}
$this->sendTestException();
}
protected function checkFlareKey()
{
$message = empty($this->config->get('flare.key'))
? '❌ Flare key not specified. Make sure you specify a value in the `key` key of the `flare` config file.'
: '✅ Flare key specified';
$this->info($message);
return $this;
}
public function checkFlareLogger()
{
$defaultLogChannel = $this->config->get('logging.default');
$activeStack = $this->config->get("logging.channels.{$defaultLogChannel}");
if (is_null($activeStack)) {
$this->info("❌ The default logging channel `{$defaultLogChannel}` is not configured in the `logging` config file");
}
if (! isset($activeStack['channels']) || ! in_array('flare', $activeStack['channels'])) {
$this->info("❌ The logging channel `{$defaultLogChannel}` does not contain the 'flare' channel");
}
if (is_null($this->config->get('logging.channels.flare'))) {
$this->info('❌ There is no logging channel named `flare` in the `logging` config file');
}
if ($this->config->get('logging.channels.flare.driver') !== 'flare') {
$this->info('❌ The `flare` logging channel defined in the `logging` config file is not set to `flare`.');
}
$this->info('✅ The Flare logging driver was configured correctly.');
return $this;
}
protected function sendTestException()
{
$testException = new Exception('This is an exception to test if the integration with Flare works.');
try {
app(Flare::class)->sendTestReport($testException);
$this->info('');
} catch (Exception $exception) {
$this->warn('❌ We were unable to send an exception to Flare. ');
if ($exception instanceof BadResponseCode) {
$this->info('');
$message = 'Unknown error';
$body = $exception->response->getBody();
if (is_array($body) && isset($body['message'])) {
$message = $body['message'];
}
$this->warn("{$exception->response->getHttpResponseCode()} - {$message}");
} else {
$this->warn($exception->getMessage());
}
$this->warn('Make sure that your key is correct and that you have a valid subscription.');
$this->info('');
$this->info('For more info visit the docs on https://flareapp.io/docs/ignition-for-laravel/introduction');
$this->info('You can see the status page of Flare at https://status.flareapp.io');
$this->info('Flare support can be reached at support@flareapp.io');
$this->line('');
$this->line('Extra info');
$this->table([], [
['Platform', PHP_OS],
['PHP', phpversion()],
['Laravel', app()->version()],
['facade/ignition', InstalledVersions::getVersion('facade/ignition')],
['facade/flare-client-php', InstalledVersions::getVersion('facade/flare-client-php')],
['Curl', curl_version()['version']],
['SSL', curl_version()['ssl_version']],
]);
if ($this->output->isVerbose()) {
throw $exception;
}
return;
}
$this->info('We tried to send an exception to Flare. Please check if it arrived!');
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace DummyNamespace;
use Facade\IgnitionContracts\RunnableSolution;
class DummyClass implements RunnableSolution
{
public function getSolutionTitle(): string
{
return '';
}
public function getDocumentationLinks(): array
{
return [];
}
public function getSolutionActionDescription(): string
{
return '';
}
public function getRunButtonText(): string
{
return '';
}
public function getSolutionDescription(): string
{
return '';
}
public function getRunParameters(): array
{
return [];
}
public function run(array $parameters = [])
{
//
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace DummyNamespace;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
class DummyClass implements HasSolutionsForThrowable
{
public function canSolve(): bool
{
return false;
}
public function getSolutions(): array
{
return [];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace DummyNamespace;
use Facade\IgnitionContracts\Solution;
class DummyClass implements Solution
{
public function getSolutionTitle(): string
{
return '';
}
public function getSolutionDescription(): string
{
return '';
}
public function getDocumentationLinks(): array
{
return [];
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Facade\Ignition\Context;
use Facade\FlareClient\Context\ConsoleContext;
class LaravelConsoleContext extends ConsoleContext
{
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Facade\Ignition\Context;
use Facade\FlareClient\Context\ContextDetectorInterface;
use Facade\FlareClient\Context\ContextInterface;
use Illuminate\Http\Request;
use Livewire\LivewireManager;
class LaravelContextDetector implements ContextDetectorInterface
{
public function detectCurrentContext(): ContextInterface
{
if (app()->runningInConsole()) {
return new LaravelConsoleContext($_SERVER['argv'] ?? []);
}
$request = app(Request::class);
if ($this->isRunningLiveWire($request)) {
return new LivewireRequestContext($request, app(LivewireManager::class));
}
return new LaravelRequestContext($request);
}
protected function isRunningLiveWire(Request $request)
{
return $request->hasHeader('x-livewire') && $request->hasHeader('referer');
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Facade\Ignition\Context;
use Facade\FlareClient\Context\RequestContext;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Throwable;
class LaravelRequestContext extends RequestContext
{
/** @var \Illuminate\Http\Request */
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function getUser(): array
{
try {
$user = $this->request->user();
if (! $user) {
return [];
}
} catch (Throwable $e) {
return [];
}
try {
if (method_exists($user, 'toFlare')) {
return $user->toFlare();
}
if (method_exists($user, 'toArray')) {
return $user->toArray();
}
} catch (Throwable $e) {
return [];
}
return [];
}
public function getRoute(): array
{
$route = $this->request->route();
return [
'route' => optional($route)->getName(),
'routeParameters' => $this->getRouteParameters(),
'controllerAction' => optional($route)->getActionName(),
'middleware' => array_values(optional($route)->gatherMiddleware() ?? []),
];
}
protected function getRouteParameters(): array
{
try {
return collect(optional($this->request->route())->parameters ?? [])
->map(function ($parameter) {
return $parameter instanceof Model ? $parameter->withoutRelations() : $parameter;
})
->map(function ($parameter) {
return method_exists($parameter, 'toFlare') ? $parameter->toFlare() : $parameter;
})
->toArray();
} catch (Throwable $e) {
return [];
}
}
public function toArray(): array
{
$properties = parent::toArray();
$properties['route'] = $this->getRoute();
$properties['user'] = $this->getUser();
return $properties;
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Facade\Ignition\Context;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Livewire\LivewireManager;
class LivewireRequestContext extends LaravelRequestContext
{
/** @var \Livewire\LivewireManager */
protected $livewireManager;
public function __construct(
Request $request,
LivewireManager $livewireManager
) {
parent::__construct($request);
$this->livewireManager = $livewireManager;
}
public function getRequest(): array
{
$properties = parent::getRequest();
$properties['method'] = $this->livewireManager->originalMethod();
$properties['url'] = $this->livewireManager->originalUrl();
return $properties;
}
public function toArray(): array
{
$properties = parent::toArray();
$properties['livewire'] = $this->getLiveWireInformation();
return $properties;
}
protected function getLiveWireInformation(): array
{
$componentId = $this->request->input('fingerprint.id');
$componentAlias = $this->request->input('fingerprint.name');
if ($componentAlias === null) {
return [];
}
try {
$componentClass = $this->livewireManager->getClass($componentAlias);
} catch (Exception $e) {
$componentClass = null;
}
return [
'component_class' => $componentClass,
'component_alias' => $componentAlias,
'component_id' => $componentId,
'data' => $this->resolveData(),
'updates' => $this->resolveUpdates(),
];
}
protected function resolveData(): array
{
$data = $this->request->input('serverMemo.data') ?? [];
$dataMeta = $this->request->input('serverMemo.dataMeta') ?? [];
foreach ($dataMeta['modelCollections'] ?? [] as $key => $value) {
$data[$key] = array_merge($data[$key] ?? [], $value);
}
foreach ($dataMeta['models'] ?? [] as $key => $value) {
$data[$key] = array_merge($data[$key] ?? [], $value);
}
return $data;
}
protected function resolveUpdates()
{
$updates = $this->request->input('updates') ?? [];
return array_map(function (array $update) {
$update['payload'] = Arr::except($update['payload'] ?? [], ['id']);
return $update;
}, $updates);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Facade\Ignition\DumpRecorder;
class Dump
{
/** @var string */
protected $htmlDump;
/** @var ?string */
protected $file;
/** @var ?int */
protected $lineNumber;
/** @var float */
protected $microtime;
public function __construct(string $htmlDump, ?string $file, ?int $lineNumber, ?float $microtime = null)
{
$this->htmlDump = $htmlDump;
$this->file = $file;
$this->lineNumber = $lineNumber;
$this->microtime = $microtime ?? microtime(true);
}
public function toArray(): array
{
return [
'html_dump' => $this->htmlDump,
'file' => $this->file,
'line_number' => $this->lineNumber,
'microtime' => $this->microtime,
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Facade\Ignition\DumpRecorder;
use Symfony\Component\VarDumper\Cloner\VarCloner;
class DumpHandler
{
/** @var \Facade\Ignition\DumpRecorder\DumpRecorder */
protected $dumpRecorder;
public function __construct(DumpRecorder $dumpRecorder)
{
$this->dumpRecorder = $dumpRecorder;
}
public function dump($value)
{
$data = (new VarCloner())->cloneVar($value);
$this->dumpRecorder->record($data);
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace Facade\Ignition\DumpRecorder;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Arr;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper;
use Symfony\Component\VarDumper\VarDumper;
class DumpRecorder
{
protected $dumps = [];
/** @var \Illuminate\Contracts\Foundation\Application */
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function register(): self
{
$multiDumpHandler = new MultiDumpHandler();
$this->app->singleton(MultiDumpHandler::class, function () use ($multiDumpHandler) {
return $multiDumpHandler;
});
$previousHandler = VarDumper::setHandler(function ($var) use ($multiDumpHandler) {
$multiDumpHandler->dump($var);
});
if ($previousHandler) {
$multiDumpHandler->addHandler($previousHandler);
} else {
$multiDumpHandler->addHandler($this->getDefaultHandler());
}
$multiDumpHandler->addHandler(function ($var) {
(new DumpHandler($this))->dump($var);
});
return $this;
}
public function record(Data $data)
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 8);
$file = (string)Arr::get($backtrace, '6.file');
$lineNumber = (int)Arr::get($backtrace, '6.line');
if (! Arr::exists($backtrace, '7.class') && (string)Arr::get($backtrace, '7.function') === 'ddd') {
$file = (string)Arr::get($backtrace, '7.file');
$lineNumber = (int)Arr::get($backtrace, '7.line');
}
$htmlDump = (new HtmlDumper())->dump($data);
$this->dumps[] = new Dump($htmlDump, $file, $lineNumber);
}
public function getDumps(): array
{
return $this->toArray();
}
public function reset()
{
$this->dumps = [];
}
public function toArray(): array
{
$dumps = [];
foreach ($this->dumps as $dump) {
$dumps[] = $dump->toArray();
}
return $dumps;
}
protected function getDefaultHandler()
{
return function ($value) {
$data = (new VarCloner())->cloneVar($value);
$this->getDumper()->dump($data);
};
}
protected function getDumper()
{
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
if ($_SERVER['VAR_DUMPER_FORMAT'] === 'html') {
return new BaseHtmlDumper();
}
return new CliDumper();
}
if (in_array(PHP_SAPI, ['cli', 'phpdbg']) && ! isset($_SERVER['LARAVEL_OCTANE'])) {
return new CliDumper() ;
}
return new BaseHtmlDumper();
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Facade\Ignition\DumpRecorder;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper;
class HtmlDumper extends BaseHtmlDumper
{
protected $dumpHeader = '';
public function dumpVariable($variable): string
{
$cloner = new VarCloner();
$clonedData = $cloner->cloneVar($variable)->withMaxDepth(3);
return $this->dump($clonedData);
}
public function dump(Data $data, $output = null, array $extraDisplayOptions = []): string
{
return (string)parent::dump($data, true, [
'maxDepth' => 3,
'maxStringLength' => 160,
]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Facade\Ignition\DumpRecorder;
class MultiDumpHandler
{
/** @var array */
protected $handlers = [];
public function dump($value)
{
foreach ($this->handlers as $handler) {
$handler($value);
}
}
public function addHandler(callable $callable = null): self
{
$this->handlers[] = $callable;
return $this;
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Facade\Ignition\ErrorPage;
use Facade\FlareClient\Flare;
use Facade\FlareClient\Report;
use Facade\Ignition\IgnitionConfig;
use Facade\IgnitionContracts\SolutionProviderRepository;
use Illuminate\Foundation\Application;
use Throwable;
class ErrorPageHandler
{
/** @var \Facade\Ignition\IgnitionConfig */
protected $ignitionConfig;
/** @var \Facade\FlareClient\Flare */
protected $flareClient;
/** @var \Facade\Ignition\ErrorPage\Renderer */
protected $renderer;
/** @var \Facade\IgnitionContracts\SolutionProviderRepository */
protected $solutionProviderRepository;
public function __construct(
Application $app,
IgnitionConfig $ignitionConfig,
Renderer $renderer,
SolutionProviderRepository $solutionProviderRepository
) {
$this->flareClient = $app->make(Flare::class);
$this->ignitionConfig = $ignitionConfig;
$this->renderer = $renderer;
$this->solutionProviderRepository = $solutionProviderRepository;
}
public function handle(Throwable $throwable, $defaultTab = null, $defaultTabProps = [])
{
$report = $this->flareClient->createReport($throwable);
$solutions = $this->solutionProviderRepository->getSolutionsForThrowable($throwable);
$viewModel = new ErrorPageViewModel(
$throwable,
$this->ignitionConfig,
$report,
$solutions
);
$viewModel->defaultTab($defaultTab, $defaultTabProps);
$this->renderException($viewModel);
}
public function handleReport(Report $report, $defaultTab = null, $defaultTabProps = [])
{
$viewModel = new ErrorPageViewModel(
$report->getThrowable(),
$this->ignitionConfig,
$report,
[]
);
$viewModel->defaultTab($defaultTab, $defaultTabProps);
$this->renderException($viewModel);
}
protected function renderException(ErrorPageViewModel $exceptionViewModel)
{
echo $this->renderer->render(
'errorPage',
$exceptionViewModel->toArray()
);
}
}

View File

@@ -0,0 +1,200 @@
<?php
namespace Facade\Ignition\ErrorPage;
use Closure;
use Exception;
use Facade\FlareClient\Report;
use Facade\Ignition\Ignition;
use Facade\Ignition\IgnitionConfig;
use Facade\Ignition\Solutions\SolutionTransformer;
use Illuminate\Contracts\Support\Arrayable;
use Laravel\Telescope\Http\Controllers\HomeController;
use Laravel\Telescope\IncomingExceptionEntry;
use Laravel\Telescope\Telescope;
use Throwable;
class ErrorPageViewModel implements Arrayable
{
/** @var \Throwable|null */
protected $throwable;
/** @var array */
protected $solutions;
/** @var \Facade\Ignition\IgnitionConfig */
protected $ignitionConfig;
/** @var \Facade\FlareClient\Report */
protected $report;
/** @var string */
protected $defaultTab;
/** @var array */
protected $defaultTabProps = [];
/** @var string */
protected $appEnv;
/** @var bool */
protected $appDebug;
public function __construct(?Throwable $throwable, IgnitionConfig $ignitionConfig, Report $report, array $solutions)
{
$this->throwable = $throwable;
$this->ignitionConfig = $ignitionConfig;
$this->report = $report;
$this->solutions = $solutions;
$this->appEnv = config('app.env');
$this->appDebug = config('app.debug');
}
public function throwableString(): string
{
if (! $this->throwable) {
return '';
}
$throwableString = sprintf(
"%s: %s in file %s on line %d\n\n%s\n",
get_class($this->throwable),
$this->throwable->getMessage(),
$this->throwable->getFile(),
$this->throwable->getLine(),
$this->report->getThrowable()->getTraceAsString()
);
return htmlspecialchars($throwableString);
}
public function telescopeUrl(): ?string
{
try {
if (! class_exists(Telescope::class)) {
return null;
}
if (! count(Telescope::$entriesQueue)) {
return null;
}
$telescopeEntry = collect(Telescope::$entriesQueue)->first(function ($entry) {
return $entry instanceof IncomingExceptionEntry;
});
if (is_null($telescopeEntry)) {
return null;
}
$telescopeEntryId = (string) $telescopeEntry->uuid;
return url(action([HomeController::class, 'index'])."/exceptions/{$telescopeEntryId}");
} catch (Exception $exception) {
return null;
}
}
public function title(): string
{
$message = htmlspecialchars($this->report->getMessage());
return "🧨 {$message}";
}
public function config(): array
{
return $this->ignitionConfig->toArray();
}
public function solutions(): array
{
$solutions = [];
foreach ($this->solutions as $solution) {
$solutions[] = (new SolutionTransformer($solution))->toArray();
}
return $solutions;
}
protected function shareEndpoint(): string
{
try {
// use string notation as L5.5 and L5.6 don't support array notation yet
return action('\Facade\Ignition\Http\Controllers\ShareReportController');
} catch (Exception $exception) {
return '';
}
}
public function report(): array
{
return $this->report->toArray();
}
public function jsonEncode($data): string
{
$jsonOptions = JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
return json_encode($data, $jsonOptions);
}
public function getAssetContents(string $asset): string
{
$assetPath = __DIR__."/../../resources/compiled/{$asset}";
return file_get_contents($assetPath);
}
public function styles(): array
{
return array_keys(Ignition::styles());
}
public function scripts(): array
{
return array_keys(Ignition::scripts());
}
public function tabs(): string
{
return json_encode(Ignition::$tabs);
}
public function defaultTab(?string $defaultTab, ?array $defaultTabProps)
{
$this->defaultTab = $defaultTab ?? 'StackTab';
if ($defaultTabProps) {
$this->defaultTabProps = $defaultTabProps;
}
}
public function toArray(): array
{
return [
'throwableString' => $this->throwableString(),
'telescopeUrl' => $this->telescopeUrl(),
'shareEndpoint' => $this->shareEndpoint(),
'title' => $this->title(),
'config' => $this->config(),
'solutions' => $this->solutions(),
'report' => $this->report(),
'housekeepingEndpoint' => url(config('ignition.housekeeping_endpoint_prefix', '_ignition')),
'styles' => $this->styles(),
'scripts' => $this->scripts(),
'tabs' => $this->tabs(),
'jsonEncode' => Closure::fromCallable([$this, 'jsonEncode']),
'getAssetContents' => Closure::fromCallable([$this, 'getAssetContents']),
'defaultTab' => $this->defaultTab,
'defaultTabProps' => $this->defaultTabProps,
'appEnv' => $this->appEnv,
'appDebug' => $this->appDebug,
];
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Facade\Ignition\ErrorPage;
use Illuminate\Contracts\Foundation\ExceptionRenderer;
/** @psalm-suppress UndefinedClass */
class IgnitionExceptionRenderer implements ExceptionRenderer
{
/** @var \Facade\Ignition\ErrorPage\ErrorPageHandler */
protected $errorPageHandler;
public function __construct(ErrorPageHandler $errorPageHandler)
{
$this->errorPageHandler = $errorPageHandler;
}
public function render($throwable)
{
ob_start();
$this->errorPageHandler->handle($throwable);
return ob_get_clean();
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Facade\Ignition\ErrorPage;
use Error;
use ErrorException;
use Whoops\Handler\Handler;
class IgnitionWhoopsHandler extends Handler
{
/** @var \Facade\Ignition\ErrorPage\ErrorPageHandler */
protected $errorPageHandler;
/** @var \Throwable */
protected $exception;
public function __construct(ErrorPageHandler $errorPageHandler)
{
$this->errorPageHandler = $errorPageHandler;
}
public function handle(): ?int
{
try {
$this->errorPageHandler->handle($this->exception);
} catch (Error $error) {
// Errors aren't caught by Whoops.
// Convert the error to an exception and throw again.
throw new ErrorException(
$error->getMessage(),
$error->getCode(),
1,
$error->getFile(),
$error->getLine(),
$error
);
}
return Handler::QUIT;
}
/** @param \Throwable $exception */
public function setException($exception): void
{
$this->exception = $exception;
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Facade\Ignition\ErrorPage;
use Exception;
use Facade\Ignition\Exceptions\ViewException;
class Renderer
{
/** @var string */
protected $viewPath;
public function __construct(string $viewPath)
{
$this->viewPath = $this->formatPath($viewPath);
}
public function render(string $viewName, array $_data): string
{
ob_start();
$viewFile = "{$this->viewPath}/{$viewName}.php";
try {
extract($_data, EXTR_OVERWRITE);
include $viewFile;
} catch (Exception $exception) {
$viewException = new ViewException($exception->getMessage());
$viewException->setView($viewFile);
$viewException->setViewData($_data);
throw $viewException;
}
return ob_get_clean();
}
protected function formatPath(string $path): string
{
return preg_replace('/(?:\/)+$/u', '', $path).'/';
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Facade\Ignition\Exceptions;
use Exception;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
use Monolog\Logger;
class InvalidConfig extends Exception implements ProvidesSolution
{
public static function invalidLogLevel(string $logLevel)
{
return new static("Invalid log level `{$logLevel}` specified.");
}
public function getSolution(): Solution
{
$validLogLevels = array_map(function (string $level) {
return strtolower($level);
}, array_keys(Logger::getLevels()));
$validLogLevelsString = implode(',', $validLogLevels);
return BaseSolution::create('You provided an invalid log level')
->setSolutionDescription("Please change the log level in your `config/logging.php` file. Valid log levels are {$validLogLevelsString}.");
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace Facade\Ignition\Exceptions;
use Exception;
class UnableToShareErrorException extends Exception
{
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Facade\Ignition\Exceptions;
use ErrorException;
use Facade\FlareClient\Contracts\ProvidesFlareContext;
use Facade\Ignition\DumpRecorder\HtmlDumper;
class ViewException extends ErrorException implements ProvidesFlareContext
{
/** @var array */
protected $viewData = [];
/** @var string */
protected $view = '';
public function setViewData(array $data)
{
$this->viewData = $data;
}
public function getViewData(): array
{
return $this->viewData;
}
public function setView(string $path)
{
$this->view = $path;
}
protected function dumpViewData($variable): string
{
return (new HtmlDumper())->dumpVariable($variable);
}
public function context(): array
{
$context = [
'view' => [
'view' => $this->view,
],
];
if (config('flare.reporting.report_view_data')) {
$context['view']['data'] = array_map([$this, 'dumpViewData'], $this->viewData);
}
return $context;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Facade\Ignition\Exceptions;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
class ViewExceptionWithSolution extends ViewException implements ProvidesSolution
{
/** @var Solution */
protected $solution;
public function setSolution(Solution $solution)
{
$this->solution = $solution;
}
public function getSolution(): Solution
{
return $this->solution;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Facade\Ignition\Facades;
use Facade\Ignition\Support\SentReports;
use Illuminate\Support\Facades\Facade;
/**
* Class Flare.
*
* @method static void glow(string $name, string $messageLevel = \Facade\FlareClient\Enums\MessageLevels::INFO, array $metaData = [])
* @method static void context($key, $value)
* @method static void group(string $groupName, array $properties)
*
* @see \Facade\FlareClient\Flare
*/
class Flare extends Facade
{
protected static function getFacadeAccessor()
{
return \Facade\FlareClient\Flare::class;
}
public static function sentReports(): SentReports
{
return app(SentReports::class);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Facade\Ignition\Http\Controllers;
use Facade\Ignition\Http\Requests\ExecuteSolutionRequest;
use Facade\IgnitionContracts\SolutionProviderRepository;
use Illuminate\Foundation\Validation\ValidatesRequests;
class ExecuteSolutionController
{
use ValidatesRequests;
public function __invoke(
ExecuteSolutionRequest $request,
SolutionProviderRepository $solutionProviderRepository
) {
$this->ensureLocalEnvironment();
$this->ensureLocalRequest();
$solution = $request->getRunnableSolution();
$solution->run($request->get('parameters', []));
return response('');
}
public function ensureLocalEnvironment()
{
if (! app()->environment('local')) {
abort(403, "Runnable solutions are disabled in non-local environments. Please make sure `APP_ENV` is set correctly. Additionally please make sure `APP_DEBUG` is set to false on ANY production environment!");
}
}
public function ensureLocalRequest()
{
$ipIsPublic = filter_var(
request()->ip(),
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
);
if ($ipIsPublic) {
abort(403, "Solutions can only be executed by requests from a local IP address. Please also make sure `APP_DEBUG` is set to false on ANY production environment.");
}
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Facade\Ignition\Http\Controllers;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
class HealthCheckController
{
public function __invoke()
{
return [
'can_execute_commands' => $this->canExecuteCommands(),
];
}
protected function canExecuteCommands(): bool
{
Artisan::call('help', ['--version']);
$output = Artisan::output();
return Str::contains($output, app()->version());
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Facade\Ignition\Http\Controllers;
use Facade\Ignition\Ignition;
use Illuminate\Http\Request;
class ScriptController
{
public function __invoke(Request $request)
{
if (!isset(Ignition::scripts()[$request->script])) {
abort(404, 'Script not found');
}
return response(
file_get_contents(
Ignition::scripts()[$request->script]
),
200,
['Content-Type' => 'application/javascript']
);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Facade\Ignition\Http\Controllers;
use Facade\Ignition\Actions\ShareReportAction;
use Facade\Ignition\Exceptions\UnableToShareErrorException;
use Facade\Ignition\Http\Requests\ShareReportRequest;
class ShareReportController
{
public function __invoke(ShareReportRequest $request, ShareReportAction $shareReportAction)
{
try {
return $shareReportAction->handle(json_decode($request->get('report'), true), $request->get('tabs'), $request->get('lineSelection'));
} catch (UnableToShareErrorException $exception) {
abort(500, 'Unable to share the error '.$exception->getMessage());
}
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Facade\Ignition\Http\Controllers;
use Facade\Ignition\Ignition;
use Illuminate\Http\Request;
class StyleController
{
public function __invoke(Request $request)
{
return response(
file_get_contents(Ignition::styles()[$request->style]),
200,
['Content-Type' => 'text/css']
);
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Facade\Ignition\Http\Middleware;
use Closure;
use Facade\Ignition\IgnitionConfig;
use Illuminate\Http\Request;
class IgnitionConfigValueEnabled
{
/** @var \Facade\Ignition\IgnitionConfig */
protected $ignitionConfig;
public function __construct(IgnitionConfig $ignitionConfig)
{
$this->ignitionConfig = $ignitionConfig;
}
public function handle(Request $request, Closure $next, string $value)
{
if (! $this->ignitionConfig->toArray()[$value]) {
abort(404);
}
return $next($request);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Facade\Ignition\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class IgnitionEnabled
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (! $this->ignitionEnabled()) {
abort(404);
}
return $next($request);
}
protected function ignitionEnabled(): bool
{
return config('app.debug');
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Facade\Ignition\Http\Requests;
use Facade\IgnitionContracts\RunnableSolution;
use Facade\IgnitionContracts\Solution;
use Facade\IgnitionContracts\SolutionProviderRepository;
use Illuminate\Foundation\Http\FormRequest;
class ExecuteSolutionRequest extends FormRequest
{
public function rules(): array
{
return [
'solution' => 'required',
'parameters' => 'array',
];
}
public function getSolution(): Solution
{
$solution = app(SolutionProviderRepository::class)
->getSolutionForClass($this->get('solution'));
abort_if(is_null($solution), 404, 'Solution could not be found');
/** @var Solution */
return $solution;
}
public function getRunnableSolution(): RunnableSolution
{
$solution = $this->getSolution();
if (! $solution instanceof RunnableSolution) {
abort(404, 'Runnable solution could not be found');
}
return $solution;
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Facade\Ignition\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ShareReportRequest extends FormRequest
{
public function rules(): array
{
return [
'report' => 'required',
'tabs' => 'required|array|min:1',
'lineSelection' => [],
];
}
}

43
vendor/facade/ignition/src/Ignition.php vendored Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace Facade\Ignition;
use Closure;
use Facade\Ignition\Tabs\Tab;
class Ignition
{
/** @var Closure[] */
public static $callBeforeShowingErrorPage = [];
/** @var array */
public static $tabs = [];
public static function tab(Tab $tab)
{
static::$tabs[] = $tab;
}
public static function styles(): array
{
return collect(static::$tabs)->flatMap(function ($tab) {
return $tab->styles;
})
->unique()
->toArray();
}
public static function scripts(): array
{
return collect(static::$tabs)->flatMap(function ($tab) {
return $tab->scripts;
})
->unique()
->toArray();
}
public static function registerAssets(Closure $callable)
{
static::$callBeforeShowingErrorPage[] = $callable;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Facade\Ignition;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
class IgnitionConfig implements Arrayable
{
/** @var array */
protected $options;
public function __construct(array $options = [])
{
$this->options = $this->mergeWithDefaultConfig($options);
}
public function getEditor(): ?string
{
return Arr::get($this->options, 'editor');
}
public function getRemoteSitesPath(): ?string
{
return Arr::get($this->options, 'remote_sites_path');
}
public function getLocalSitesPath(): ?string
{
return Arr::get($this->options, 'local_sites_path');
}
public function getTheme(): ?string
{
return Arr::get($this->options, 'theme');
}
public function getEnableShareButton(): bool
{
if (! app()->isBooted()) {
return false;
}
return Arr::get($this->options, 'enable_share_button', true);
}
public function getEnableRunnableSolutions(): bool
{
$enabled = Arr::get($this->options, 'enable_runnable_solutions', null);
if ($enabled === null) {
$enabled = config('app.debug');
}
return $enabled ?? false;
}
public function toArray(): array
{
return [
'editor' => $this->getEditor(),
'remoteSitesPath' => $this->getRemoteSitesPath(),
'localSitesPath' => $this->getLocalSitesPath(),
'theme' => $this->getTheme(),
'enableShareButton' => $this->getEnableShareButton(),
'enableRunnableSolutions' => $this->getEnableRunnableSolutions(),
'directorySeparator' => DIRECTORY_SEPARATOR,
];
}
protected function mergeWithDefaultConfig(array $options = []): array
{
return array_merge(config('ignition') ?: include __DIR__.'/../config/ignition.php', $options);
}
}

View File

@@ -0,0 +1,559 @@
<?php
namespace Facade\Ignition;
use Exception;
use Facade\FlareClient\Api;
use Facade\FlareClient\Flare;
use Facade\FlareClient\Http\Client;
use Facade\Ignition\Commands\SolutionMakeCommand;
use Facade\Ignition\Commands\SolutionProviderMakeCommand;
use Facade\Ignition\Commands\TestCommand;
use Facade\Ignition\Context\LaravelContextDetector;
use Facade\Ignition\DumpRecorder\DumpRecorder;
use Facade\Ignition\ErrorPage\IgnitionExceptionRenderer;
use Facade\Ignition\ErrorPage\IgnitionWhoopsHandler;
use Facade\Ignition\ErrorPage\Renderer;
use Facade\Ignition\Exceptions\InvalidConfig;
use Facade\Ignition\Http\Controllers\ExecuteSolutionController;
use Facade\Ignition\Http\Controllers\HealthCheckController;
use Facade\Ignition\Http\Controllers\ScriptController;
use Facade\Ignition\Http\Controllers\ShareReportController;
use Facade\Ignition\Http\Controllers\StyleController;
use Facade\Ignition\Http\Middleware\IgnitionConfigValueEnabled;
use Facade\Ignition\Http\Middleware\IgnitionEnabled;
use Facade\Ignition\JobRecorder\JobRecorder;
use Facade\Ignition\Logger\FlareHandler;
use Facade\Ignition\LogRecorder\LogRecorder;
use Facade\Ignition\Middleware\AddDumps;
use Facade\Ignition\Middleware\AddEnvironmentInformation;
use Facade\Ignition\Middleware\AddExceptionInformation;
use Facade\Ignition\Middleware\AddGitInformation;
use Facade\Ignition\Middleware\AddJobInformation;
use Facade\Ignition\Middleware\AddLogs;
use Facade\Ignition\Middleware\AddQueries;
use Facade\Ignition\Middleware\AddSolutions;
use Facade\Ignition\Middleware\SetNotifierName;
use Facade\Ignition\QueryRecorder\QueryRecorder;
use Facade\Ignition\SolutionProviders\BadMethodCallSolutionProvider;
use Facade\Ignition\SolutionProviders\DefaultDbNameSolutionProvider;
use Facade\Ignition\SolutionProviders\IncorrectValetDbCredentialsSolutionProvider;
use Facade\Ignition\SolutionProviders\InvalidRouteActionSolutionProvider;
use Facade\Ignition\SolutionProviders\LazyLoadingViolationSolutionProvider;
use Facade\Ignition\SolutionProviders\MergeConflictSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingAppKeySolutionProvider;
use Facade\Ignition\SolutionProviders\MissingColumnSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingImportSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingLivewireComponentSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingMixManifestSolutionProvider;
use Facade\Ignition\SolutionProviders\MissingPackageSolutionProvider;
use Facade\Ignition\SolutionProviders\RunningLaravelDuskInProductionProvider;
use Facade\Ignition\SolutionProviders\SolutionProviderRepository;
use Facade\Ignition\SolutionProviders\TableNotFoundSolutionProvider;
use Facade\Ignition\SolutionProviders\UndefinedLivewireMethodSolutionProvider;
use Facade\Ignition\SolutionProviders\UndefinedLivewirePropertySolutionProvider;
use Facade\Ignition\SolutionProviders\UndefinedPropertySolutionProvider;
use Facade\Ignition\SolutionProviders\UndefinedVariableSolutionProvider;
use Facade\Ignition\SolutionProviders\UnknownValidationSolutionProvider;
use Facade\Ignition\SolutionProviders\ViewNotFoundSolutionProvider;
use Facade\Ignition\Support\SentReports;
use Facade\Ignition\Views\Engines\CompilerEngine;
use Facade\Ignition\Views\Engines\PhpEngine;
use Facade\IgnitionContracts\SolutionProviderRepository as SolutionProviderRepositoryContract;
use Illuminate\Foundation\Application;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Log\LogManager;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\CompilerEngine as LaravelCompilerEngine;
use Illuminate\View\Engines\PhpEngine as LaravelPhpEngine;
use Laravel\Octane\Events\RequestReceived;
use Laravel\Octane\Events\TaskReceived;
use Laravel\Octane\Events\TickReceived;
use Livewire\CompilerEngineForIgnition;
use Monolog\Logger;
use Throwable;
class IgnitionServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/flare.php' => config_path('flare.php'),
], 'flare-config');
$this->publishes([
__DIR__.'/../config/ignition.php' => config_path('ignition.php'),
], 'ignition-config');
if (isset($_SERVER['argv']) && ['artisan', 'tinker'] === $_SERVER['argv']) {
Api::sendReportsInBatches(false);
}
$this->app->make(JobRecorder::class)->register();
}
$this
->registerViewEngines()
->registerHousekeepingRoutes()
->registerLogHandler()
->registerCommands();
if ($this->app->bound('queue')) {
$this->setupQueue($this->app->get('queue'));
}
if (isset($_SERVER['LARAVEL_OCTANE'])) {
$this->setupOctane();
}
if (config('flare.reporting.report_logs', true)) {
$this->app->make(LogRecorder::class)->register();
}
if (config('flare.reporting.report_queries', true)) {
$this->app->make(QueryRecorder::class)->register();
}
$this->app->make(DumpRecorder::class)->register();
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/flare.php', 'flare');
$this->mergeConfigFrom(__DIR__.'/../config/ignition.php', 'ignition');
$this
->registerSolutionProviderRepository()
->registerRenderer()
->registerExceptionRenderer()
->registerIgnitionConfig()
->registerFlare()
->registerDumpCollector()
->registerJobRecorder();
if (config('flare.reporting.report_logs', true)) {
$this->registerLogRecorder();
}
if (config('flare.reporting.report_queries', true)) {
$this->registerQueryRecorder();
}
if (config('flare.reporting.anonymize_ips')) {
$this->app->get(Flare::class)->anonymizeIp();
}
$this->app->get(Flare::class)->censorRequestBodyFields(config('flare.reporting.censor_request_body_fields', ['password']));
$this->registerBuiltInMiddleware();
}
protected function registerViewEngines()
{
if (! $this->hasCustomViewEnginesRegistered()) {
return $this;
}
$this->app->make('view.engine.resolver')->register('php', function () {
return new PhpEngine($this->app['files']);
});
$this->app->make('view.engine.resolver')->register('blade', function () {
if (class_exists(CompilerEngineForIgnition::class)) {
return new CompilerEngineForIgnition($this->app['blade.compiler']);
}
return new CompilerEngine($this->app['blade.compiler']);
});
return $this;
}
protected function registerHousekeepingRoutes()
{
if ($this->app->runningInConsole()) {
return $this;
}
Route::group([
'as' => 'ignition.',
'prefix' => config('ignition.housekeeping_endpoint_prefix', '_ignition'),
'middleware' => [IgnitionEnabled::class],
], function () {
Route::get('health-check', HealthCheckController::class)->name('healthCheck');
Route::post('execute-solution', ExecuteSolutionController::class)
->middleware(IgnitionConfigValueEnabled::class.':enableRunnableSolutions')
->name('executeSolution');
Route::post('share-report', ShareReportController::class)
->middleware(IgnitionConfigValueEnabled::class.':enableShareButton')
->name('shareReport');
Route::get('scripts/{script}', ScriptController::class)->name('scripts');
Route::get('styles/{style}', StyleController::class)->name('styles');
});
return $this;
}
protected function registerSolutionProviderRepository()
{
$this->app->singleton(SolutionProviderRepositoryContract::class, function () {
$defaultSolutions = $this->getDefaultSolutions();
return new SolutionProviderRepository($defaultSolutions);
});
return $this;
}
protected function registerRenderer()
{
$this->app->bind(Renderer::class, function () {
return new Renderer(__DIR__.'/../resources/views/');
});
return $this;
}
protected function registerExceptionRenderer()
{
if (interface_exists(\Whoops\Handler\HandlerInterface::class)) {
$this->app->bind(\Whoops\Handler\HandlerInterface::class, function (Application $app) {
return $app->make(IgnitionWhoopsHandler::class);
});
}
if (interface_exists(\Illuminate\Contracts\Foundation\ExceptionRenderer::class)) {
$this->app->bind(\Illuminate\Contracts\Foundation\ExceptionRenderer::class, function (Application $app) {
return $app->make(IgnitionExceptionRenderer::class);
});
}
return $this;
}
protected function registerIgnitionConfig()
{
$this->app->singleton(IgnitionConfig::class, function () {
$options = [];
try {
if ($configPath = $this->getConfigFileLocation()) {
$options = require $configPath;
}
} catch (Throwable $e) {
// possible open_basedir restriction
}
return new IgnitionConfig($options);
});
return $this;
}
protected function registerFlare()
{
$this->app->singleton('flare.http', function () {
return new Client(
config('flare.key'),
config('flare.secret'),
config('flare.base_url', 'https://reporting.flareapp.io/api')
);
});
$this->app->singleton(SentReports::class);
$this->app->alias('flare.http', Client::class);
$this->app->singleton(Flare::class, function () {
$client = new Flare($this->app->get('flare.http'), new LaravelContextDetector(), $this->app);
$client->applicationPath(base_path());
$client->stage(config('app.env'));
return $client;
});
return $this;
}
protected function registerLogHandler()
{
$this->app->singleton('flare.logger', function ($app) {
$handler = new FlareHandler(
$app->make(Flare::class),
$app->make(SentReports::class)
);
$logLevelString = config('logging.channels.flare.level', 'error');
$logLevel = $this->getLogLevel($logLevelString);
$handler->setMinimumReportLogLevel($logLevel);
$logger = new Logger('Flare');
$logger->pushHandler($handler);
return $logger;
});
if ($this->app['log'] instanceof LogManager) {
Log::extend('flare', function ($app) {
return $app['flare.logger'];
});
} else {
$this->bindLogListener();
}
return $this;
}
protected function getLogLevel(string $logLevelString): int
{
$logLevel = Logger::getLevels()[strtoupper($logLevelString)] ?? null;
if (! $logLevel) {
throw InvalidConfig::invalidLogLevel($logLevelString);
}
return $logLevel;
}
protected function registerLogRecorder(): self
{
$this->app->singleton(LogRecorder::class, function (Application $app): LogRecorder {
return new LogRecorder(
$app,
$app->get('config')->get('flare.reporting.maximum_number_of_collected_logs')
);
});
return $this;
}
protected function registerDumpCollector()
{
$dumpCollector = $this->app->make(DumpRecorder::class);
$this->app->singleton(DumpRecorder::class);
$this->app->instance(DumpRecorder::class, $dumpCollector);
return $this;
}
protected function registerJobRecorder()
{
if (! $this->app->runningInConsole()) {
return $this;
}
$this->app->singleton(JobRecorder::class);
return $this;
}
protected function registerCommands()
{
$this->app->bind('command.flare:test', TestCommand::class);
$this->app->bind('command.make:solution', SolutionMakeCommand::class);
$this->app->bind('command.make:solution-provider', SolutionProviderMakeCommand::class);
if ($this->app['config']->get('flare.key')) {
$this->commands(['command.flare:test']);
}
if ($this->app['config']->get('ignition.register_commands', false)) {
$this->commands(['command.make:solution']);
$this->commands(['command.make:solution-provider']);
}
return $this;
}
protected function registerQueryRecorder(): self
{
$this->app->singleton(QueryRecorder::class, function (Application $app): QueryRecorder {
return new QueryRecorder(
$app,
$app->get('config')->get('flare.reporting.report_query_bindings'),
$app->get('config')->get('flare.reporting.maximum_number_of_collected_queries')
);
});
return $this;
}
protected function registerBuiltInMiddleware()
{
$middlewares = [
SetNotifierName::class,
AddEnvironmentInformation::class,
AddExceptionInformation::class,
];
if (config('flare.reporting.report_logs', true)) {
$middlewares[] = AddLogs::class;
}
$middlewares[] = AddDumps::class;
if (config('flare.reporting.report_queries', true)) {
$middlewares[] = AddQueries::class;
}
$middlewares[] = AddSolutions::class;
if ($this->app->runningInConsole()) {
$middlewares[] = AddJobInformation::class;
}
$middleware = collect($middlewares)
->map(function (string $middlewareClass) {
return $this->app->make($middlewareClass);
});
if (config('flare.reporting.collect_git_information')) {
$middleware[] = (new AddGitInformation());
}
foreach ($middleware as $singleMiddleware) {
$this->app->get(Flare::class)->registerMiddleware($singleMiddleware);
}
return $this;
}
protected function getDefaultSolutions(): array
{
return [
IncorrectValetDbCredentialsSolutionProvider::class,
MissingAppKeySolutionProvider::class,
DefaultDbNameSolutionProvider::class,
BadMethodCallSolutionProvider::class,
TableNotFoundSolutionProvider::class,
MissingImportSolutionProvider::class,
MissingPackageSolutionProvider::class,
InvalidRouteActionSolutionProvider::class,
ViewNotFoundSolutionProvider::class,
UndefinedVariableSolutionProvider::class,
MergeConflictSolutionProvider::class,
RunningLaravelDuskInProductionProvider::class,
MissingColumnSolutionProvider::class,
UnknownValidationSolutionProvider::class,
UndefinedLivewireMethodSolutionProvider::class,
UndefinedLivewirePropertySolutionProvider::class,
UndefinedPropertySolutionProvider::class,
MissingMixManifestSolutionProvider::class,
MissingLivewireComponentSolutionProvider::class,
LazyLoadingViolationSolutionProvider::class,
];
}
protected function hasCustomViewEnginesRegistered()
{
$resolver = $this->app->make('view.engine.resolver');
if (! $resolver->resolve('php') instanceof LaravelPhpEngine) {
return false;
}
if (! $resolver->resolve('blade') instanceof LaravelCompilerEngine) {
return false;
}
return true;
}
protected function bindLogListener()
{
$this->app['log']->listen(function (MessageLogged $messageLogged) {
if (config('flare.key')) {
try {
$this->app['flare.logger']->log(
$messageLogged->level,
$messageLogged->message,
$messageLogged->context
);
} catch (Exception $exception) {
return;
}
}
});
}
protected function getConfigFileLocation(): ?string
{
$configFullPath = base_path().DIRECTORY_SEPARATOR.'.ignition';
if (file_exists($configFullPath)) {
return $configFullPath;
}
$configFullPath = Arr::get($_SERVER, 'HOME', '').DIRECTORY_SEPARATOR.'.ignition';
if (file_exists($configFullPath)) {
return $configFullPath;
}
return null;
}
protected function resetFlare()
{
$this->app->get(SentReports::class)->clear();
$this->app->get(Flare::class)->reset();
if (config('flare.reporting.report_logs', true)) {
$this->app->make(LogRecorder::class)->reset();
}
if (config('flare.reporting.report_queries', true)) {
$this->app->make(QueryRecorder::class)->reset();
}
if ($this->app->runningInConsole()) {
$this->app->make(JobRecorder::class)->reset();
}
$this->app->make(DumpRecorder::class)->reset();
}
protected function setupQueue(QueueManager $queue)
{
// Reset before executing a queue job to make sure the job's log/query/dump recorders are empty.
// When using a sync queue this also reports the queued reports from previous exceptions.
$queue->before(function () {
$this->resetFlare();
});
// Send queued reports (and reset) after executing a queue job.
$queue->after(function () {
$this->resetFlare();
});
// Note: the $queue->looping() event can't be used because it's not triggered on Vapor
}
/** @psalm-suppress UndefinedClass */
protected function setupOctane()
{
$this->app['events']->listen(RequestReceived::class, function () {
$this->resetFlare();
});
$this->app['events']->listen(TaskReceived::class, function () {
$this->resetFlare();
});
$this->app['events']->listen(TickReceived::class, function () {
$this->resetFlare();
});
}
}

View File

@@ -0,0 +1,167 @@
<?php
namespace Facade\Ignition\JobRecorder;
use DateTime;
use Error;
use Exception;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Jobs\RedisJob;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionProperty;
use RuntimeException;
class JobRecorder
{
/** @var \Illuminate\Contracts\Foundation\Application */
protected $app;
/** @var \Illuminate\Contracts\Queue\Job|null */
protected $job = null;
public function __construct(Application $app)
{
$this->app = $app;
}
public function register(): self
{
$this->app['events']->listen(JobExceptionOccurred::class, [$this, 'record']);
return $this;
}
public function record(JobExceptionOccurred $event): void
{
$this->job = $event->job;
}
public function getJob(): ?Job
{
return $this->job;
}
public function reset(): void
{
$this->job = null;
}
public function toArray(): array
{
if ($this->job === null) {
return [];
}
return array_merge(
$this->getJobProperties(),
[
'name' => $this->job->resolveName(),
'connection' => $this->job->getConnectionName(),
'queue' => $this->job->getQueue(),
]
);
}
protected function getJobProperties(): array
{
$payload = collect($this->resolveJobPayload());
$properties = [];
foreach ($payload as $key => $value) {
if (! in_array($key, ['job', 'data', 'displayName'])) {
$properties[$key] = $value;
}
}
if ($pushedAt = DateTime::createFromFormat('U.u', $payload->get('pushedAt', ''))) {
$properties['pushedAt'] = $pushedAt->format(DATE_ATOM);
}
try {
$properties['data'] = $this->resolveCommandProperties(
$this->resolveObjectFromCommand($payload['data']['command']),
config('ignition.max_chained_job_reporting_depth', 5)
);
} catch (Exception $exception) {
}
return $properties;
}
protected function resolveJobPayload(): array
{
if (! $this->job instanceof RedisJob) {
return $this->job->payload();
}
try {
return json_decode($this->job->getReservedJob(), true, 512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
return $this->job->payload();
}
}
protected function resolveCommandProperties(object $command, int $maxChainDepth): array
{
$propertiesToIgnore = ['job', 'closure'];
$properties = collect((new ReflectionClass($command))->getProperties())
->reject(function (ReflectionProperty $property) use ($propertiesToIgnore) {
return in_array($property->name, $propertiesToIgnore);
})
->mapWithKeys(function (ReflectionProperty $property) use ($command) {
try {
$property->setAccessible(true);
return [$property->name => $property->getValue($command)];
} catch (Error $error) {
return [$property->name => 'uninitialized'];
}
});
if ($properties->has('chained')) {
$properties['chained'] = $this->resolveJobChain($properties->get('chained'), $maxChainDepth);
}
return $properties->all();
}
protected function resolveJobChain(array $chainedCommands, int $maxDepth): array
{
if ($maxDepth === 0) {
return ['Ignition stopped recording jobs after this point since the max chain depth was reached'];
}
return array_map(
function (string $command) use ($maxDepth) {
$commandObject = $this->resolveObjectFromCommand($command);
return [
'name' => $commandObject instanceof CallQueuedClosure ? $commandObject->displayName() : get_class($commandObject),
'data' => $this->resolveCommandProperties($commandObject, $maxDepth - 1),
];
},
$chainedCommands
);
}
// Taken from Illuminate\Queue\CallQueuedHandler
protected function resolveObjectFromCommand(string $command): object
{
if (Str::startsWith($command, 'O:')) {
return unserialize($command);
}
if ($this->app->bound(Encrypter::class)) {
return unserialize($this->app[Encrypter::class]->decrypt($command));
}
throw new RuntimeException('Unable to extract job payload.');
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Facade\Ignition\LogRecorder;
use Illuminate\Log\Events\MessageLogged;
class LogMessage
{
/** @var string */
protected $message;
/** @var array */
protected $context;
/** @var string */
protected $level;
/** @var float */
protected $microtime;
public function __construct(?string $message, string $level, array $context = [], ?float $microtime = null)
{
$this->message = $message;
$this->level = $level;
$this->context = $context;
$this->microtime = $microtime ?? microtime(true);
}
public static function fromMessageLoggedEvent(MessageLogged $event): self
{
return new self(
$event->message,
$event->level,
$event->context
);
}
public function toArray()
{
return [
'message' => $this->message,
'level' => $this->level,
'context' => $this->context,
'microtime' => $this->microtime,
];
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace Facade\Ignition\LogRecorder;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Log\Events\MessageLogged;
use Throwable;
class LogRecorder
{
/** @var \Facade\Ignition\LogRecorder\LogMessage[] */
protected $logMessages = [];
/** @var \Illuminate\Contracts\Foundation\Application */
protected $app;
/** @var int|null */
private $maxLogs;
public function __construct(Application $app, ?int $maxLogs = null)
{
$this->app = $app;
$this->maxLogs = $maxLogs;
}
public function register(): self
{
$this->app['events']->listen(MessageLogged::class, [$this, 'record']);
return $this;
}
public function record(MessageLogged $event): void
{
if ($this->shouldIgnore($event)) {
return;
}
$this->logMessages[] = LogMessage::fromMessageLoggedEvent($event);
if (is_int($this->maxLogs)) {
$this->logMessages = array_slice($this->logMessages, -$this->maxLogs);
}
}
public function getLogMessages(): array
{
return $this->toArray();
}
public function toArray(): array
{
$logMessages = [];
foreach ($this->logMessages as $log) {
$logMessages[] = $log->toArray();
}
return $logMessages;
}
protected function shouldIgnore($event): bool
{
if (! isset($event->context['exception'])) {
return false;
}
if (! $event->context['exception'] instanceof Throwable) {
return false;
}
return true;
}
public function reset(): void
{
$this->logMessages = [];
}
public function getMaxLogs(): ?int
{
return $this->maxLogs;
}
public function setMaxLogs(?int $maxLogs): self
{
$this->maxLogs = $maxLogs;
return $this;
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Facade\Ignition\Logger;
use Facade\FlareClient\Flare;
use Facade\FlareClient\Report;
use Facade\Ignition\Ignition;
use Facade\Ignition\Support\SentReports;
use Facade\Ignition\Tabs\Tab;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Throwable;
class FlareHandler extends AbstractProcessingHandler
{
/** @var \Facade\FlareClient\Flare */
protected $flare;
/** @var \Facade\Ignition\Support\SentReports */
protected $sentReports;
protected $minimumReportLogLevel = Logger::ERROR;
public function __construct(Flare $flare, SentReports $sentReports, $level = Logger::DEBUG, $bubble = true)
{
$this->flare = $flare;
$this->sentReports = $sentReports;
parent::__construct($level, $bubble);
}
public function setMinimumReportLogLevel(int $level)
{
if (! in_array($level, Logger::getLevels())) {
throw new \InvalidArgumentException('The given minimum log level is not supported.');
}
$this->minimumReportLogLevel = $level;
}
protected function write(array $record): void
{
if (! $this->shouldReport($record)) {
return;
}
if ($this->hasException($record)) {
/** @var Throwable $throwable */
$throwable = $record['context']['exception'];
collect(Ignition::$tabs)
->each(function (Tab $tab) use ($throwable) {
$tab->beforeRenderingErrorPage($this->flare, $throwable);
});
$report = $this->flare->report($record['context']['exception']);
if ($report) {
$this->sentReports->add($report);
}
return;
}
if (config('flare.send_logs_as_events')) {
if ($this->hasValidLogLevel($record)) {
$this->flare->reportMessage(
$record['message'],
'Log ' . Logger::getLevelName($record['level']),
function (Report $flareReport) use ($record) {
foreach ($record['context'] as $key => $value) {
$flareReport->context($key, $value);
}
}
);
}
}
}
protected function shouldReport(array $report): bool
{
if (! config('flare.key')) {
return false;
}
return $this->hasException($report) || $this->hasValidLogLevel($report);
}
protected function hasException(array $report): bool
{
$context = $report['context'];
return isset($context['exception']) && $context['exception'] instanceof Throwable;
}
protected function hasValidLogLevel(array $report): bool
{
return $report['level'] >= $this->minimumReportLogLevel;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use Facade\Ignition\DumpRecorder\DumpRecorder;
class AddDumps
{
/** @var \Facade\Ignition\DumpRecorder\DumpRecorder */
protected $dumpRecorder;
public function __construct(DumpRecorder $dumpRecorder)
{
$this->dumpRecorder = $dumpRecorder;
}
public function handle(Report $report, $next)
{
$report->group('dumps', $this->dumpRecorder->getDumps());
return $next($report);
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
class AddEnvironmentInformation
{
public function handle(Report $report, $next)
{
$report->frameworkVersion(app()->version());
$report->group('env', [
'laravel_version' => app()->version(),
'laravel_locale' => app()->getLocale(),
'laravel_config_cached' => app()->configurationIsCached(),
'php_version' => phpversion(),
]);
return $next($report);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use Illuminate\Database\QueryException;
class AddExceptionInformation
{
public function handle(Report $report, $next)
{
$throwable = $report->getThrowable();
if (! $throwable instanceof QueryException) {
return $next($report);
}
$report->group('exception', [
'raw_sql' => $throwable->getSql(),
]);
return $next($report);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use ReflectionClass;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class AddGitInformation
{
public function handle(Report $report, $next)
{
try {
$report->group('git', [
'hash' => $this->hash(),
'message' => $this->message(),
'tag' => $this->tag(),
'remote' => $this->remote(),
]);
} catch (RuntimeException $exception) {
}
return $next($report);
}
public function hash(): ?string
{
return $this->command("git log --pretty=format:'%H' -n 1");
}
public function message(): ?string
{
return $this->command("git log --pretty=format:'%s' -n 1");
}
public function tag(): ?string
{
return $this->command('git describe --tags --abbrev=0');
}
public function remote(): ?string
{
return $this->command('git config --get remote.origin.url');
}
protected function command($command)
{
$process = (new ReflectionClass(Process::class))->hasMethod('fromShellCommandline')
? Process::fromShellCommandline($command, base_path())
: new Process($command, base_path());
$process->run();
return trim($process->getOutput());
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use Facade\Ignition\JobRecorder\JobRecorder;
class AddJobInformation
{
/** @var \Facade\Ignition\JobRecorder\JobRecorder */
protected $jobRecorder;
public function __construct(JobRecorder $jobRecorder)
{
$this->jobRecorder = $jobRecorder;
}
public function handle(Report $report, $next)
{
if ($this->jobRecorder->getJob()) {
$report->group('job', $this->jobRecorder->toArray());
}
return $next($report);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use Facade\Ignition\LogRecorder\LogRecorder;
class AddLogs
{
/** @var \Facade\Ignition\LogRecorder\LogRecorder */
protected $logRecorder;
public function __construct(LogRecorder $logRecorder)
{
$this->logRecorder = $logRecorder;
}
public function handle(Report $report, $next)
{
$report->group('logs', $this->logRecorder->getLogMessages());
return $next($report);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use Facade\Ignition\QueryRecorder\QueryRecorder;
class AddQueries
{
/** @var \Facade\Ignition\QueryRecorder\QueryRecorder */
protected $queryRecorder;
public function __construct(QueryRecorder $queryRecorder)
{
$this->queryRecorder = $queryRecorder;
}
public function handle(Report $report, $next)
{
$report->group('queries', $this->queryRecorder->getQueries());
return $next($report);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
use Facade\IgnitionContracts\SolutionProviderRepository;
class AddSolutions
{
/** @var \Facade\IgnitionContracts\SolutionProviderRepository */
protected $solutionProviderRepository;
public function __construct(SolutionProviderRepository $solutionProviderRepository)
{
$this->solutionProviderRepository = $solutionProviderRepository;
}
public function handle(Report $report, $next)
{
if ($throwable = $report->getThrowable()) {
$solutions = $this->solutionProviderRepository->getSolutionsForThrowable($throwable);
foreach ($solutions as $solution) {
$report->addSolution($solution);
}
}
return $next($report);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Facade\Ignition\Middleware;
use Facade\FlareClient\Report;
class SetNotifierName
{
public const NOTIFIER_NAME = 'Laravel Client';
public function handle(Report $report, $next)
{
$report->notifierName(static::NOTIFIER_NAME);
return $next($report);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Facade\Ignition\QueryRecorder;
use Illuminate\Database\Events\QueryExecuted;
class Query
{
/** @var string */
protected $sql;
/** @var float */
protected $time;
/** @var string */
protected $connectionName;
/** @var null|array */
protected $bindings;
/** @var float */
protected $microtime;
public static function fromQueryExecutedEvent(QueryExecuted $queryExecuted, bool $reportBindings = false)
{
return new static(
$queryExecuted->sql,
$queryExecuted->time,
$queryExecuted->connectionName ?? '',
$reportBindings ? $queryExecuted->bindings : null
);
}
protected function __construct(
string $sql,
float $time,
string $connectionName,
?array $bindings = null,
?float $microtime = null
) {
$this->sql = $sql;
$this->time = $time;
$this->connectionName = $connectionName;
$this->bindings = $bindings;
$this->microtime = $microtime ?? microtime(true);
}
public function toArray(): array
{
return [
'sql' => $this->sql,
'time' => $this->time,
'connection_name' => $this->connectionName,
'bindings' => $this->bindings,
'microtime' => $this->microtime,
];
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Facade\Ignition\QueryRecorder;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Events\QueryExecuted;
class QueryRecorder
{
/** @var \Facade\Ignition\QueryRecorder\Query|[] */
protected $queries = [];
/** @var \Illuminate\Contracts\Foundation\Application */
protected $app;
/** @var bool */
private $reportBindings;
/** @var int|null */
private $maxQueries;
public function __construct(
Application $app,
bool $reportBindings = true,
?int $maxQueries = null
) {
$this->app = $app;
$this->reportBindings = $reportBindings;
$this->maxQueries = $maxQueries;
}
public function register()
{
$this->app['events']->listen(QueryExecuted::class, [$this, 'record']);
return $this;
}
public function record(QueryExecuted $queryExecuted)
{
$this->queries[] = Query::fromQueryExecutedEvent($queryExecuted, $this->reportBindings);
if (is_int($this->maxQueries)) {
$this->queries = array_slice($this->queries, -$this->maxQueries);
}
}
public function getQueries(): array
{
$queries = [];
foreach ($this->queries as $query) {
$queries[] = $query->toArray();
}
return $queries;
}
public function reset()
{
$this->queries = [];
}
public function getReportBindings(): bool
{
return $this->reportBindings;
}
public function setReportBindings(bool $reportBindings): self
{
$this->reportBindings = $reportBindings;
return $this;
}
public function getMaxQueries(): ?int
{
return $this->maxQueries;
}
public function setMaxQueries(?int $maxQueries): self
{
$this->maxQueries = $maxQueries;
return $this;
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use BadMethodCallException;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionMethod;
use Throwable;
class BadMethodCallSolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/([a-zA-Z\\\\]+)::([a-zA-Z]+)/m';
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof BadMethodCallException) {
return false;
}
if (is_null($this->getClassAndMethodFromExceptionMessage($throwable->getMessage()))) {
return false;
}
return true;
}
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Bad Method Call')
->setSolutionDescription($this->getSolutionDescription($throwable)),
];
}
public function getSolutionDescription(Throwable $throwable): string
{
if (! $this->canSolve($throwable)) {
return '';
}
extract($this->getClassAndMethodFromExceptionMessage($throwable->getMessage()), EXTR_OVERWRITE);
$possibleMethod = $this->findPossibleMethod($class, $method);
return "Did you mean {$class}::{$possibleMethod->name}() ?";
}
protected function getClassAndMethodFromExceptionMessage(string $message): ?array
{
if (! preg_match(self::REGEX, $message, $matches)) {
return null;
}
return [
'class' => $matches[1],
'method' => $matches[2],
];
}
protected function findPossibleMethod(string $class, string $invalidMethodName)
{
return $this->getAvailableMethods($class)
->sortByDesc(function (ReflectionMethod $method) use ($invalidMethodName) {
similar_text($invalidMethodName, $method->name, $percentage);
return $percentage;
})->first();
}
protected function getAvailableMethods($class): Collection
{
$class = new ReflectionClass($class);
return Collection::make($class->getMethods());
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\SuggestUsingCorrectDbNameSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Database\QueryException;
use Throwable;
class DefaultDbNameSolutionProvider implements HasSolutionsForThrowable
{
public const MYSQL_UNKNOWN_DATABASE_CODE = 1049;
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof QueryException) {
return false;
}
if ($throwable->getCode() !== self::MYSQL_UNKNOWN_DATABASE_CODE) {
return false;
}
if (! in_array(env('DB_DATABASE'), ['homestead', 'laravel'])) {
return false;
}
return true;
}
public function getSolutions(Throwable $throwable): array
{
return [new SuggestUsingCorrectDbNameSolution()];
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\UseDefaultValetDbCredentialsSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Database\QueryException;
use Throwable;
class IncorrectValetDbCredentialsSolutionProvider implements HasSolutionsForThrowable
{
public const MYSQL_ACCESS_DENIED_CODE = 1045;
public function canSolve(Throwable $throwable): bool
{
if (PHP_OS !== 'Darwin') {
return false;
}
if (! $throwable instanceof QueryException) {
return false;
}
if (! $this->isAccessDeniedCode($throwable->getCode())) {
return false;
}
if (! $this->envFileExists()) {
return false;
}
if (! $this->isValetInstalled()) {
return false;
}
if ($this->usingCorrectDefaultCredentials()) {
return false;
}
return true;
}
public function getSolutions(Throwable $throwable): array
{
return [new UseDefaultValetDbCredentialsSolution()];
}
protected function envFileExists(): bool
{
return file_exists(base_path('.env'));
}
protected function isAccessDeniedCode($code): bool
{
return $code === static::MYSQL_ACCESS_DENIED_CODE;
}
protected function isValetInstalled(): bool
{
return file_exists('/usr/local/bin/valet');
}
protected function usingCorrectDefaultCredentials(): bool
{
return env('DB_USERNAME') === 'root' && env('DB_PASSWORD') === '';
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Support\ComposerClassMap;
use Facade\Ignition\Support\StringComparator;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Str;
use Throwable;
use UnexpectedValueException;
class InvalidRouteActionSolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/\[([a-zA-Z\\\\]+)\]/m';
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof UnexpectedValueException) {
return false;
}
if (! preg_match(self::REGEX, $throwable->getMessage(), $matches)) {
return false;
}
return Str::startsWith($throwable->getMessage(), 'Invalid route action: ');
}
public function getSolutions(Throwable $throwable): array
{
preg_match(self::REGEX, $throwable->getMessage(), $matches);
$invalidController = $matches[1] ?? null;
$suggestedController = $this->findRelatedController($invalidController);
if ($suggestedController === $invalidController) {
return [
BaseSolution::create("`{$invalidController}` is not invokable.")
->setSolutionDescription("The controller class `{$invalidController}` is not invokable. Did you forget to add the `__invoke` method or is the controller's method missing in your routes file?"),
];
}
if ($suggestedController) {
return [
BaseSolution::create("`{$invalidController}` was not found.")
->setSolutionDescription("Controller class `{$invalidController}` for one of your routes was not found. Did you mean `{$suggestedController}`?"),
];
}
return [
BaseSolution::create("`{$invalidController}` was not found.")
->setSolutionDescription("Controller class `{$invalidController}` for one of your routes was not found. Are you sure this controller exists and is imported correctly?"),
];
}
protected function findRelatedController(string $invalidController): ?string
{
$composerClassMap = app(ComposerClassMap::class);
$controllers = collect($composerClassMap->listClasses())
->filter(function (string $_file, string $fqcn) {
return Str::endsWith($fqcn, 'Controller');
})
->mapWithKeys(function (string $_file, string $fqcn) {
return [$fqcn => class_basename($fqcn)];
})
->toArray();
$basenameMatch = StringComparator::findClosestMatch($controllers, $invalidController, 4);
$controllers = array_flip($controllers);
$fqcnMatch = StringComparator::findClosestMatch($controllers, $invalidController, 4);
return $fqcnMatch ?? $basenameMatch;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Support\LaravelVersion;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Database\LazyLoadingViolationException;
use Throwable;
class LazyLoadingViolationSolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
if ($throwable instanceof LazyLoadingViolationException) {
return true;
}
if (! $previous = $throwable->getPrevious()) {
return false;
}
return $previous instanceof LazyLoadingViolationException;
}
public function getSolutions(Throwable $throwable): array
{
$majorVersion = LaravelVersion::major();
return [BaseSolution::create(
'Lazy loading was disabled to detect N+1 problems'
)
->setSolutionDescription(
'Either avoid lazy loading the relation or allow lazy loading.'
)
->setDocumentationLinks([
'Read the docs on preventing lazy loading' => "https://laravel.com/docs/{$majorVersion}.x/eloquent-relationships#preventing-lazy-loading",
'Watch a video on how to deal with the N+1 problem' => 'https://www.youtube.com/watch?v=ZE7KBeraVpc',
]),];
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Str;
use ParseError;
use Throwable;
class MergeConflictSolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
if (! ($throwable instanceof ParseError)) {
return false;
}
if (! $this->hasMergeConflictExceptionMessage($throwable)) {
return false;
}
$file = file_get_contents($throwable->getFile());
if (strpos($file, '=======') === false) {
return false;
}
if (strpos($file, '>>>>>>>') === false) {
return false;
}
return true;
}
public function getSolutions(Throwable $throwable): array
{
$file = file_get_contents($throwable->getFile());
preg_match('/\>\>\>\>\>\>\> (.*?)\n/', $file, $matches);
$source = $matches[1];
$target = $this->getCurrentBranch(basename($throwable->getFile()));
return [
BaseSolution::create("Merge conflict from branch '$source' into $target")
->setSolutionDescription('You have a Git merge conflict. To undo your merge do `git reset --hard HEAD`'),
];
}
protected function getCurrentBranch(string $directory): string
{
$branch = "'".trim(shell_exec("cd ${directory}; git branch | grep \\* | cut -d ' ' -f2"))."'";
if ($branch === "''") {
$branch = 'current branch';
}
return $branch;
}
protected function hasMergeConflictExceptionMessage(Throwable $throwable): bool
{
// For PHP 7.x and below
if (Str::startsWith($throwable->getMessage(), 'syntax error, unexpected \'<<\'')) {
return true;
}
// For PHP 8+
if (Str::startsWith($throwable->getMessage(), 'syntax error, unexpected token "<<"')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\GenerateAppKeySolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use RuntimeException;
use Throwable;
class MissingAppKeySolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof RuntimeException) {
return false;
}
return $throwable->getMessage() === 'No application encryption key has been specified.';
}
public function getSolutions(Throwable $throwable): array
{
return [new GenerateAppKeySolution()];
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\RunMigrationsSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Database\QueryException;
use Throwable;
class MissingColumnSolutionProvider implements HasSolutionsForThrowable
{
/**
* See https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html#error_er_bad_field_error.
*/
public const MYSQL_BAD_FIELD_CODE = '42S22';
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof QueryException) {
return false;
}
return $this->isBadTableErrorCode($throwable->getCode());
}
protected function isBadTableErrorCode($code): bool
{
return $code === static::MYSQL_BAD_FIELD_CODE;
}
public function getSolutions(Throwable $throwable): array
{
return [new RunMigrationsSolution('A column was not found')];
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\SuggestImportSolution;
use Facade\Ignition\Support\ComposerClassMap;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Throwable;
class MissingImportSolutionProvider implements HasSolutionsForThrowable
{
/** @var string */
protected $foundClass;
/** @var \Facade\Ignition\Support\ComposerClassMap */
protected $composerClassMap;
public function canSolve(Throwable $throwable): bool
{
$pattern = '/Class \'([^\s]+)\' not found/m';
if (! preg_match($pattern, $throwable->getMessage(), $matches)) {
return false;
}
$class = $matches[1];
$this->composerClassMap = new ComposerClassMap();
$this->search($class);
return ! is_null($this->foundClass);
}
public function getSolutions(Throwable $throwable): array
{
return [new SuggestImportSolution($this->foundClass)];
}
protected function search(string $missingClass)
{
$this->foundClass = $this->composerClassMap->searchClassMap($missingClass);
if (is_null($this->foundClass)) {
$this->foundClass = $this->composerClassMap->searchPsrMaps($missingClass);
}
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\LivewireDiscoverSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Livewire\Exceptions\ComponentNotFoundException;
use Livewire\LivewireComponentsFinder;
use Throwable;
class MissingLivewireComponentSolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
if (! $this->livewireIsInstalled()) {
return false;
}
if (! $throwable instanceof ComponentNotFoundException) {
return false;
}
return true;
}
public function getSolutions(Throwable $throwable): array
{
return [new LivewireDiscoverSolution('A Livewire component was not found')];
}
public function livewireIsInstalled(): bool
{
if (! class_exists(ComponentNotFoundException::class)) {
return false;
}
if (! class_exists(LivewireComponentsFinder::class)) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Str;
use Throwable;
class MissingMixManifestSolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
return Str::startsWith($throwable->getMessage(), 'The Mix manifest does not exist');
}
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Missing Mix Manifest File')
->setSolutionDescription('Did you forget to run `npm ci && npm run dev`?'),
];
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\MissingPackageSolution;
use Facade\Ignition\Support\Packagist\Package;
use Facade\Ignition\Support\Packagist\Packagist;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Str;
use Throwable;
class MissingPackageSolutionProvider implements HasSolutionsForThrowable
{
/** @var \Facade\Ignition\Support\Packagist\Package|null */
protected $package;
public function canSolve(Throwable $throwable): bool
{
$pattern = '/Class \'([^\s]+)\' not found/m';
if (! preg_match($pattern, $throwable->getMessage(), $matches)) {
return false;
}
$class = $matches[1];
if (Str::startsWith($class, app()->getNamespace())) {
return false;
}
$this->package = $this->findPackageFromClassName($class);
return ! is_null($this->package);
}
public function getSolutions(Throwable $throwable): array
{
return [new MissingPackageSolution($this->package)];
}
protected function findPackageFromClassName(string $missingClassName): ?Package
{
if (! $package = $this->findComposerPackageForClassName($missingClassName)) {
return null;
}
return $package->hasNamespaceThatContainsClassName($missingClassName)
? $package
: null;
}
protected function findComposerPackageForClassName(string $className): ?Package
{
$packages = Packagist::findPackagesForClassName($className);
return $packages[0] ?? null;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Support\StringComparator;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use InvalidArgumentException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Throwable;
class RouteNotDefinedSolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/Route \[(.*)\] not defined/m';
public function canSolve(Throwable $throwable): bool
{
if (version_compare(Application::VERSION, '6.0.0', '>=')) {
if (! $throwable instanceof RouteNotFoundException) {
return false;
}
}
if (version_compare(Application::VERSION, '6.0.0', '<')) {
if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) {
return false;
}
}
return (bool)preg_match(self::REGEX, $throwable->getMessage(), $matches);
}
public function getSolutions(Throwable $throwable): array
{
preg_match(self::REGEX, $throwable->getMessage(), $matches);
$missingRoute = $matches[1] ?? null;
$suggestedRoute = $this->findRelatedRoute($missingRoute);
if ($suggestedRoute) {
return [
BaseSolution::create("{$missingRoute} was not defined.")
->setSolutionDescription("Did you mean `{$suggestedRoute}`?"),
];
}
return [
BaseSolution::create("{$missingRoute} was not defined.")
->setSolutionDescription('Are you sure that the route is defined'),
];
}
protected function findRelatedRoute(string $missingRoute): ?string
{
Route::getRoutes()->refreshNameLookups();
return StringComparator::findClosestMatch(array_keys(Route::getRoutes()->getRoutesByName()), $missingRoute);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Exception;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Throwable;
class RunningLaravelDuskInProductionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof Exception) {
return false;
}
return $throwable->getMessage() === 'It is unsafe to run Dusk in production.';
}
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Laravel Dusk should not be run in production.')
->setSolutionDescription('Install the dependencies with the `--no-dev` flag.'),
BaseSolution::create('Laravel Dusk can be run in other environments.')
->setSolutionDescription('Consider setting the `APP_ENV` to something other than `production` like `local` for example.'),
];
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
use Facade\IgnitionContracts\SolutionProviderRepository as SolutionProviderRepositoryContract;
use Illuminate\Support\Collection;
use Throwable;
class SolutionProviderRepository implements SolutionProviderRepositoryContract
{
/** @var \Illuminate\Support\Collection */
protected $solutionProviders;
public function __construct(array $solutionProviders = [])
{
$this->solutionProviders = Collection::make($solutionProviders);
}
public function registerSolutionProvider(string $solutionProviderClass): SolutionProviderRepositoryContract
{
$this->solutionProviders->push($solutionProviderClass);
return $this;
}
public function registerSolutionProviders(array $solutionProviderClasses): SolutionProviderRepositoryContract
{
$this->solutionProviders = $this->solutionProviders->merge($solutionProviderClasses);
return $this;
}
public function getSolutionsForThrowable(Throwable $throwable): array
{
$solutions = [];
if ($throwable instanceof Solution) {
$solutions[] = $throwable;
}
if ($throwable instanceof ProvidesSolution) {
$solutions[] = $throwable->getSolution();
}
$providedSolutions = $this->solutionProviders
->filter(function (string $solutionClass) {
if (! in_array(HasSolutionsForThrowable::class, class_implements($solutionClass))) {
return false;
}
if (in_array($solutionClass, config('ignition.ignored_solution_providers', []))) {
return false;
}
return true;
})
->map(function (string $solutionClass) {
return app($solutionClass);
})
->filter(function (HasSolutionsForThrowable $solutionProvider) use ($throwable) {
try {
return $solutionProvider->canSolve($throwable);
} catch (Throwable $e) {
return false;
}
})
->map(function (HasSolutionsForThrowable $solutionProvider) use ($throwable) {
try {
return $solutionProvider->getSolutions($throwable);
} catch (Throwable $e) {
return [];
}
})
->flatten()
->toArray();
return array_merge($solutions, $providedSolutions);
}
public function getSolutionForClass(string $solutionClass): ?Solution
{
if (! class_exists($solutionClass)) {
return null;
}
if (! in_array(Solution::class, class_implements($solutionClass))) {
return null;
}
return app($solutionClass);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\RunMigrationsSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Database\QueryException;
use Throwable;
class TableNotFoundSolutionProvider implements HasSolutionsForThrowable
{
/**
* See https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html#error_er_bad_table_error.
*/
public const MYSQL_BAD_TABLE_CODE = '42S02';
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof QueryException) {
return false;
}
return $this->isBadTableErrorCode($throwable->getCode());
}
protected function isBadTableErrorCode($code): bool
{
return $code === static::MYSQL_BAD_TABLE_CODE;
}
public function getSolutions(Throwable $throwable): array
{
return [new RunMigrationsSolution('A table was not found')];
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\SuggestLivewireMethodNameSolution;
use Facade\Ignition\Support\LivewireComponentParser;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Livewire\Exceptions\MethodNotFoundException;
use Throwable;
class UndefinedLivewireMethodSolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
return $throwable instanceof MethodNotFoundException;
}
public function getSolutions(Throwable $throwable): array
{
['methodName' => $methodName, 'component' => $component] = $this->getMethodAndComponent($throwable);
if ($methodName === null || $component === null) {
return [];
}
$parsed = LivewireComponentParser::create($component);
return $parsed->getMethodNamesLike($methodName)
->map(function (string $suggested) use ($parsed, $methodName) {
return new SuggestLivewireMethodNameSolution(
$methodName,
$parsed->getComponentClass(),
$suggested
);
})
->toArray();
}
protected function getMethodAndComponent(Throwable $throwable): array
{
preg_match_all('/\[([\d\w\-_]*)\]/m', $throwable->getMessage(), $matches, PREG_SET_ORDER);
return [
'methodName' => $matches[0][1] ?? null,
'component' => $matches[1][1] ?? null,
];
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\SuggestLivewirePropertyNameSolution;
use Facade\Ignition\Support\LivewireComponentParser;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Livewire\Exceptions\PropertyNotFoundException;
use Livewire\Exceptions\PublicPropertyNotFoundException;
use Throwable;
class UndefinedLivewirePropertySolutionProvider implements HasSolutionsForThrowable
{
public function canSolve(Throwable $throwable): bool
{
return $throwable instanceof PropertyNotFoundException || $throwable instanceof PublicPropertyNotFoundException;
}
public function getSolutions(Throwable $throwable): array
{
['variable' => $variable, 'component' => $component] = $this->getMethodAndComponent($throwable);
if ($variable === null || $component === null) {
return [];
}
$parsed = LivewireComponentParser::create($component);
return $parsed->getPropertyNamesLike($variable)
->map(function (string $suggested) use ($parsed, $variable) {
return new SuggestLivewirePropertyNameSolution(
$variable,
$parsed->getComponentClass(),
'$'.$suggested
);
})
->toArray();
}
protected function getMethodAndComponent(Throwable $throwable): array
{
preg_match_all('/\[([\d\w\-_\$]*)\]/m', $throwable->getMessage(), $matches, PREG_SET_ORDER, 0);
return [
'variable' => $matches[0][1] ?? null,
'component' => $matches[1][1] ?? null,
];
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use ErrorException;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionProperty;
use Throwable;
class UndefinedPropertySolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/([a-zA-Z\\\\]+)::\$([a-zA-Z]+)/m';
protected const MINIMUM_SIMILARITY = 80;
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof ErrorException) {
return false;
}
if (is_null($this->getClassAndPropertyFromExceptionMessage($throwable->getMessage()))) {
return false;
}
if (! $this->similarPropertyExists($throwable)) {
return false;
}
return true;
}
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Unknown Property')
->setSolutionDescription($this->getSolutionDescription($throwable)),
];
}
public function getSolutionDescription(Throwable $throwable): string
{
if (! $this->canSolve($throwable) || ! $this->similarPropertyExists($throwable)) {
return '';
}
extract($this->getClassAndPropertyFromExceptionMessage($throwable->getMessage()), EXTR_OVERWRITE);
$possibleProperty = $this->findPossibleProperty($class, $property);
return "Did you mean {$class}::\${$possibleProperty->name} ?";
}
protected function similarPropertyExists(Throwable $throwable)
{
extract($this->getClassAndPropertyFromExceptionMessage($throwable->getMessage()), EXTR_OVERWRITE);
$possibleProperty = $this->findPossibleProperty($class, $property);
return $possibleProperty !== null;
}
protected function getClassAndPropertyFromExceptionMessage(string $message): ?array
{
if (! preg_match(self::REGEX, $message, $matches)) {
return null;
}
return [
'class' => $matches[1],
'property' => $matches[2],
];
}
protected function findPossibleProperty(string $class, string $invalidPropertyName)
{
return $this->getAvailableProperties($class)
->sortByDesc(function (ReflectionProperty $property) use ($invalidPropertyName) {
similar_text($invalidPropertyName, $property->name, $percentage);
return $percentage;
})
->filter(function (ReflectionProperty $property) use ($invalidPropertyName) {
similar_text($invalidPropertyName, $property->name, $percentage);
return $percentage >= self::MINIMUM_SIMILARITY;
})->first();
}
protected function getAvailableProperties($class): Collection
{
$class = new ReflectionClass($class);
return Collection::make($class->getProperties());
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Solutions\MakeViewVariableOptionalSolution;
use Facade\Ignition\Solutions\SuggestCorrectVariableNameSolution;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Throwable;
class UndefinedVariableSolutionProvider implements HasSolutionsForThrowable
{
private $variableName;
private $viewFile;
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof ViewException) {
return false;
}
return $this->getNameAndView($throwable) !== null;
}
public function getSolutions(Throwable $throwable): array
{
$solutions = [];
extract($this->getNameAndView($throwable));
if (! isset($variableName)) {
return [];
}
$solutions = $this->findCorrectVariableSolutions($throwable, $variableName, $viewFile);
$solutions[] = $this->findOptionalVariableSolution($variableName, $viewFile);
return $solutions;
}
protected function findCorrectVariableSolutions(
ViewException $throwable,
string $variableName,
string $viewFile
): array {
return collect($throwable->getViewData())
->map(function ($value, $key) use ($variableName) {
similar_text($variableName, $key, $percentage);
return ['match' => $percentage, 'value' => $value];
})
->sortByDesc('match')->filter(function ($var) {
return $var['match'] > 40;
})
->keys()
->map(function ($suggestion) use ($variableName, $viewFile) {
return new SuggestCorrectVariableNameSolution($variableName, $viewFile, $suggestion);
})
->map(function ($solution) {
return $solution->isRunnable()
? $solution
: BaseSolution::create($solution->getSolutionTitle())
->setSolutionDescription($solution->getSolutionDescription());
})
->toArray();
}
protected function findOptionalVariableSolution(string $variableName, string $viewFile)
{
$optionalSolution = new MakeViewVariableOptionalSolution($variableName, $viewFile);
return $optionalSolution->isRunnable()
? $optionalSolution
: BaseSolution::create($optionalSolution->getSolutionTitle())
->setSolutionDescription($optionalSolution->getSolutionDescription());
}
protected function getNameAndView(Throwable $throwable): ?array
{
$pattern = '/Undefined variable:? (.*?) \(View: (.*?)\)/';
preg_match($pattern, $throwable->getMessage(), $matches);
if (count($matches) === 3) {
[, $variableName, $viewFile] = $matches;
$variableName = ltrim($variableName, '$');
return compact('variableName', 'viewFile');
}
return null;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use BadMethodCallException;
use Facade\Ignition\Support\StringComparator;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Validation\Validator;
use ReflectionClass;
use ReflectionMethod;
use Throwable;
class UnknownValidationSolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/Illuminate\\\\Validation\\\\Validator::(?P<method>validate(?!(Attribute|UsingCustomRule))[A-Z][a-zA-Z]+)/m';
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof BadMethodCallException) {
return false;
}
return ! is_null($this->getMethodFromExceptionMessage($throwable->getMessage()));
}
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Unknown Validation Rule')
->setSolutionDescription($this->getSolutionDescription($throwable)),
];
}
protected function getSolutionDescription(Throwable $throwable): string
{
$method = $this->getMethodFromExceptionMessage($throwable->getMessage());
$possibleMethod = StringComparator::findSimilarText(
$this->getAvailableMethods()->toArray(),
$method
);
if (empty($possibleMethod)) {
return '';
}
$rule = Str::snake(str_replace('validate', '', $possibleMethod));
return "Did you mean `{$rule}` ?";
}
protected function getMethodFromExceptionMessage(string $message): ?string
{
if (! preg_match(self::REGEX, $message, $matches)) {
return null;
}
return $matches['method'];
}
protected function getAvailableMethods(): Collection
{
$class = new ReflectionClass(Validator::class);
$extensions = Collection::make((app('validator')->make([], []))->extensions)
->keys()
->map(function (string $extension) {
return 'validate'.Str::studly($extension);
});
return Collection::make($class->getMethods())
->filter(function (ReflectionMethod $method) {
return preg_match('/(validate(?!(Attribute|UsingCustomRule))[A-Z][a-zA-Z]+)/', $method->name);
})
->map(function (ReflectionMethod $method) {
return $method->name;
})
->merge($extensions);
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Support\StringComparator;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\View;
use InvalidArgumentException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Throwable;
class ViewNotFoundSolutionProvider implements HasSolutionsForThrowable
{
protected const REGEX = '/View \[(.*)\] not found/m';
public function canSolve(Throwable $throwable): bool
{
if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) {
return false;
}
return (bool)preg_match(self::REGEX, $throwable->getMessage(), $matches);
}
public function getSolutions(Throwable $throwable): array
{
preg_match(self::REGEX, $throwable->getMessage(), $matches);
$missingView = $matches[1] ?? null;
$suggestedView = $this->findRelatedView($missingView);
if ($suggestedView) {
return [
BaseSolution::create("{$missingView} was not found.")
->setSolutionDescription("Did you mean `{$suggestedView}`?"),
];
}
return [
BaseSolution::create("{$missingView} was not found.")
->setSolutionDescription('Are you sure the view exists and is a `.blade.php` file?'),
];
}
protected function findRelatedView(string $missingView): ?string
{
$views = $this->getAllViews();
return StringComparator::findClosestMatch($views, $missingView);
}
protected function getAllViews(): array
{
/** @var \Illuminate\View\FileViewFinder $fileViewFinder */
$fileViewFinder = View::getFinder();
$extensions = $fileViewFinder->getExtensions();
$viewsForHints = collect($fileViewFinder->getHints())
->flatMap(function ($paths, string $namespace) use ($extensions) {
$paths = Arr::wrap($paths);
return collect($paths)
->flatMap(function (string $path) use ($extensions) {
return $this->getViewsInPath($path, $extensions);
})
->map(function (string $view) use ($namespace) {
return "{$namespace}::{$view}";
})
->toArray();
});
$viewsForViewPaths = collect($fileViewFinder->getPaths())
->flatMap(function (string $path) use ($extensions) {
return $this->getViewsInPath($path, $extensions);
});
return $viewsForHints->merge($viewsForViewPaths)->toArray();
}
protected function getViewsInPath(string $path, array $extensions): array
{
$filePatterns = array_map(function (string $extension) {
return "*.{$extension}";
}, $extensions);
$extensionsWithDots = array_map(function (string $extension) {
return ".{$extension}";
}, $extensions);
$files = (new Finder())
->in($path)
->files();
foreach ($filePatterns as $filePattern) {
$files->name($filePattern);
}
$views = [];
foreach ($files as $file) {
if ($file instanceof SplFileInfo) {
$view = $file->getRelativePathname();
$view = str_replace($extensionsWithDots, '', $view);
$view = str_replace('/', '.', $view);
$views[] = $view;
}
}
return $views;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Facades\Artisan;
class GenerateAppKeySolution implements RunnableSolution
{
public function getSolutionTitle(): string
{
return 'Your app key is missing';
}
public function getDocumentationLinks(): array
{
return [
'Laravel installation' => 'https://laravel.com/docs/master/installation#configuration',
];
}
public function getSolutionActionDescription(): string
{
return 'Generate your application encryption key using `php artisan key:generate`.';
}
public function getRunButtonText(): string
{
return 'Generate app key';
}
public function getSolutionDescription(): string
{
return '';
}
public function getRunParameters(): array
{
return [];
}
public function run(array $parameters = [])
{
Artisan::call('key:generate');
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Livewire\LivewireComponentsFinder;
class LivewireDiscoverSolution implements RunnableSolution
{
private $customTitle;
public function __construct($customTitle = '')
{
$this->customTitle = $customTitle;
}
public function getSolutionTitle(): string
{
return $this->customTitle;
}
public function getSolutionDescription(): string
{
return 'You might have forgotten to discover your Livewire components. You can discover your Livewire components using `php artisan livewire:discover`.';
}
public function getDocumentationLinks(): array
{
return [
'Livewire: Artisan Commands' => 'https://laravel-livewire.com/docs/2.x/artisan-commands',
];
}
public function getRunParameters(): array
{
return [];
}
public function getSolutionActionDescription(): string
{
return 'Pressing the button below will try to discover your Livewire components.';
}
public function getRunButtonText(): string
{
return 'Run livewire:discover';
}
public function run(array $parameters = [])
{
app(LivewireComponentsFinder::class)->build();
}
}

View File

@@ -0,0 +1,122 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
class MakeViewVariableOptionalSolution implements RunnableSolution
{
/** @var string */
private $variableName;
/** @var string */
private $viewFile;
public function __construct($variableName = null, $viewFile = null)
{
$this->variableName = $variableName;
$this->viewFile = $viewFile;
}
public function getSolutionTitle(): string
{
return "$$this->variableName is undefined";
}
public function getDocumentationLinks(): array
{
return [];
}
public function getSolutionActionDescription(): string
{
$output = [
'Make the variable optional in the blade template.',
"Replace `{{ $$this->variableName }}` with `{{ $$this->variableName ?? '' }}`",
];
return implode(PHP_EOL, $output);
}
public function getRunButtonText(): string
{
return 'Make variable optional';
}
public function getSolutionDescription(): string
{
return '';
}
public function getRunParameters(): array
{
return [
'variableName' => $this->variableName,
'viewFile' => $this->viewFile,
];
}
public function isRunnable(array $parameters = [])
{
return $this->makeOptional($this->getRunParameters()) !== false;
}
public function run(array $parameters = [])
{
$output = $this->makeOptional($parameters);
if ($output !== false) {
file_put_contents($parameters['viewFile'], $output);
}
}
protected function isSafePath(string $path): bool
{
if (! Str::startsWith($path, ['/', './'])) {
return false;
}
if (! Str::endsWith($path, '.blade.php')) {
return false;
}
return true;
}
public function makeOptional(array $parameters = [])
{
if (! $this->isSafePath($parameters['viewFile'])) {
return false;
}
$originalContents = file_get_contents($parameters['viewFile']);
$newContents = str_replace('$'.$parameters['variableName'], '$'.$parameters['variableName']." ?? ''", $originalContents);
$originalTokens = token_get_all(Blade::compileString($originalContents));
$newTokens = token_get_all(Blade::compileString($newContents));
$expectedTokens = $this->generateExpectedTokens($originalTokens, $parameters['variableName']);
if ($expectedTokens !== $newTokens) {
return false;
}
return $newContents;
}
protected function generateExpectedTokens(array $originalTokens, string $variableName): array
{
$expectedTokens = [];
foreach ($originalTokens as $token) {
$expectedTokens[] = $token;
if ($token[0] === T_VARIABLE && $token[1] === '$'.$variableName) {
$expectedTokens[] = [T_WHITESPACE, ' ', $token[2]];
$expectedTokens[] = [T_COALESCE, '??', $token[2]];
$expectedTokens[] = [T_WHITESPACE, ' ', $token[2]];
$expectedTokens[] = [T_CONSTANT_ENCAPSED_STRING, "''", $token[2]];
}
}
return $expectedTokens;
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\Ignition\Support\Packagist\Package;
use Facade\IgnitionContracts\Solution;
class MissingPackageSolution implements Solution
{
/** @var Package */
protected $possiblePackage;
public function __construct(Package $possiblePackage)
{
$this->possiblePackage = $possiblePackage;
}
public function getSolutionTitle(): string
{
return 'A composer dependency is missing';
}
public function getSolutionDescription(): string
{
$output = [
'You might be missing a composer dependency.',
'A possible package that was found is `'.$this->possiblePackage->name.'`.',
'',
'See if this is the package that you need and install it via `composer require '.$this->possiblePackage->name.'`.',
];
return implode(PHP_EOL, $output);
}
public function getDocumentationLinks(): array
{
return [
'Git repository' => $this->possiblePackage->repository,
'Package on Packagist' => $this->possiblePackage->url,
];
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Facades\Artisan;
class RunMigrationsSolution implements RunnableSolution
{
private $customTitle;
public function __construct($customTitle = '')
{
$this->customTitle = $customTitle;
}
public function getSolutionTitle(): string
{
return $this->customTitle;
}
public function getSolutionDescription(): string
{
return 'You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`.';
}
public function getDocumentationLinks(): array
{
return [
'Database: Running Migrations docs' => 'https://laravel.com/docs/master/migrations#running-migrations',
];
}
public function getRunParameters(): array
{
return [];
}
public function getSolutionActionDescription(): string
{
return 'Pressing the button below will try to run your migrations.';
}
public function getRunButtonText(): string
{
return 'Run migrations';
}
public function run(array $parameters = [])
{
Artisan::call('migrate');
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Facade\IgnitionContracts\Solution;
use Illuminate\Contracts\Support\Arrayable;
use Throwable;
class SolutionTransformer implements Arrayable
{
/** @var \Facade\IgnitionContracts\Solution */
protected $solution;
public function __construct(Solution $solution)
{
$this->solution = $solution;
}
public function toArray(): array
{
$isRunnable = ($this->solution instanceof RunnableSolution);
return [
'class' => get_class($this->solution),
'title' => $this->solution->getSolutionTitle(),
'description' => $this->solution->getSolutionDescription(),
'links' => $this->solution->getDocumentationLinks(),
'is_runnable' => $isRunnable,
'run_button_text' => $isRunnable ? $this->solution->getRunButtonText() : '',
'run_parameters' => $isRunnable ? $this->solution->getRunParameters() : [],
'action_description' => $isRunnable ? $this->solution->getSolutionActionDescription() : '',
'execute_endpoint' => $this->executeEndpoint(),
];
}
protected function executeEndpoint(): string
{
try {
return action('\Facade\Ignition\Http\Controllers\ExecuteSolutionController');
} catch (Throwable $exception) {
return '';
}
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\Solution;
class SuggestCorrectVariableNameSolution implements Solution
{
/** @var string */
private $variableName;
/** @var string */
private $viewFile;
/** @var string|null */
private $suggested;
public function __construct($variableName = null, $viewFile = null, $suggested = null)
{
$this->variableName = $variableName;
$this->viewFile = $viewFile;
$this->suggested = $suggested;
}
public function getSolutionTitle(): string
{
return 'Possible typo $'.$this->variableName;
}
public function getDocumentationLinks(): array
{
return [];
}
public function getSolutionDescription(): string
{
return "Did you mean `$$this->suggested`?";
}
public function isRunnable(): bool
{
return false;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\Solution;
class SuggestImportSolution implements Solution
{
/** @var string */
protected $class;
public function __construct(string $class)
{
$this->class = $class;
}
public function getSolutionTitle(): string
{
return 'A class import is missing';
}
public function getSolutionDescription(): string
{
return 'You have a missing class import. Try importing this class: `'.$this->class.'`.';
}
public function getDocumentationLinks(): array
{
return [];
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\Solution;
class SuggestLivewireMethodNameSolution implements Solution
{
/** @var string */
private $methodName;
/** @var string */
private $componentClass;
/** @var string|null */
private $suggested;
public function __construct($methodName = null, $componentClass = null, $suggested = null)
{
$this->methodName = $methodName;
$this->componentClass = $componentClass;
$this->suggested = $suggested;
}
public function getSolutionTitle(): string
{
return "Possible typo `{$this->componentClass}::{$this->methodName}()`";
}
public function getDocumentationLinks(): array
{
return [];
}
public function getSolutionDescription(): string
{
return "Did you mean `{$this->suggested}()`?";
}
public function isRunnable(): bool
{
return false;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\Solution;
class SuggestLivewirePropertyNameSolution implements Solution
{
/** @var string */
private $variableName;
/** @var string */
private $componentClass;
/** @var string|null */
private $suggested;
public function __construct($variableName = null, $componentClass = null, $suggested = null)
{
$this->variableName = $variableName;
$this->componentClass = $componentClass;
$this->suggested = $suggested;
}
public function getSolutionTitle(): string
{
return "Possible typo {$this->componentClass}::{$this->variableName}";
}
public function getDocumentationLinks(): array
{
return [];
}
public function getSolutionDescription(): string
{
return "Did you mean `$this->suggested`?";
}
public function isRunnable(): bool
{
return false;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\Solution;
class SuggestUsingCorrectDbNameSolution implements Solution
{
public function getSolutionTitle(): string
{
return 'Database name seems incorrect';
}
public function getSolutionDescription(): string
{
$defaultDatabaseName = env('DB_DATABASE');
return "You're using the default database name `$defaultDatabaseName`. This database does not exist.\n\nEdit the `.env` file and use the correct database name in the `DB_DATABASE` key.";
}
public function getDocumentationLinks(): array
{
return [
'Database: Getting Started docs' => 'https://laravel.com/docs/master/database#configuration',
];
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Str;
class UseDefaultValetDbCredentialsSolution implements RunnableSolution
{
public function getSolutionActionDescription(): string
{
return 'Pressing the button below will change `DB_USER` and `DB_PASSWORD` in your `.env` file.';
}
public function getRunButtonText(): string
{
return 'Use default Valet credentials';
}
public function getSolutionTitle(): string
{
return 'Could not connect to database';
}
public function run(array $parameters = [])
{
if (! file_exists(base_path('.env'))) {
return;
}
$this->ensureLineExists('DB_USERNAME', 'root');
$this->ensureLineExists('DB_PASSWORD', '');
}
protected function ensureLineExists(string $key, string $value)
{
$envPath = base_path('.env');
$envLines = array_map(function (string $envLine) use ($value, $key) {
return Str::startsWith($envLine, $key)
? "{$key}={$value}".PHP_EOL
: $envLine;
}, file($envPath));
file_put_contents($envPath, implode('', $envLines));
}
public function getDocumentationLinks(): array
{
return [];
}
public function getRunParameters(): array
{
return [
'Valet documentation' => 'https://laravel.com/docs/master/valet',
];
}
public function getSolutionDescription(): string
{
return 'You seem to be using Valet, but the .env file does not contain the right default database credentials.';
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Facade\Ignition\Support;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class ComposerClassMap
{
/** @var \Composer\Autoload\ClassLoader|FakeComposer */
protected $composer;
/** @var string */
protected $basePath;
public function __construct(?string $autoloaderPath = null)
{
$autoloaderPath = $autoloaderPath ?? base_path('/vendor/autoload.php');
if (file_exists($autoloaderPath)) {
$this->composer = require $autoloaderPath;
} else {
$this->composer = new FakeComposer();
}
$this->basePath = app_path();
}
public function listClasses(): array
{
$classes = $this->composer->getClassMap();
return array_merge($classes, $this->listClassesInPsrMaps());
}
public function searchClassMap(string $missingClass): ?string
{
foreach ($this->composer->getClassMap() as $fqcn => $file) {
$basename = basename($file, '.php');
if ($basename === $missingClass) {
return $fqcn;
}
}
return null;
}
public function listClassesInPsrMaps(): array
{
// TODO: This is incorrect. Doesnt list all fqcns. Need to parse namespace? e.g. App\LoginController is wrong
$prefixes = array_merge(
$this->composer->getPrefixes(),
$this->composer->getPrefixesPsr4()
);
$classes = [];
foreach ($prefixes as $namespace => $directories) {
foreach ($directories as $directory) {
if (file_exists($directory)) {
$files = (new Finder())
->in($directory)
->files()
->name('*.php');
foreach ($files as $file) {
if ($file instanceof SplFileInfo) {
$fqcn = $this->getFullyQualifiedClassNameFromFile($namespace, $file);
$classes[$fqcn] = $file->getRelativePathname();
}
}
}
}
}
return $classes;
}
public function searchPsrMaps(string $missingClass): ?string
{
$prefixes = array_merge(
$this->composer->getPrefixes(),
$this->composer->getPrefixesPsr4()
);
foreach ($prefixes as $namespace => $directories) {
foreach ($directories as $directory) {
if (file_exists($directory)) {
$files = (new Finder())
->in($directory)
->files()
->name('*.php');
foreach ($files as $file) {
if ($file instanceof SplFileInfo) {
$basename = basename($file->getRelativePathname(), '.php');
if ($basename === $missingClass) {
return $namespace.basename($file->getRelativePathname(), '.php');
}
}
}
}
}
}
return null;
}
protected function getFullyQualifiedClassNameFromFile(string $rootNamespace, SplFileInfo $file): string
{
$class = trim(str_replace($this->basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR);
$class = str_replace(
[DIRECTORY_SEPARATOR, 'App\\'],
['\\', app()->getNamespace()],
ucfirst(Str::replaceLast('.php', '', $class))
);
return $rootNamespace.$class;
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Facade\Ignition\Support;
class FakeComposer
{
public function getClassMap()
{
return [];
}
public function getPrefixes()
{
return [];
}
public function getPrefixesPsr4()
{
return [];
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Facade\Ignition\Support;
class LaravelVersion
{
public static function major()
{
return substr(app()->version(), 0, 1);
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace Facade\Ignition\Support;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Livewire\LivewireManager;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
class LivewireComponentParser
{
/** @var string */
protected $componentAlias;
/** @var string */
protected $componentClass;
/** @var ReflectionClass */
protected $reflectionClass;
public static function create(string $componentAlias): self
{
return new self($componentAlias);
}
public function __construct(string $componentAlias)
{
$this->componentAlias = $componentAlias;
$this->componentClass = app(LivewireManager::class)->getClass($this->componentAlias);
$this->reflectionClass = new ReflectionClass($this->componentClass);
}
public function getComponentClass(): string
{
return $this->componentClass;
}
public function getPropertyNamesLike(string $similar): Collection
{
$properties = collect($this->reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC))
->reject(function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->class !== $this->reflectionClass->name;
})
->map(function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->name;
});
$computedProperties = collect($this->reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
->reject(function (ReflectionMethod $reflectionMethod) {
return $reflectionMethod->class !== $this->reflectionClass->name;
})
->filter(function (ReflectionMethod $reflectionMethod) {
return str_starts_with($reflectionMethod->name, 'get') && str_ends_with($reflectionMethod->name, 'Property');
})
->map(function (ReflectionMethod $reflectionMethod) {
return lcfirst(Str::of($reflectionMethod->name)->after('get')->before('Property'));
});
return $this->filterItemsBySimilarity(
$properties->merge($computedProperties),
$similar
);
}
public function getMethodNamesLike(string $similar): Collection
{
$methods = collect($this->reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
->reject(function (ReflectionMethod $reflectionMethod) {
return $reflectionMethod->class !== $this->reflectionClass->name;
})
->map(function (ReflectionMethod $reflectionMethod) {
return $reflectionMethod->name;
});
return $this->filterItemsBySimilarity($methods, $similar);
}
protected function filterItemsBySimilarity(Collection $items, string $similar): Collection
{
return $items
->map(function (string $name) use ($similar) {
similar_text($similar, $name, $percentage);
return ['match' => $percentage, 'value' => $name];
})
->sortByDesc('match')
->filter(function (array $item) {
return $item['match'] > 40;
})
->map(function (array $item) {
return $item['value'];
})
->values();
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Facade\Ignition\Support\Packagist;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
class Package
{
/** @var string */
public $name;
/** @var string */
public $url;
/** @var string */
public $repository;
public function __construct(array $properties)
{
$this->name = $properties['name'];
$this->url = $properties['url'];
$this->repository = $properties['repository'];
}
public function hasNamespaceThatContainsClassName(string $className): bool
{
return $this->getNamespaces()->contains(function ($namespace) use ($className) {
return Str::startsWith(strtolower($className), strtolower($namespace));
});
}
protected function getNamespaces(): Collection
{
$details = json_decode(file_get_contents("https://packagist.org/packages/{$this->name}.json"), true);
return collect($details['package']['versions'])
->map(function ($version) {
return collect($version['autoload'] ?? [])
->map(function ($autoload) {
return array_keys($autoload);
})
->flatten();
})
->flatten()
->unique();
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Facade\Ignition\Support\Packagist;
class Packagist
{
/**
* @param string $className
*
* @return \Facade\Ignition\Support\Packagist\Package[]
*/
public static function findPackagesForClassName(string $className): array
{
$parts = explode('\\', $className);
$queryParts = array_splice($parts, 0, 2);
$url = 'https://packagist.org/search.json?q='.implode(' ', $queryParts);
try {
$packages = json_decode(file_get_contents($url));
} catch (\Exception $e) {
return [];
}
return array_map(function ($packageProperties) {
return new Package((array) $packageProperties);
}, $packages->results);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace Facade\Ignition\Support;
use Facade\FlareClient\Report;
use Illuminate\Support\Arr;
class SentReports
{
/** @var array<int, Report> */
protected $reports = [];
public function add(Report $report): self
{
$this->reports[] = $report;
return $this;
}
public function all(): array
{
return $this->reports;
}
public function uuids(): array
{
return array_map(function (Report $report) {
return $report->trackingUuid();
}, $this->reports);
}
public function urls(): array
{
return array_map(function (string $trackingUuid) {
return "https://flareapp.io/tracked-occurrence/{$trackingUuid}";
}, $this->uuids());
}
public function latestUuid(): ?string
{
if (! $latestReport = Arr::last($this->reports)) {
return null;
}
return $latestReport->trackingUuid();
}
public function latestUrl(): ?string
{
return Arr::last($this->urls());
}
public function clear()
{
$this->reports = [];
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Facade\Ignition\Support;
use Illuminate\Support\Collection;
class StringComparator
{
public static function findClosestMatch(array $strings, string $input, int $sensitivity = 4): ?string
{
$closestDistance = -1;
$closestMatch = null;
foreach ($strings as $string) {
$levenshteinDistance = levenshtein($input, $string);
if ($levenshteinDistance === 0) {
$closestMatch = $string;
$closestDistance = 0;
break;
}
if ($levenshteinDistance <= $closestDistance || $closestDistance < 0) {
$closestMatch = $string;
$closestDistance = $levenshteinDistance;
}
}
if ($closestDistance <= $sensitivity) {
return $closestMatch;
}
return null;
}
public static function findSimilarText(array $strings, string $input): ?string
{
if (empty($strings)) {
return null;
}
return Collection::make($strings)
->sortByDesc(function (string $string) use ($input) {
similar_text($input, $string, $percentage);
return $percentage;
})
->first();
}
}

75
vendor/facade/ignition/src/Tabs/Tab.php vendored Normal file
View File

@@ -0,0 +1,75 @@
<?php
namespace Facade\Ignition\Tabs;
use Facade\FlareClient\Flare;
use Illuminate\Support\Str;
use JsonSerializable;
use Throwable;
abstract class Tab implements JsonSerializable
{
public $scripts = [];
public $styles = [];
/** @var \Facade\FlareClient\Flare */
protected $flare;
/** @var Throwable */
protected $throwable;
public function __construct()
{
$this->registerAssets();
}
public function name(): string
{
return Str::studly(class_basename(get_called_class()));
}
public function component(): string
{
return Str::snake(class_basename(get_called_class()), '-');
}
public function beforeRenderingErrorPage(Flare $flare, Throwable $throwable)
{
$this->flare = $flare;
$this->throwable = $throwable;
}
public function script(string $name, string $path)
{
$this->scripts[$name] = $path;
return $this;
}
public function style(string $name, string $path)
{
$this->styles[$name] = $path;
return $this;
}
abstract protected function registerAssets();
public function meta(): array
{
return [];
}
public function jsonSerialize()
{
return [
'title' => $this->name(),
'component' => $this->component(),
'props' => [
'meta' => $this->meta(),
],
];
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Facade\Ignition\Views\Compilers;
use ErrorException;
use Illuminate\View\Compilers\BladeCompiler;
class BladeSourceMapCompiler extends BladeCompiler
{
public function detectLineNumber(string $filename, int $exceptionLineNumber): int
{
try {
$map = $this->compileString(file_get_contents($filename));
} catch (ErrorException $e) {
return 1;
}
$map = explode("\n", $map);
$line = $map[$exceptionLineNumber - 1] ?? $exceptionLineNumber;
$pattern = '/\|---LINE:([0-9]+)---\|/m';
if (preg_match($pattern, (string)$line, $matches)) {
return (int)$matches[1];
}
return $exceptionLineNumber;
}
public function compileString($value)
{
try {
$value = $this->addEchoLineNumbers($value);
$value = $this->addStatementLineNumbers($value);
$value = parent::compileString($value);
return $this->trimEmptyLines($value);
} catch (\Exception $e) {
return $value;
}
}
protected function addEchoLineNumbers(string $value)
{
$pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
if (preg_match_all($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
foreach (array_reverse($matches[0]) as $match) {
$position = mb_strlen(substr($value, 0, $match[1]));
$value = $this->insertLineNumberAtPosition($position, $value);
}
}
return $value;
}
protected function addStatementLineNumbers(string $value)
{
$shouldInsertLineNumbers = preg_match_all(
'/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x',
$value,
$matches,
PREG_OFFSET_CAPTURE
);
if ($shouldInsertLineNumbers) {
foreach (array_reverse($matches[0]) as $match) {
$position = mb_strlen(substr($value, 0, $match[1]));
$value = $this->insertLineNumberAtPosition($position, $value);
}
}
return $value;
}
protected function insertLineNumberAtPosition(int $position, string $value)
{
$before = mb_substr($value, 0, $position);
$lineNumber = count(explode("\n", $before));
return mb_substr($value, 0, $position)."|---LINE:{$lineNumber}---|".mb_substr($value, $position);
}
protected function trimEmptyLines(string $value)
{
$value = preg_replace('/^\|---LINE:([0-9]+)---\|$/m', '', $value);
return ltrim($value, PHP_EOL);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Facade\Ignition\Views\Concerns;
use Illuminate\Foundation\Application;
use Illuminate\Support\Collection;
use Illuminate\View\Engines\CompilerEngine;
trait CollectsViewExceptions
{
protected $lastCompiledData = [];
public function collectViewData($path, array $data): void
{
$this->lastCompiledData[] = [
'path' => $path,
'compiledPath' => $this->getCompiledPath($path),
'data' => $this->filterViewData($data),
];
}
public function filterViewData(array $data): array
{
// By default, Laravel views get two shared data keys:
// __env and app. We try to filter them out.
return array_filter($data, function ($value, $key) {
if ($key === 'app') {
return ! $value instanceof Application;
}
return $key !== '__env';
}, ARRAY_FILTER_USE_BOTH);
}
public function getCompiledViewData($compiledPath): array
{
$compiledView = $this->findCompiledView($compiledPath);
return $compiledView['data'] ?? [];
}
public function getCompiledViewName($compiledPath): string
{
$compiledView = $this->findCompiledView($compiledPath);
return $compiledView['path'] ?? $compiledPath;
}
protected function findCompiledView($compiledPath): ?array
{
return Collection::make($this->lastCompiledData)
->first(function ($compiledData) use ($compiledPath) {
$comparePath = $compiledData['compiledPath'];
return realpath(dirname($comparePath)).DIRECTORY_SEPARATOR.basename($comparePath) === $compiledPath;
});
}
protected function getCompiledPath($path): string
{
if ($this instanceof CompilerEngine) {
return $this->getCompiler()->getCompiledPath($path);
}
return $path;
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Facade\Ignition\Views\Engines;
use Exception;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Exceptions\ViewExceptionWithSolution;
use Facade\Ignition\Views\Compilers\BladeSourceMapCompiler;
use Facade\Ignition\Views\Concerns\CollectsViewExceptions;
use Facade\IgnitionContracts\ProvidesSolution;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use ReflectionProperty;
use Throwable;
class CompilerEngine extends \Illuminate\View\Engines\CompilerEngine
{
use CollectsViewExceptions;
protected $currentPath = null;
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
*
* @return string
*/
public function get($path, array $data = [])
{
$this->currentPath = $path;
$this->collectViewData($path, $data);
return parent::get($path, $data);
}
/**
* Handle a view exception.
*
* @param \Throwable $baseException
* @param int $obLevel
*
* @return void
*
* @throws \Throwable
*/
protected function handleViewException(Throwable $baseException, $obLevel)
{
while (ob_get_level() > $obLevel) {
ob_end_clean();
}
if ($baseException instanceof ViewException) {
throw $baseException;
}
$viewExceptionClass = ViewException::class;
if ($baseException instanceof ProvidesSolution) {
$viewExceptionClass = ViewExceptionWithSolution::class;
}
$exception = new $viewExceptionClass(
$this->getMessage($baseException),
0,
1,
$this->getCompiledViewName($baseException->getFile()),
$this->getBladeLineNumber($baseException->getFile(), $baseException->getLine()),
$baseException
);
if ($baseException instanceof ProvidesSolution) {
$exception->setSolution($baseException->getSolution());
}
$this->modifyViewsInTrace($exception);
$exception->setView($this->getCompiledViewName($baseException->getFile()));
$exception->setViewData($this->getCompiledViewData($baseException->getFile()));
throw $exception;
}
protected function getBladeLineNumber(string $compiledPath, int $exceptionLineNumber): int
{
$viewPath = $this->getCompiledViewName($compiledPath);
if (! $viewPath) {
return $exceptionLineNumber;
}
$sourceMapCompiler = new BladeSourceMapCompiler(app(Filesystem::class), 'not-needed');
return $sourceMapCompiler->detectLineNumber($viewPath, $exceptionLineNumber);
}
protected function modifyViewsInTrace(ViewException $exception)
{
$trace = Collection::make($exception->getPrevious()->getTrace())
->map(function ($trace) {
if ($compiledData = $this->findCompiledView(Arr::get($trace, 'file', ''))) {
$trace['file'] = $compiledData['path'];
$trace['line'] = $this->getBladeLineNumber($trace['file'], $trace['line']);
}
return $trace;
})->toArray();
$traceProperty = new ReflectionProperty('Exception', 'trace');
$traceProperty->setAccessible(true);
$traceProperty->setValue($exception, $trace);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Facade\Ignition\Views\Engines;
use Exception;
use Facade\Ignition\Exceptions\ViewException;
use Facade\Ignition\Views\Concerns\CollectsViewExceptions;
use Throwable;
class PhpEngine extends \Illuminate\View\Engines\PhpEngine
{
use CollectsViewExceptions;
/**
* Get the evaluated contents of the view.
*
* @param string $path
* @param array $data
* @return string
*/
public function get($path, array $data = [])
{
$this->collectViewData($path, $data);
return parent::get($path, $data);
}
/**
* Handle a view exception.
*
* @param \Throwable $baseException
* @param int $obLevel
*
* @return void
*
* @throws \Throwable
*/
protected function handleViewException(Throwable $baseException, $obLevel)
{
$exception = new ViewException($baseException->getMessage(), 0, 1, $baseException->getFile(), $baseException->getLine(), $baseException);
$exception->setView($this->getCompiledViewName($baseException->getFile()));
$exception->setViewData($this->getCompiledViewData($baseException->getFile()));
parent::handleViewException($exception, $obLevel);
}
}

Some files were not shown because too many files have changed in this diff Show More