ValidationServiceProvider.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Illuminate\Validation;
  3. use Illuminate\Support\ServiceProvider;
  4. class ValidationServiceProvider extends ServiceProvider
  5. {
  6. /**
  7. * Indicates if loading of the provider is deferred.
  8. *
  9. * @var bool
  10. */
  11. protected $defer = true;
  12. /**
  13. * Register the service provider.
  14. *
  15. * @return void
  16. */
  17. public function register()
  18. {
  19. $this->registerPresenceVerifier();
  20. $this->registerValidationFactory();
  21. }
  22. /**
  23. * Register the validation factory.
  24. *
  25. * @return void
  26. */
  27. protected function registerValidationFactory()
  28. {
  29. $this->app->singleton('validator', function ($app) {
  30. $validator = new Factory($app['translator'], $app);
  31. // The validation presence verifier is responsible for determining the existence of
  32. // values in a given data collection which is typically a relational database or
  33. // other persistent data stores. It is used to check for "uniqueness" as well.
  34. if (isset($app['db'], $app['validation.presence'])) {
  35. $validator->setPresenceVerifier($app['validation.presence']);
  36. }
  37. return $validator;
  38. });
  39. }
  40. /**
  41. * Register the database presence verifier.
  42. *
  43. * @return void
  44. */
  45. protected function registerPresenceVerifier()
  46. {
  47. $this->app->singleton('validation.presence', function ($app) {
  48. return new DatabasePresenceVerifier($app['db']);
  49. });
  50. }
  51. /**
  52. * Get the services provided by the provider.
  53. *
  54. * @return array
  55. */
  56. public function provides()
  57. {
  58. return [
  59. 'validator', 'validation.presence',
  60. ];
  61. }
  62. }