| 1 | <?php |
||
| 10 | trait Session |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Flag to disable the session for API usage. |
||
| 14 | * |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | protected $stateless = false; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Make a session var name for. |
||
| 21 | * |
||
| 22 | * @param null $name |
||
| 23 | * |
||
| 24 | * @return mixed |
||
| 25 | */ |
||
| 26 | 1 | protected function makeSessionVarName($name = null) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Get a session var value. |
||
| 33 | * |
||
| 34 | * @param null $var |
||
| 35 | * |
||
| 36 | * @return mixed |
||
| 37 | */ |
||
| 38 | 1 | public function sessionGet($var = null, $default = null) |
|
| 39 | { |
||
| 40 | 1 | if ($this->stateless) { |
|
| 41 | return $default; |
||
| 42 | } |
||
| 43 | |||
| 44 | 1 | return $this->getRequest()->session()->get( |
|
| 45 | 1 | $this->makeSessionVarName($var), |
|
| 46 | $default |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Put a var value to the current session. |
||
| 52 | * |
||
| 53 | * @param $var |
||
| 54 | * @param $value |
||
| 55 | * |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | protected function sessionPut($var, $value) |
||
| 59 | { |
||
| 60 | if ($this->stateless) { |
||
| 61 | return $value; |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->getRequest()->session()->put( |
||
| 65 | $this->makeSessionVarName($var), |
||
| 66 | $value |
||
| 67 | ); |
||
| 68 | |||
| 69 | return $value; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Forget a session var. |
||
| 74 | * |
||
| 75 | * @param null $var |
||
| 76 | */ |
||
| 77 | protected function sessionForget($var = null) |
||
| 78 | { |
||
| 79 | if ($this->stateless) { |
||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->getRequest()->session()->forget( |
||
| 84 | $this->makeSessionVarName($var) |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param mixed $stateless |
||
| 90 | * |
||
| 91 | * @return Authenticator |
||
| 92 | */ |
||
| 93 | public function setStateless($stateless = true) |
||
| 94 | { |
||
| 95 | $this->stateless = $stateless; |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | abstract protected function config($string, $children = []); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return \Illuminate\Http\Request |
||
| 104 | */ |
||
| 105 | abstract public function getRequest(); |
||
| 106 | } |
||
| 107 |