Skip to content

Commit

Permalink
Make ServiceProvider dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
the-pulli committed Dec 19, 2023
1 parent 4db69b0 commit 956bb02
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/CollectionMacroServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
namespace Pulli\CollectionMacros;

use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;

class CollectionMacroServiceProvider extends ServiceProvider
{
public function register(): void
if (class_exists('\Illuminate\Support\ServiceProvider')) {
class CollectionMacroServiceProvider extends \Illuminate\Support\ServiceProvider
{
Collection::make(self::macros())
->reject(fn ($class, $macro) => Collection::hasMacro($macro))
->each(fn ($class, $macro) => Collection::macro($macro, app($class)()));
}
public function register(): void
{
Collection::make(self::macros())
->reject(fn ($class, $macro) => Collection::hasMacro($macro))
->each(fn ($class, $macro) => Collection::macro($macro, app($class)()));
}

public static function macros(): array
{
return [
'mapToCollection' => \Pulli\CollectionMacros\Macros\MapToCollection::class,
'mapToCollectionFrom' => \Pulli\CollectionMacros\Macros\MapToCollectionFrom::class,
'recursiveToArray' => \Pulli\CollectionMacros\Macros\RecursiveToArray::class,
'recursiveToArrayFrom' => \Pulli\CollectionMacros\Macros\RecursiveToArrayFrom::class,
];
public static function macros(): array
{
return [
'mapToCollection' => \Pulli\CollectionMacros\Macros\MapToCollection::class,
'mapToCollectionFrom' => \Pulli\CollectionMacros\Macros\MapToCollectionFrom::class,
'recursiveToArray' => \Pulli\CollectionMacros\Macros\RecursiveToArray::class,
'recursiveToArrayFrom' => \Pulli\CollectionMacros\Macros\RecursiveToArrayFrom::class,
];
}
}
} else {
class CollectionMacroServiceProvider {}
}

0 comments on commit 956bb02

Please sign in to comment.