App\Services\Filter -> Blessing\Filter
This commit is contained in:
parent
7f46e1510b
commit
d40726a718
|
|
@ -7,10 +7,10 @@ use App\Models\Texture;
|
|||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use App\Rules;
|
||||
use App\Services\Filter;
|
||||
use App\Services\OptionForm;
|
||||
use App\Services\PluginManager;
|
||||
use Auth;
|
||||
use Blessing\Filter;
|
||||
use Cache;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use Auth;
|
||||
use Blessing\Filter;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ClosetController extends Controller
|
||||
|
|
|
|||
|
|
@ -12,9 +12,9 @@ use App\Http\Middleware\CheckPlayerOwner;
|
|||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Rules;
|
||||
use App\Services\Filter;
|
||||
use App\Services\Rejection;
|
||||
use Auth;
|
||||
use Blessing\Filter;
|
||||
use Event;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ namespace App\Http\Controllers;
|
|||
use App\Models\Report;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use App\Services\Rejection;
|
||||
use Blessing\Filter;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ namespace App\Http\Controllers;
|
|||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use Auth;
|
||||
use Blessing\Filter;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Option;
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ use App\Events\UserProfileUpdated;
|
|||
use App\Mail\EmailVerification;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use App\Services\Rejection;
|
||||
use Auth;
|
||||
use Blessing\Filter;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace App\Http\View\Composers;
|
||||
|
||||
use App\Services\Filter;
|
||||
use Blessing\Filter;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace App\Http\View\Composers;
|
||||
|
||||
use App\Services\Filter;
|
||||
use Blessing\Filter;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\View\View;
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ class AppServiceProvider extends ServiceProvider
|
|||
$this->app->singleton(Services\Option::class);
|
||||
$this->app->alias(Services\Option::class, 'options');
|
||||
$this->app->singleton(Services\Webpack::class);
|
||||
$this->app->singleton(Services\Filter::class);
|
||||
$this->app->singleton('oauth.providers', function () {
|
||||
return new \Illuminate\Support\Collection();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class Filter
|
||||
{
|
||||
protected $listeners = [];
|
||||
|
||||
public function add(string $hook, Closure $callback, $priority = 20)
|
||||
{
|
||||
if (!isset($this->listeners[$hook])) {
|
||||
$this->listeners[$hook] = collect();
|
||||
}
|
||||
|
||||
$this->listeners[$hook]->push([
|
||||
'callback' => $callback,
|
||||
'priority' => $priority,
|
||||
]);
|
||||
}
|
||||
|
||||
public function apply(string $hook, $init, $args = [])
|
||||
{
|
||||
$listeners = $this->getListeners($hook);
|
||||
if ($listeners->isNotEmpty()) {
|
||||
return $this->listeners[$hook]
|
||||
->sortBy('priority')
|
||||
->reduce(function ($carry, $item) use ($args) {
|
||||
return call_user_func($item['callback'], $carry, ...$args);
|
||||
}, $init);
|
||||
} else {
|
||||
return $init;
|
||||
}
|
||||
}
|
||||
|
||||
public function remove(string $hook)
|
||||
{
|
||||
unset($this->listeners[$hook]);
|
||||
}
|
||||
|
||||
public function getListeners(string $hook): Collection
|
||||
{
|
||||
return Arr::get($this->listeners, $hook, collect());
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@
|
|||
"ext-tokenizer": "*",
|
||||
"ext-xml": "*",
|
||||
"ext-zip": "*",
|
||||
"blessing/filter": "^1.0",
|
||||
"composer/ca-bundle": "^1.2",
|
||||
"composer/semver": "^1.4",
|
||||
"doctrine/dbal": "^2.9",
|
||||
|
|
|
|||
55
composer.lock
generated
55
composer.lock
generated
|
|
@ -4,8 +4,61 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "7941921d4db971b81f1b97420c1831a6",
|
||||
"content-hash": "3a91095a48c06e8fc97341f7e0f8ad9e",
|
||||
"packages": [
|
||||
{
|
||||
"name": "blessing/filter",
|
||||
"version": "v1.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bs-community/filter.git",
|
||||
"reference": "ce6227b621cbf071d89214ab5bebcb1fcf797fcd"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/bs-community/filter/zipball/ce6227b621cbf071d89214ab5bebcb1fcf797fcd",
|
||||
"reference": "ce6227b621cbf071d89214ab5bebcb1fcf797fcd",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"illuminate/contracts": "6.*",
|
||||
"illuminate/support": "6.*"
|
||||
},
|
||||
"require-dev": {
|
||||
"illuminate/container": "6.*",
|
||||
"phpunit/phpunit": "~8.0"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Blessing\\FilterServiceProvider"
|
||||
]
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Blessing\\": "src/Blessing"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Pig Fang",
|
||||
"email": "g-plane@hotmail.com"
|
||||
}
|
||||
],
|
||||
"description": "Filters API for designing plugin system.",
|
||||
"keywords": [
|
||||
"filters",
|
||||
"laravel",
|
||||
"wordpress"
|
||||
],
|
||||
"time": "2019-12-27T15:01:44+00:00"
|
||||
},
|
||||
{
|
||||
"name": "composer/ca-bundle",
|
||||
"version": "1.2.5",
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Services\Filter as BaseFilter;
|
||||
use Blessing\Filter as BaseFilter;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class Filter extends BaseFilter
|
||||
|
|
@ -18,7 +18,7 @@ class Filter extends BaseFilter
|
|||
|
||||
public static function fake(): Filter
|
||||
{
|
||||
$fake = new self();
|
||||
$fake = resolve(Filter::class);
|
||||
|
||||
app()->instance(BaseFilter::class, $fake);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ use App\Events;
|
|||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use App\Services\Rejection;
|
||||
use Blessing\Filter;
|
||||
use Event;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ namespace Tests;
|
|||
use App\Models\Report;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use App\Services\Rejection;
|
||||
use Blessing\Filter;
|
||||
use Event;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ namespace Tests;
|
|||
use App\Models\Player;
|
||||
use App\Models\Texture;
|
||||
use App\Models\User;
|
||||
use App\Services\Filter;
|
||||
use Blessing\Filter;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ use App\Events;
|
|||
use App\Mail\EmailVerification;
|
||||
use App\Models\User;
|
||||
use App\Notifications;
|
||||
use App\Services\Filter;
|
||||
use App\Services\Rejection;
|
||||
use Blessing\Filter;
|
||||
use Carbon\Carbon;
|
||||
use Event;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
|
|
|||
|
|
@ -1,56 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Services\Filter;
|
||||
|
||||
class FilterTest extends TestCase
|
||||
{
|
||||
public function testAdd()
|
||||
{
|
||||
$filter = new Filter();
|
||||
$filter->add('hook', function () {
|
||||
});
|
||||
$filter->add('hook', function () {
|
||||
}, 10);
|
||||
$this->assertCount(2, $filter->getListeners('hook'));
|
||||
}
|
||||
|
||||
public function testApply()
|
||||
{
|
||||
$filter = new Filter();
|
||||
$this->assertEquals('value', $filter->apply('hook', 'value', ['add']));
|
||||
|
||||
$filter->add('hook', function ($value, $addition) {
|
||||
$this->assertEquals('add', $addition);
|
||||
|
||||
return $value.'_medium';
|
||||
});
|
||||
$filter->add('hook', function ($value) {
|
||||
return $value.'_low';
|
||||
}, 10);
|
||||
$filter->add('hook', function ($value) {
|
||||
return $value.'_high';
|
||||
}, 30);
|
||||
$this->assertEquals('value_low_medium_high', $filter->apply('hook', 'value', ['add']));
|
||||
}
|
||||
|
||||
public function testRemove()
|
||||
{
|
||||
$filter = new Filter();
|
||||
$filter->remove('hook');
|
||||
$this->assertCount(0, $filter->getListeners('hook'));
|
||||
|
||||
$filter->add('hook', function () {
|
||||
});
|
||||
$this->assertCount(1, $filter->getListeners('hook'));
|
||||
$filter->remove('hook');
|
||||
$this->assertCount(0, $filter->getListeners('hook'));
|
||||
}
|
||||
|
||||
public function testGetListeners()
|
||||
{
|
||||
$filter = new Filter();
|
||||
$this->assertCount(0, $filter->getListeners('hook'));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user