-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3b86e
commit f563bbd
Showing
3 changed files
with
92 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
{ | ||
"name": "lextira/larvel-formrequest-singleton", | ||
"description": "Use Laravel's excellent FormRequest as Singleton.", | ||
"license": "MIT", | ||
"require": { | ||
"php" : "^7.0", | ||
"laravel/framework": "^6.0" | ||
}, | ||
"autoload": { | ||
"psr-4" : { | ||
"Lextira\\FormRequestSingleton\\" : "src/" | ||
} | ||
"name": "lextira/laravel-formrequest-singleton", | ||
"description": "Use Laravel's excellent FormRequest as Singleton.", | ||
"license": "MIT", | ||
"require": { | ||
"php" : "^7.0", | ||
"laravel/framework": "^6.0" | ||
}, | ||
"autoload": { | ||
"psr-4" : { | ||
"Lextira\\FormRequestSingleton\\" : "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Lextira\FormRequestSingleton; | ||
|
||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Contracts\Validation\ValidatesWhenResolved; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Redirector; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class FormRequestServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->app->resolving(FormRequest::class, function ($request, Application $app) { | ||
|
||
// if the request is already bound, we don't need to do anything. | ||
if ($app->bound(get_class($request))) { | ||
return; | ||
} | ||
|
||
// but if not, we resolve a new FromRequest and register it as singleton. | ||
$this->resolveFormRequestAndRegisterAsSingleton($request, $app); | ||
}); | ||
} | ||
|
||
/** | ||
* Resolve a new FormRequest instance and bind it as singleton. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Illuminate\Contracts\Foundation\Application $app | ||
* @throws | ||
*/ | ||
protected function resolveFormRequestAndRegisterAsSingleton(Request $request, Application $app) | ||
{ | ||
$request = FormRequest::createFrom($app['request'], $request); | ||
|
||
$request->setContainer($app)->setRedirector($app->make(Redirector::class)); | ||
|
||
if ($request instanceof ValidatesWhenResolved) { | ||
$request->validateResolved(); | ||
} | ||
|
||
$app->singleton(get_class($request), function(Application $app) use ($request) { | ||
return $request; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Lextira\FormRequestSingleton; | ||
|
||
use Illuminate\Foundation\Providers\FoundationServiceProvider as IlluminateFoundationServiceProvider; | ||
|
||
class FoundationServiceProvider extends IlluminateFoundationServiceProvider | ||
{ | ||
/** | ||
* The provider class names. | ||
* | ||
* @var array | ||
*/ | ||
protected $providers = [ | ||
FormRequestServiceProvider::class, | ||
]; | ||
} |