/src/Illuminate/Session/SessionServiceProvider.php

https://bitbucket.org/mikebosire/framework · PHP · 169 lines · 78 code · 29 blank · 62 comment · 3 complexity · f46eeeb70a24eade7c6b596ba1489313 MD5 · raw file

  1. <?php namespace Illuminate\Session;
  2. use Illuminate\Support\ServiceProvider;
  3. class SessionServiceProvider extends ServiceProvider {
  4. /**
  5. * Bootstrap the application events.
  6. *
  7. * @return void
  8. */
  9. public function boot()
  10. {
  11. $this->registerSessionEvents();
  12. }
  13. /**
  14. * Register the service provider.
  15. *
  16. * @return void
  17. */
  18. public function register()
  19. {
  20. $this->setupDefaultDriver();
  21. $this->registerSessionManager();
  22. $this->registerSessionDriver();
  23. }
  24. /**
  25. * Setup the default session driver for the application.
  26. *
  27. * @return void
  28. */
  29. protected function setupDefaultDriver()
  30. {
  31. if ($this->app->runningInConsole())
  32. {
  33. $this->app['config']['session.driver'] = 'array';
  34. }
  35. }
  36. /**
  37. * Register the session manager instance.
  38. *
  39. * @return void
  40. */
  41. protected function registerSessionManager()
  42. {
  43. $this->app['session.manager'] = $this->app->share(function($app)
  44. {
  45. return new SessionManager($app);
  46. });
  47. }
  48. /**
  49. * Register the session driver instance.
  50. *
  51. * @return void
  52. */
  53. protected function registerSessionDriver()
  54. {
  55. $this->app['session'] = $this->app->share(function($app)
  56. {
  57. // First, we will create the session manager which is responsible for the
  58. // creation of the various session drivers when they are needed by the
  59. // application instance, and will resolve them on a lazy load basis.
  60. $manager = $app['session.manager'];
  61. $driver = $manager->driver();
  62. $config = $app['config']['session'];
  63. // Once we get an instance of the session driver, we need to set a few of
  64. // the session options based on the application configuration, such as
  65. // the session lifetime and the sweeper lottery configuration value.
  66. $driver->setLifetime($config['lifetime']);
  67. $driver->setSweepLottery($config['lottery']);
  68. return $driver;
  69. });
  70. }
  71. /**
  72. * Register the events needed for session management.
  73. *
  74. * @return void
  75. */
  76. protected function registerSessionEvents()
  77. {
  78. $app = $this->app;
  79. $config = $app['config']['session'];
  80. // The session needs to be started and closed, so we will register a before
  81. // and after events to do all stuff for us. This will manage the loading
  82. // the session "payloads", as well as writing them after each request.
  83. if ( ! is_null($config['driver']))
  84. {
  85. $this->registerBootingEvent($app);
  86. $this->registerCloseEvent($app, $config);
  87. }
  88. }
  89. /**
  90. * Register the session booting event.
  91. *
  92. * @param \Illuminate\Foundation\Application $app
  93. * @return void
  94. */
  95. protected function registerBootingEvent($app)
  96. {
  97. $app->booting(function($app)
  98. {
  99. $app['session']->start($app['cookie'], $app['config']['session.cookie']);
  100. });
  101. }
  102. /**
  103. * Register the session close event.
  104. *
  105. * @param \Illuminate\Foundation\Application $app
  106. * @param array $config
  107. * @return void
  108. */
  109. protected function registerCloseEvent($app, $config)
  110. {
  111. $me = $this;
  112. $app->close(function($request, $response) use ($me, $app, $config)
  113. {
  114. $session = $app['session'];
  115. // Once we finish the session handling for the request, we will need to get a
  116. // cookie that we can attach to the response from the application. This is
  117. // used to identify the session on future requests into the application.
  118. $session->finish($response, $config['lifetime']);
  119. $cookie = $me->makeCookie($session, $config);
  120. if ( ! is_null($cookie))
  121. {
  122. $response->headers->setCookie($cookie);
  123. }
  124. });
  125. }
  126. /**
  127. * Create a session cookie based on the given config.
  128. *
  129. * @param \Illuminate\Session\Store $session
  130. * @param array $config
  131. * @return Symfony\Component\HttpFoundation\Cookie
  132. */
  133. public function makeCookie($session, $config)
  134. {
  135. $app = $this->app;
  136. return $session->getCookie(
  137. $app['cookie'], $config['cookie'], $config['lifetime'], $config['path'], $config['domain']
  138. );
  139. }
  140. }