SessionHelper.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Bundle\FrameworkBundle\Templating\Helper;
  11. use Symfony\Component\Templating\Helper\Helper;
  12. use Symfony\Component\HttpFoundation\Request;
  13. /**
  14. * SessionHelper provides read-only access to the session attributes.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.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 getFlashes()
  56. {
  57. return $this->session->getFlashes();
  58. }
  59. public function hasFlash($name)
  60. {
  61. return $this->session->hasFlash($name);
  62. }
  63. /**
  64. * Returns the canonical name of this helper.
  65. *
  66. * @return string The canonical name
  67. */
  68. public function getName()
  69. {
  70. return 'session';
  71. }
  72. }