SessionManager.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Illuminate\Session;
  3. use Illuminate\Support\Manager;
  4. class SessionManager extends Manager
  5. {
  6. /**
  7. * Call a custom driver creator.
  8. *
  9. * @param string $driver
  10. * @return mixed
  11. */
  12. protected function callCustomCreator($driver)
  13. {
  14. return $this->buildSession(parent::callCustomCreator($driver));
  15. }
  16. /**
  17. * Create an instance of the "array" session driver.
  18. *
  19. * @return \Illuminate\Session\Store
  20. */
  21. protected function createArrayDriver()
  22. {
  23. return $this->buildSession(new NullSessionHandler);
  24. }
  25. /**
  26. * Create an instance of the "cookie" session driver.
  27. *
  28. * @return \Illuminate\Session\Store
  29. */
  30. protected function createCookieDriver()
  31. {
  32. return $this->buildSession(new CookieSessionHandler(
  33. $this->app['cookie'], $this->app['config']['session.lifetime']
  34. ));
  35. }
  36. /**
  37. * Create an instance of the file session driver.
  38. *
  39. * @return \Illuminate\Session\Store
  40. */
  41. protected function createFileDriver()
  42. {
  43. return $this->createNativeDriver();
  44. }
  45. /**
  46. * Create an instance of the file session driver.
  47. *
  48. * @return \Illuminate\Session\Store
  49. */
  50. protected function createNativeDriver()
  51. {
  52. $lifetime = $this->app['config']['session.lifetime'];
  53. return $this->buildSession(new FileSessionHandler(
  54. $this->app['files'], $this->app['config']['session.files'], $lifetime
  55. ));
  56. }
  57. /**
  58. * Create an instance of the database session driver.
  59. *
  60. * @return \Illuminate\Session\Store
  61. */
  62. protected function createDatabaseDriver()
  63. {
  64. $table = $this->app['config']['session.table'];
  65. $lifetime = $this->app['config']['session.lifetime'];
  66. return $this->buildSession(new DatabaseSessionHandler(
  67. $this->getDatabaseConnection(), $table, $lifetime, $this->app
  68. ));
  69. }
  70. /**
  71. * Get the database connection for the database driver.
  72. *
  73. * @return \Illuminate\Database\Connection
  74. */
  75. protected function getDatabaseConnection()
  76. {
  77. $connection = $this->app['config']['session.connection'];
  78. return $this->app['db']->connection($connection);
  79. }
  80. /**
  81. * Create an instance of the APC session driver.
  82. *
  83. * @return \Illuminate\Session\Store
  84. */
  85. protected function createApcDriver()
  86. {
  87. return $this->createCacheBased('apc');
  88. }
  89. /**
  90. * Create an instance of the Memcached session driver.
  91. *
  92. * @return \Illuminate\Session\Store
  93. */
  94. protected function createMemcachedDriver()
  95. {
  96. return $this->createCacheBased('memcached');
  97. }
  98. /**
  99. * Create an instance of the Redis session driver.
  100. *
  101. * @return \Illuminate\Session\Store
  102. */
  103. protected function createRedisDriver()
  104. {
  105. $handler = $this->createCacheHandler('redis');
  106. $handler->getCache()->getStore()->setConnection(
  107. $this->app['config']['session.connection']
  108. );
  109. return $this->buildSession($handler);
  110. }
  111. /**
  112. * Create an instance of a cache driven driver.
  113. *
  114. * @param string $driver
  115. * @return \Illuminate\Session\Store
  116. */
  117. protected function createCacheBased($driver)
  118. {
  119. return $this->buildSession($this->createCacheHandler($driver));
  120. }
  121. /**
  122. * Create the cache based session handler instance.
  123. *
  124. * @param string $driver
  125. * @return \Illuminate\Session\CacheBasedSessionHandler
  126. */
  127. protected function createCacheHandler($driver)
  128. {
  129. $store = $this->app['config']->get('session.store') ?: $driver;
  130. return new CacheBasedSessionHandler(
  131. clone $this->app['cache']->store($store),
  132. $this->app['config']['session.lifetime']
  133. );
  134. }
  135. /**
  136. * Build the session instance.
  137. *
  138. * @param \SessionHandlerInterface $handler
  139. * @return \Illuminate\Session\Store
  140. */
  141. protected function buildSession($handler)
  142. {
  143. if ($this->app['config']['session.encrypt']) {
  144. return $this->buildEncryptedSession($handler);
  145. }
  146. return new Store($this->app['config']['session.cookie'], $handler);
  147. }
  148. /**
  149. * Build the encrypted session instance.
  150. *
  151. * @param \SessionHandlerInterface $handler
  152. * @return \Illuminate\Session\EncryptedStore
  153. */
  154. protected function buildEncryptedSession($handler)
  155. {
  156. return new EncryptedStore(
  157. $this->app['config']['session.cookie'], $handler, $this->app['encrypter']
  158. );
  159. }
  160. /**
  161. * Get the session configuration.
  162. *
  163. * @return array
  164. */
  165. public function getSessionConfig()
  166. {
  167. return $this->app['config']['session'];
  168. }
  169. /**
  170. * Get the default session driver name.
  171. *
  172. * @return string
  173. */
  174. public function getDefaultDriver()
  175. {
  176. return $this->app['config']['session.driver'];
  177. }
  178. /**
  179. * Set the default session driver name.
  180. *
  181. * @param string $name
  182. * @return void
  183. */
  184. public function setDefaultDriver($name)
  185. {
  186. $this->app['config']['session.driver'] = $name;
  187. }
  188. }