session.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\DependencyInjection\Loader\Configurator;
  11. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  12. use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
  17. use Symfony\Component\HttpFoundation\Session\Storage\Handler\IdentityMarshaller;
  18. use Symfony\Component\HttpFoundation\Session\Storage\Handler\MarshallingSessionHandler;
  19. use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
  20. use Symfony\Component\HttpFoundation\Session\Storage\Handler\SessionHandlerFactory;
  21. use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
  22. use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
  23. use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
  24. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  25. use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
  26. use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
  27. use Symfony\Component\HttpKernel\EventListener\SessionListener;
  28. return static function (ContainerConfigurator $container) {
  29. $container->parameters()->set('session.metadata.storage_key', '_sf2_meta');
  30. $container->services()
  31. ->set('session', Session::class)
  32. ->public()
  33. ->args([
  34. service('session.storage'),
  35. null, // AttributeBagInterface
  36. null, // FlashBagInterface
  37. [service('session_listener'), 'onSessionUsage'],
  38. ])
  39. ->alias(SessionInterface::class, 'session')
  40. ->alias(SessionStorageInterface::class, 'session.storage')
  41. ->alias(\SessionHandlerInterface::class, 'session.handler')
  42. ->set('session.storage.metadata_bag', MetadataBag::class)
  43. ->args([
  44. param('session.metadata.storage_key'),
  45. param('session.metadata.update_threshold'),
  46. ])
  47. ->set('session.storage.native', NativeSessionStorage::class)
  48. ->args([
  49. param('session.storage.options'),
  50. service('session.handler'),
  51. service('session.storage.metadata_bag'),
  52. ])
  53. ->set('session.storage.php_bridge', PhpBridgeSessionStorage::class)
  54. ->args([
  55. service('session.handler'),
  56. service('session.storage.metadata_bag'),
  57. ])
  58. ->set('session.flash_bag', FlashBag::class)
  59. ->factory([service('session'), 'getFlashBag'])
  60. ->deprecate('symfony/framework-bundle', '5.1', 'The "%service_id%" service is deprecated, use "$session->getFlashBag()" instead.')
  61. ->alias(FlashBagInterface::class, 'session.flash_bag')
  62. ->set('session.attribute_bag', AttributeBag::class)
  63. ->factory([service('session'), 'getBag'])
  64. ->args(['attributes'])
  65. ->deprecate('symfony/framework-bundle', '5.1', 'The "%service_id%" service is deprecated, use "$session->getAttributeBag()" instead.')
  66. ->set('session.storage.mock_file', MockFileSessionStorage::class)
  67. ->args([
  68. param('kernel.cache_dir').'/sessions',
  69. 'MOCKSESSID',
  70. service('session.storage.metadata_bag'),
  71. ])
  72. ->set('session.handler.native_file', StrictSessionHandler::class)
  73. ->args([
  74. inline_service(NativeFileSessionHandler::class)
  75. ->args([param('session.save_path')]),
  76. ])
  77. ->set('session.abstract_handler', AbstractSessionHandler::class)
  78. ->factory([SessionHandlerFactory::class, 'createHandler'])
  79. ->args([abstract_arg('A string or a connection object')])
  80. ->set('session_listener', SessionListener::class)
  81. ->args([
  82. service_locator([
  83. 'session' => service('session')->ignoreOnInvalid(),
  84. 'initialized_session' => service('session')->ignoreOnUninitialized(),
  85. 'logger' => service('logger')->ignoreOnInvalid(),
  86. 'session_collector' => service('data_collector.request.session_collector')->ignoreOnInvalid(),
  87. ]),
  88. param('kernel.debug'),
  89. ])
  90. ->tag('kernel.event_subscriber')
  91. // for BC
  92. ->alias('session.storage.filesystem', 'session.storage.mock_file')
  93. ->set('session.marshaller', IdentityMarshaller::class)
  94. ->set('session.marshalling_handler', MarshallingSessionHandler::class)
  95. ->decorate('session.handler')
  96. ->args([
  97. service('session.marshalling_handler.inner'),
  98. service('session.marshaller'),
  99. ])
  100. ;
  101. };