SessionHelper.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
  3. use Symfony\Component\Templating\Helper\Helper;
  4. use Symfony\Component\HttpFoundation\Request;
  5. /*
  6. * This file is part of the Symfony framework.
  7. *
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * This source file is subject to the MIT license that is bundled
  11. * with this source code in the file LICENSE.
  12. */
  13. /**
  14. * SessionHelper provides read-only access to the session attributes.
  15. *
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class SessionHelper extends Helper
  19. {
  20. protected $session;
  21. /**
  22. * Constructor.
  23. *
  24. * @param Request $request A Request instance
  25. */
  26. public function __construct(Request $request)
  27. {
  28. $this->session = $request->getSession();
  29. }
  30. /**
  31. * Returns an attribute
  32. *
  33. * @param string $name The attribute name
  34. * @param mixed $default The default value
  35. *
  36. * @return mixed
  37. */
  38. public function get($name, $default = null)
  39. {
  40. return $this->session->get($name, $default);
  41. }
  42. /**
  43. * Returns the locale
  44. *
  45. * @return string
  46. */
  47. public function getLocale()
  48. {
  49. return $this->session->getLocale();
  50. }
  51. public function getFlash($name, $default = null)
  52. {
  53. return $this->session->getFlash($name, $default);
  54. }
  55. public function hasFlash($name)
  56. {
  57. return $this->session->hasFlash($name);
  58. }
  59. /**
  60. * Returns the canonical name of this helper.
  61. *
  62. * @return string The canonical name
  63. */
  64. public function getName()
  65. {
  66. return 'session';
  67. }
  68. }