菜谱项目

NativeFileSessionHandler.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  11. /**
  12. * Native session handler using PHP's built in file storage.
  13. *
  14. * @author Drak <drak@zikula.org>
  15. */
  16. class NativeFileSessionHandler extends NativeSessionHandler
  17. {
  18. /**
  19. * @param string $savePath Path of directory to save session files
  20. * Default null will leave setting as defined by PHP.
  21. * '/path', 'N;/path', or 'N;octal-mode;/path
  22. *
  23. * @see http://php.net/session.configuration.php#ini.session.save-path for further details.
  24. *
  25. * @throws \InvalidArgumentException On invalid $savePath
  26. */
  27. public function __construct($savePath = null)
  28. {
  29. if (null === $savePath) {
  30. $savePath = ini_get('session.save_path');
  31. }
  32. $baseDir = $savePath;
  33. if ($count = substr_count($savePath, ';')) {
  34. if ($count > 2) {
  35. throw new \InvalidArgumentException(sprintf('Invalid argument $savePath \'%s\'', $savePath));
  36. }
  37. // characters after last ';' are the path
  38. $baseDir = ltrim(strrchr($savePath, ';'), ';');
  39. }
  40. if ($baseDir && !is_dir($baseDir) && !@mkdir($baseDir, 0777, true) && !is_dir($baseDir)) {
  41. throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s"', $baseDir));
  42. }
  43. ini_set('session.save_path', $savePath);
  44. ini_set('session.save_handler', 'files');
  45. }
  46. }