Skip to content

Commit

Permalink
chore: Release v2.1.0
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Gaspar <[email protected]>
  • Loading branch information
brunogaspar committed Sep 6, 2019
1 parent ba21262 commit 3f2a186
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -43,7 +43,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "2.1.x-dev"
},
"laravel": {
"providers": [
Expand Down
15 changes: 15 additions & 0 deletions config/collection-macros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Werxe\Laravel\CollectionMacros\Macros\Ksort;
use Werxe\Laravel\CollectionMacros\Macros\Krsort;
use Werxe\Laravel\CollectionMacros\Macros\Decrement;
use Werxe\Laravel\CollectionMacros\Macros\Increment;
use Werxe\Laravel\CollectionMacros\Macros\Recursive;

return [
'increment' => Increment::class,
'decrement' => Decrement::class,
'krsort' => Krsort::class,
'ksort' => Ksort::class,
'recursive' => Recursive::class,
];
36 changes: 25 additions & 11 deletions src/CollectionMacrosServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)());
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
}

0 comments on commit 3f2a186

Please sign in to comment.