NativeFileSessionHandler.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. * NativeFileSessionHandler.
  13. *
  14. * Native session handler using PHP's built in file storage.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class NativeFileSessionHandler extends NativeSessionHandler
  19. {
  20. /**
  21. * Constructor.
  22. *
  23. * @param string $savePath Path of directory to save session files
  24. * Default null will leave setting as defined by PHP.
  25. * '/path', 'N;/path', or 'N;octal-mode;/path
  26. *
  27. * @see http://php.net/session.configuration.php#ini.session.save-path for further details.
  28. *
  29. * @throws \InvalidArgumentException On invalid $savePath
  30. */
  31. public function __construct($savePath = null)
  32. {
  33. if (null === $savePath) {
  34. $savePath = ini_get('session.save_path');
  35. }
  36. $baseDir = $savePath;
  37. if ($count = substr_count($savePath, ';')) {
  38. if ($count > 2) {
  39. throw new \InvalidArgumentException(sprintf('Invalid argument $savePath \'%s\'', $savePath));
  40. }
  41. // characters after last ';' are the path
  42. $baseDir = ltrim(strrchr($savePath, ';'), ';');
  43. }
  44. if ($baseDir && !is_dir($baseDir) && !@mkdir($baseDir, 0777, true) && !is_dir($baseDir)) {
  45. throw new \RuntimeException(sprintf('Session Storage was not able to create directory "%s"', $baseDir));
  46. }
  47. ini_set('session.save_path', $savePath);
  48. ini_set('session.save_handler', 'files');
  49. }
  50. }