Skip to content

Commit fe573bd

Browse files
committed
fix: wip
1 parent 0d37f5a commit fe573bd

File tree

9 files changed

+25
-22
lines changed

9 files changed

+25
-22
lines changed

src/Exceptions/RestifyHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Binaryk\LaravelRestify\Exceptions;
44

5-
use Binaryk\LaravelRestify\Exceptions\Solutions\OpenAiSolution;
5+
use Binaryk\LaravelRestify\Exceptions\Solutions\AiSolution;
66
use Illuminate\Foundation\Exceptions\Handler;
77
use Throwable;
88

@@ -24,7 +24,7 @@ protected function convertExceptionToArray(Throwable $e): array
2424
return $response;
2525
}
2626

27-
$solution = (new OpenAiSolution($e))->getSolutionDescription();
27+
$solution = (new AiSolution($e))->getSolutionDescription();
2828

2929
return array_merge([
3030
'restify-solution' => $solution,

src/Exceptions/Solutions/OpenAiSolution.php renamed to src/Exceptions/Solutions/AiSolution.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22

33
namespace Binaryk\LaravelRestify\Exceptions\Solutions;
44

5+
use Illuminate\Http\Request;
56
use Illuminate\Support\Facades\Cache;
67
use Prism\Prism\Prism;
78
use Spatie\Backtrace\Backtrace;
89
use Spatie\Backtrace\Frame;
910
use Throwable;
1011

11-
class OpenAiSolution
12+
class AiSolution
1213
{
1314
protected mixed $solution;
1415

1516
public function __construct(protected Throwable $throwable)
1617
{
1718
$cacheKey = 'restify-solutions-'.sha1(
18-
$this->throwable::class.
19-
$this->throwable->getMessage().
20-
$this->throwable->getFile().
21-
$this->throwable->getLine().
22-
$this->throwable->getTraceAsString()
23-
);
19+
$this->throwable::class.
20+
$this->throwable->getMessage().
21+
$this->throwable->getFile().
22+
$this->throwable->getLine().
23+
$this->throwable->getTraceAsString()
24+
);
2425

2526
$this->solution = Cache::remember($cacheKey,
2627
now()->addHour(),
27-
fn () => trim(Prism::text()
28+
fn() => trim(Prism::text()
2829
->using(config('restify.ai_solutions.provider'), config('restify.ai_solutions.model'))
2930
->withSystemPrompt('You are an expert PHP/Laravel developer. Provide concise, actionable solutions in a single paragraph without line breaks, code blocks, or formatting. Your response should be suitable for JSON API responses.')
3031
->withPrompt($this->generatePrompt($this->throwable))
@@ -33,6 +34,14 @@ public function __construct(protected Throwable $throwable)
3334
);
3435
}
3536

37+
public static function canUse(Request $request): bool
38+
{
39+
return $request->expectsJson() &&
40+
config('restify.ai_solutions') &&
41+
config('app.debug') &&
42+
config('prism.providers.openai.api_key');
43+
}
44+
3645
public function getSolutionTitle(): string
3746
{
3847
return 'AI Generated Solution';
@@ -63,7 +72,7 @@ protected function generatePrompt(Throwable $throwable): string
6372
$snippet = $applicationFrame->getSnippet(15);
6473

6574
return (string) view('restify::prompts.prompt', [
66-
'snippet' => collect($snippet)->map(fn ($line, $number) => $number.' '.$line)->join(PHP_EOL),
75+
'snippet' => collect($snippet)->map(fn($line, $number) => $number.' '.$line)->join(PHP_EOL),
6776
'file' => $applicationFrame->file,
6877
'line' => $applicationFrame->lineNumber,
6978
'exception' => $throwable->getMessage(),
@@ -102,7 +111,8 @@ protected function getMethodsFrom($filePath): string
102111
$methods = [];
103112

104113
// Extract public method signatures using regex
105-
preg_match_all('/public\s+function\s+(\w+)\s*\([^)]*\)(?:\s*:\s*[^{]+)?\s*{/m', $content, $matches, PREG_OFFSET_CAPTURE);
114+
preg_match_all('/public\s+function\s+(\w+)\s*\([^)]*\)(?:\s*:\s*[^{]+)?\s*{/m', $content, $matches,
115+
PREG_OFFSET_CAPTURE);
106116

107117
foreach ($matches[0] as $match) {
108118
$methodSignature = trim($match[0]);

src/Services/Search/RepositorySearchService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function search(RestifyRequest $request, Repository $repository): Builder
2929

3030
if ($repository::usesScout()) {
3131
$scoutQuery = $this->initializeQueryUsingScout($request, $repository);
32+
3233
}
3334

3435
$query = $this->prepareMatchFields(

tests/Controllers/GlobalSearchControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostPolicy;
88
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
99
use Binaryk\LaravelRestify\Tests\IntegrationTestCase;
10-
use Illuminate\Foundation\Testing\RefreshDatabase;
1110
use Illuminate\Support\Facades\Gate;
1211
use Illuminate\Testing\Fluent\AssertableJson;
1312

1413
class GlobalSearchControllerTest extends IntegrationTestCase
1514
{
16-
use RefreshDatabase;
1715

1816
public function test_global_search_returns_matches(): void
1917
{

tests/Controllers/Index/IndexRelatedFeatureTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
2222
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
2323
use Binaryk\LaravelRestify\Tests\IntegrationTestCase;
24-
use Illuminate\Foundation\Testing\RefreshDatabase;
2524
use Illuminate\Http\Request;
2625
use Illuminate\Testing\Fluent\AssertableJson;
2726
use PHPUnit\Framework\Attributes\Test;
2827

2928
class IndexRelatedFeatureTest extends IntegrationTestCase
3029
{
31-
use RefreshDatabase;
3230

3331
protected function setUp(): void
3432
{

tests/Controllers/Index/RepositoryIndexControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
1010
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
1111
use Binaryk\LaravelRestify\Tests\IntegrationTestCase;
12-
use Illuminate\Foundation\Testing\RefreshDatabase;
1312
use Illuminate\Testing\Fluent\AssertableJson;
1413
use PHPUnit\Framework\Attributes\Test;
1514

1615
class RepositoryIndexControllerTest extends IntegrationTestCase
1716
{
18-
use RefreshDatabase;
1917

2018
#[Test]
2119
public function it_can_paginate(): void

tests/Controllers/Show/ShowRelatedFeatureTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
use Binaryk\LaravelRestify\Tests\Fixtures\Comment\CommentRepository;
99
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
1010
use Binaryk\LaravelRestify\Tests\IntegrationTestCase;
11-
use Illuminate\Foundation\Testing\RefreshDatabase;
1211
use Illuminate\Testing\Fluent\AssertableJson;
1312

1413
class ShowRelatedFeatureTest extends IntegrationTestCase
1514
{
16-
use RefreshDatabase;
1715

1816
public function test_show_related_doesnt_load_for_nested_relationships_that_didnt_require_it(): void
1917
{

tests/Feature/AuthorizableModelsTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
use Binaryk\LaravelRestify\Tests\IntegrationTestCase;
1212
use Carbon\Carbon;
1313
use Carbon\CarbonInterface;
14-
use Illuminate\Foundation\Testing\RefreshDatabase;
1514
use Illuminate\Support\Facades\Cache;
1615
use Illuminate\Support\Facades\Gate;
1716

1817
class AuthorizableModelsTest extends IntegrationTestCase
1918
{
20-
use RefreshDatabase;
2119

2220
protected function setUp(): void
2321
{

tests/IntegrationTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Binaryk\LaravelRestify\RestifyApplicationServiceProvider;
1111
use Binaryk\LaravelRestify\Tests\Concerns\Mockers;
1212
use Binaryk\LaravelRestify\Tests\Fixtures\Comment\Comment;
13+
use Illuminate\Foundation\Testing\RefreshDatabase;
1314
use Binaryk\LaravelRestify\Tests\Fixtures\Comment\CommentPolicy;
1415
use Binaryk\LaravelRestify\Tests\Fixtures\Comment\CommentRepository;
1516
use Binaryk\LaravelRestify\Tests\Fixtures\Company\Company;
@@ -39,6 +40,7 @@ abstract class IntegrationTestCase extends TestCase
3940
{
4041
use Mockers;
4142
use Prototypes;
43+
use RefreshDatabase;
4244

4345
protected Mockery\MockInterface|User|null $authenticatedAs = null;
4446

0 commit comments

Comments
 (0)