SessionManager.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php namespace Illuminate\Session;
  2. use Illuminate\Support\Manager;
  3. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  4. use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
  5. use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
  6. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
  7. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
  8. class SessionManager extends Manager {
  9. /**
  10. * Call a custom driver creator.
  11. *
  12. * @param string $driver
  13. * @return mixed
  14. */
  15. protected function callCustomCreator($driver)
  16. {
  17. return $this->buildSession(parent::callCustomCreator($driver));
  18. }
  19. /**
  20. * Create an instance of the "array" session driver.
  21. *
  22. * @return \Illuminate\Session\Store
  23. */
  24. protected function createArrayDriver()
  25. {
  26. return new Store(new MockArraySessionStorage);
  27. }
  28. /**
  29. * Create an instance of the "cookie" session driver.
  30. *
  31. * @return \Illuminate\Session\Store
  32. */
  33. protected function createCookieDriver()
  34. {
  35. $lifetime = $this->app['config']['session.lifetime'];
  36. return $this->buildSession(new CookieSessionHandler($this->app['cookie'], $lifetime));
  37. }
  38. /**
  39. * Create an instance of the native session driver.
  40. *
  41. * @return \Illuminate\Session\Session
  42. */
  43. protected function createNativeDriver()
  44. {
  45. $path = $this->app['config']['session.files'];
  46. return $this->buildSession(new NativeFileSessionHandler($path));
  47. }
  48. /**
  49. * Create an instance of the database session driver.
  50. *
  51. * @return \Illuminate\Session\Store
  52. */
  53. protected function createDatabaseDriver()
  54. {
  55. $connection = $this->getDatabaseConnection();
  56. $table = $connection->getTablePrefix().$this->app['config']['session.table'];
  57. return $this->buildSession(new PdoSessionHandler($connection->getPdo(), $this->getDatabaseOptions($table)));
  58. }
  59. /**
  60. * Get the database connection for the database driver.
  61. *
  62. * @return \Illuminate\Database\Connection
  63. */
  64. protected function getDatabaseConnection()
  65. {
  66. $connection = $this->app['config']['session.connection'];
  67. return $this->app['db']->connection($connection);
  68. }
  69. /**
  70. * Get the database session options.
  71. *
  72. * @return array
  73. */
  74. protected function getDatabaseOptions($table)
  75. {
  76. return array('db_table' => $table, 'db_id_col' => 'id', 'db_data_col' => 'payload', 'db_time_col' => 'last_activity');
  77. }
  78. /**
  79. * Create an instance of the APC session driver.
  80. *
  81. * @return \Illuminate\Session\CacheDrivenStore
  82. */
  83. protected function createApcDriver()
  84. {
  85. return $this->createCacheBased('apc');
  86. }
  87. /**
  88. * Create an instance of the Memcached session driver.
  89. *
  90. * @return \Illuminate\Session\CacheDrivenStore
  91. */
  92. protected function createMemcachedDriver()
  93. {
  94. return $this->createCacheBased('memcached');
  95. }
  96. /**
  97. * Create an instance of the Wincache session driver.
  98. *
  99. * @return \Illuminate\Session\CacheDrivenStore
  100. */
  101. protected function createWincacheDriver()
  102. {
  103. return $this->createCacheBased('wincache');
  104. }
  105. /**
  106. * Create an instance of the Redis session driver.
  107. *
  108. * @return \Illuminate\Session\CacheDrivenStore
  109. */
  110. protected function createRedisDriver()
  111. {
  112. $handler = $this->createCacheHandler('redis');
  113. $handler->getCache()->getStore()->setConnection($this->app['config']['session.connection']);
  114. return $this->buildSession($handler);
  115. }
  116. /**
  117. * Create an instance of a cache driven driver.
  118. *
  119. * @return \Illuminate\Session\CacheDrivenStore
  120. */
  121. protected function createCacheBased($driver)
  122. {
  123. return $this->buildSession($this->createCacheHandler($driver));
  124. }
  125. /**
  126. * Create the cache based session handler instance.
  127. *
  128. * @param string $driver
  129. * @return \Illuminate\Session\CacheBasedSessionHandler
  130. */
  131. protected function createCacheHandler($driver)
  132. {
  133. $minutes = $this->app['config']['session.lifetime'];
  134. return new CacheBasedSessionHandler($this->app['cache']->driver($driver), $minutes);
  135. }
  136. /**
  137. * Build the session instance.
  138. *
  139. * @param \SessionHandlerInterface $handler
  140. * @return \Illuminate\Session\Store
  141. */
  142. protected function buildSession($handler)
  143. {
  144. return new Store(new NativeSessionStorage($this->getOptions(), $handler));
  145. }
  146. /**
  147. * Get the session options.
  148. *
  149. * @return array
  150. */
  151. protected function getOptions()
  152. {
  153. $config = $this->app['config']['session'];
  154. return array(
  155. 'cookie_domain' => $config['domain'], 'cookie_lifetime' => $config['lifetime'] * 60,
  156. 'cookie_path' => $config['path'], 'cookie_httponly' => '1', 'name' => $config['cookie'],
  157. 'gc_divisor' => $config['lottery'][1], 'gc_probability' => $config['lottery'][0],
  158. );
  159. }
  160. /**
  161. * Get the default session driver name.
  162. *
  163. * @return string
  164. */
  165. protected function getDefaultDriver()
  166. {
  167. return $this->app['config']['session.driver'];
  168. }
  169. }