Unverified Commit d1daa7ec authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3108967 by znerol, andypost, alexpott: Introduce a container tag to register session bags

parent b978479e
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -2605,5 +2605,29 @@ function hook_validation_constraint_alter(array &$definitions) {
 * within contributed and custom code. Reserved attributes include:
 * - uid: The user ID for an authenticated user. The value of this attribute
 *   cannot be modified.
 *
 * @section sec_custom_session_bags Custom session bags
 * Modules can register custom session bags in order to provide type safe
 * interfaces on module specific session data. A session bag must implement
 * \Symfony\Component\HttpFoundation\Session\SessionBagInterface. Custom session
 * bags are registered using a service entry tagged with the session_bag service
 * tag. Custom session bags can be accessed through the session retrieved from
 * the request object.
 *
 * Example service definition:
 * @code
 * session_test.session_bag:
 *   class: Drupal\session_test\Session\TestSessionBag
 *   tags:
 *     - { name: session_bag }
 * @endcode
 *
 * Example of accessing a custom session bag:
 * @code
 * $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
 * $bag->setFlag();
 * @endcode
 * Session data must be deleted from custom session bags as soon as it is no
 * longer needed (see @ref sec_intro above).
 * @}
 */
+2 −0
Original line number Diff line number Diff line
@@ -1512,6 +1512,8 @@ services:
  session:
    class: Symfony\Component\HttpFoundation\Session\Session
    arguments: ['@session_manager', '@session.attribute_bag', '@session.flash_bag']
    tags:
      - { name: service_collector, tag: session_bag, call: registerBag }
  session.flash_bag:
    class: Symfony\Component\HttpFoundation\Session\Flash\FlashBag
    public: false
+30 −0
Original line number Diff line number Diff line
@@ -131,3 +131,33 @@ session_test.set_session:
      test_value: '\s+'
  requirements:
    _permission: 'administer site configuration'

session_test.set_bag_flag:
  path: '/session-test/set-bag-flag'
  defaults:
    _title: 'Sets the test flag in the session test bag'
    _controller: '\Drupal\session_test\Controller\SessionTestController::setSessionBagFlag'
  options:
    no_cache: TRUE
  requirements:
    _access: 'TRUE'

session_test.clear_bag_flag:
  path: '/session-test/clear-bag-flag'
  defaults:
    _title: 'Clears the test flag in the session test bag'
    _controller: '\Drupal\session_test\Controller\SessionTestController::clearSessionBagFlag'
  options:
    no_cache: TRUE
  requirements:
    _access: 'TRUE'

session_test.has_bag_flag:
  path: '/session-test/has-bag-flag'
  defaults:
    _title: 'Prints a message if the flag in the session bag is set'
    _controller: '\Drupal\session_test\Controller\SessionTestController::hasSessionBagFlag'
  options:
    no_cache: TRUE
  requirements:
    _access: 'TRUE'
+4 −0
Original line number Diff line number Diff line
@@ -14,3 +14,7 @@ services:
      - { name: session_handler_proxy, priority: 20 }
  session_test.session_handler_proxy_trace:
    class: ArrayObject
  session_test.session_bag:
    class: Drupal\session_test\Session\TestSessionBag
    tags:
      - { name: session_bag }
+51 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\session_test\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\session_test\Session\TestSessionBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -195,4 +196,54 @@ public function setSession(Request $request, $test_value) {
    return new JsonResponse(['session' => $session->all(), 'user' => $this->currentUser()->id()]);
  }

  /**
   * Sets the test flag in the session test bag.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  public function setSessionBagFlag(Request $request) {
    /** @var \Drupal\session_test\Session\TestSessionBag */
    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
    $bag->setFlag();
    return new Response();
  }

  /**
   * Clears the test flag from the session test bag.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  public function clearSessionBagFlag(Request $request) {
    /** @var \Drupal\session_test\Session\TestSessionBag */
    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
    $bag->clearFlag();
    return new Response();
  }

  /**
   * Prints a message if the flag in the session bag is set.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  public function hasSessionBagFlag(Request $request) {
    /** @var \Drupal\session_test\Session\TestSessionBag */
    $bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
    return new Response(empty($bag->hasFlag())
      ? $this->t('Flag is absent from session bag')
      : $this->t('Flag is present in session bag')
    );
  }

}
Loading