├── composer.json ├── readme.md └── src ├── CacheContainer.php ├── Middelware └── AddUniqueIdentifier.php └── UniqueRequestServiceProvider.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inani/laravel-same-request", 3 | "description": "The missing Laravel's request check on its unicity.", 4 | "keywords": ["laravel", "request", "unique", "php"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Inani El Houssain", 9 | "email": "inanielhoussain@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "stable", 13 | "autoload": { 14 | "psr-4": { 15 | "Inani\\UniqueRequest\\": "src/" 16 | } 17 | }, 18 | "require": { 19 | "illuminate/support": "~5|~6|~7|~8|~9|~10" 20 | }, 21 | "require-dev": { 22 | "fzaninotto/faker": "~1.4", 23 | "phpunit/phpunit": "~4.0" 24 | }, 25 | "extra": { 26 | "laravel": { 27 | "providers": [ 28 | "Inani\\UniqueRequest\\UniqueRequestServiceProvider" 29 | ] 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Motivation 2 | 3 | [![Total Downloads](https://img.shields.io/packagist/dt/inani/laravel-same-request.svg?style=flat-square)]([https://packagist.org/packages/spatie/laravel-rate-limited-job-middleware](https://packagist.org/packages/inani/laravel-same-request)) 4 | 5 | This laravel package will allow you to execute a code once in the current request based on the key provided. 6 | 7 | ## Installation 8 | 9 | ```` 10 | composer require inani/laravel-same-request 11 | ```` 12 | 13 | You will(for L5) need to register the service provider in the ````config/app.php```` . 14 | ````php 15 | return [ 16 | /* 17 | * Package Service Providers... 18 | */ 19 | 20 | /* 21 | * Application Service Providers... 22 | */ 23 | App\Providers\AppServiceProvider::class, 24 | App\Providers\AuthServiceProvider::class, 25 | // App\Providers\BroadcastServiceProvider::class, 26 | App\Providers\EventServiceProvider::class, 27 | App\Providers\RouteServiceProvider::class, 28 | Inani\UniqueRequest\UniqueRequestServiceProvider::class, // <=== HERE 29 | ]; 30 | ```` 31 | 32 | You will need to use the middleware or binding it globally in your ````app/Http/Kernel.php```` 33 | 34 | ````php 35 | return [ 36 | 'api' => [ 37 | 'throttle:120,1', 38 | 'bindings', 39 | PreventCache::class, 40 | GetJwtFromCookie::class, 41 | AddUniqueIdentifier::class 42 | ], 43 | ]; 44 | ```` 45 | 46 | 47 | 48 | ## Usage 49 | 50 | 51 | ````php 52 | for ($i= 0; $i < 2; $i++){ 53 | $greeting = request()->once(function (){ 54 | info('entred once'); 55 | return 'hello'; 56 | }, 'greeting'); 57 | } 58 | 59 | $goodbye = request()->once(function (){ 60 | return 'Saynoara'; 61 | }, 'bye'); 62 | 63 | $goodbye = request()->once(function (){ 64 | return 'ByeBye'; 65 | }, 'bye'); 66 | 67 | return [$greeting, $goodbye]; 68 | 69 | ```` 70 | -------------------------------------------------------------------------------- /src/CacheContainer.php: -------------------------------------------------------------------------------- 1 | entries)){ 14 | $this->entries[$key] = $value(); 15 | } 16 | 17 | return $this->entries[$key]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Middelware/AddUniqueIdentifier.php: -------------------------------------------------------------------------------- 1 | merge([ 22 | 'uuid_request' => uniqid(). '_' . time() 23 | ]); 24 | 25 | return $next($request); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/UniqueRequestServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('localeCache', function(){ 18 | return new CacheContainer(); 19 | }); 20 | Request::macro('once', function (Closure $function, $key) { 21 | if(!request()->has('uuid_request')){ 22 | return $function(); 23 | } 24 | return app('localeCache')->remember($key. '_' . request()->get('uuid_request'), $function); 25 | }); 26 | } 27 | } 28 | --------------------------------------------------------------------------------