diff --git a/CHANGELOG.md b/CHANGELOG.md index fbae997..879be9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [v2.1.0] - 2019-09-06 + +### Updated + +- Made macros configurable by adding a config file. + ## [v2.0.1] - 2019-08-27 ### Fixed diff --git a/README.md b/README.md index bb3f24d..4f4f6d2 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,27 @@ Version | Laravel | PHP Version 2.x | 5.8.x | >= 7.1.3 1.x | 5.7.x | >= 7.1.3 -## Available Macros +## Getting Started + +Install the package via [Composer](https://getcomposer.org/) by running: + +``` +composer require werxe/laravel-collection-macros +``` + +By default all macros are enabled and the macro name is the lower cased version of the macro class. + +If you want to customize which macros are enabled or just rename the macro name, you can do so by publishing the configuration file, by running: + +``` +php artisan vendor:publish --tag="werxe:collection-macros.config" +``` + +The configuration file is now published at `config/werxe/collection-macros/config.php`. + +## Documentation + +### Available Macros - [`increment`](#increment) - [`decrement`](#decrement) diff --git a/composer.json b/composer.json index 9fe11dd..6eeae7a 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,9 @@ "illuminate/support": "5.8.*" }, "require-dev": { - "werxe/php-cs-fixer-config": "^1.0", "orchestra/testbench": "^3.8", - "phpunit/phpunit": "^7.5" + "phpunit/phpunit": "^7.5", + "werxe/php-cs-fixer-config": "^1.0" }, "autoload": { "psr-4": { @@ -43,7 +43,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "2.1.x-dev" }, "laravel": { "providers": [ diff --git a/config/collection-macros.php b/config/collection-macros.php new file mode 100644 index 0000000..b5860d9 --- /dev/null +++ b/config/collection-macros.php @@ -0,0 +1,15 @@ + Increment::class, + 'decrement' => Decrement::class, + 'krsort' => Krsort::class, + 'ksort' => Ksort::class, + 'recursive' => Recursive::class, +]; diff --git a/src/CollectionMacrosServiceProvider.php b/src/CollectionMacrosServiceProvider.php index 39accf1..5e5f7bb 100644 --- a/src/CollectionMacrosServiceProvider.php +++ b/src/CollectionMacrosServiceProvider.php @@ -2,28 +2,42 @@ namespace Werxe\Laravel\CollectionMacros; -use Illuminate\Support\Str; use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; class CollectionMacrosServiceProvider extends ServiceProvider { /** - * {@inheritdoc} + * Bootstrap any package services. + * + * @return void */ - public function register() + public function boot() { - $macros = glob(__DIR__.'/Macros/*.php'); - - foreach ($macros as $macroPath) { - $macroClass = pathinfo($macroPath, PATHINFO_FILENAME); + if ($this->app->runningInConsole()) { + // Publish config + $this->publishes([ + realpath(__DIR__.'/../config/collection-macros.php') => config_path('werxe/collection-macros/config.php'), + ], 'werxe:collection-macros.config'); + } + } - $macroName = Str::camel($macroClass); + /** + * Register any package services. + * + * @return void + */ + public function register() + { + $this->mergeConfigFrom( + realpath(__DIR__.'/../config/collection-macros.php'), 'werxe.collection-macros.config' + ); - if (! Collection::hasMacro($macroName)) { - $class = "Werxe\\Laravel\\CollectionMacros\\Macros\\{$macroClass}"; + $macros = $this->app['config']->get('werxe.collection-macros.config'); - Collection::macro($macroName, app($class)()); + foreach ($macros as $macro => $class) { + if (! Collection::hasMacro($macro)) { + Collection::macro($macro, app($class)()); } } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 54d6c98..1c86f2c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,21 +2,18 @@ namespace Werxe\Laravel\CollectionMacros\Tests; -use ReflectionClass; -use PHPUnit\Framework\TestCase as BaseTestCase; +use Orchestra\Testbench\TestCase as OrchestraTestCase; use Werxe\Laravel\CollectionMacros\CollectionMacrosServiceProvider; -abstract class TestCase extends BaseTestCase +abstract class TestCase extends OrchestraTestCase { /** - * This method is called before each test. - * - * @return void + * {@inheritdoc} */ - protected function setUp(): void + protected function getPackageProviders($app) { - $provider = new ReflectionClass(CollectionMacrosServiceProvider::class); - - $provider->newInstanceWithoutConstructor()->register(); + return [ + CollectionMacrosServiceProvider::class, + ]; } }