Skip to content

Commit

Permalink
feat(macros): Add JSON macro to Collection class
Browse files Browse the repository at this point in the history
- Introduce a new `CollectionMacMini` that provides a `json()` method
- Update `CommitCommand` to utilize the new Collection method for JSON decoding
- Enhance `_ide_helper.php` by correcting namespace references for `Str` and `Collection`
- Improve code organization and maintainability with added macros
  • Loading branch information
ityaozm@gmail.com committed Nov 19, 2024
1 parent 974bd06 commit 46dde21
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
15 changes: 12 additions & 3 deletions _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

namespace Illuminate\Support {
/**
* @method static bool jsonValidate(string $json, int $depth = 512, int $flags = 0)
* @method static \Illuminate\Support\Collection json(string $json, int $depth = 512, int $options = 0)
*
* @mixin \Illuminate\Support\Str
* @mixin \Illuminate\Support\Collection
*/
final class Str
final class Collection
{
}

Expand All @@ -28,6 +28,15 @@ final class Str
final class Stringable
{
}

/**
* @method static bool jsonValidate(string $json, int $depth = 512, int $flags = 0)
*
* @mixin \Illuminate\Support\Str
*/
final class Str
{
}
}

namespace App\Support {
Expand Down
2 changes: 1 addition & 1 deletion app/Commands/CommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function ($attempts) use ($cachedDiff, $type): string {
);
})
->tap(function () use (&$message): void {
$message = collect(json_decode($message, true, 512, JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR))
$message = Collection::json($message)
->map(static function ($content) {
if (\is_array($content)) {
return collect($content)
Expand Down
35 changes: 35 additions & 0 deletions app/Macros/CollectionMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/ai-commit.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace App\Macros;

/**
* @mixin \Illuminate\Support\Collection
*/
class CollectionMacro
{
/**
* @noinspection JsonEncodingApiUsageInspection
* @noinspection PhpMethodParametersCountMismatchInspection
*/
public static function json(): callable
{
return static function (string $json, int $depth = 512, int $options = 0): self {
return new static(json_decode(
$json,
true,
$depth,
$options
));
};
}
}
8 changes: 6 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

use App\ConfigManager;
use App\GeneratorManager;
use App\Macros\CollectionMacro;
use App\Macros\StringableMacro;
use App\Macros\StrMacro;
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
Expand All @@ -33,9 +35,10 @@ final class AppServiceProvider extends ServiceProvider
* @var array<array-key, string>
*/
public $singletons = [
CollectionMacro::class => CollectionMacro::class,
GeneratorManager::class => GeneratorManager::class,
StringableMacro::class => StringableMacro::class,
StrMacro::class => StrMacro::class,
GeneratorManager::class => GeneratorManager::class,
];

/**
Expand All @@ -50,8 +53,9 @@ public function register(): void
return new OutputStyle(new ArgvInput(), new ConsoleOutput());
});

Stringable::mixin($this->app->make(StringableMacro::class));
Collection::mixin($this->app->make(CollectionMacro::class));
Str::mixin($this->app->make(StrMacro::class));
Stringable::mixin($this->app->make(StringableMacro::class));
}

/**
Expand Down

0 comments on commit 46dde21

Please sign in to comment.