|
3 | 3 |
|
4 | 4 | trait BrowserSessionTrait
|
5 | 5 | {
|
6 |
| - private $browserSessionController; |
7 |
| - |
8 | 6 | /**
|
9 | 7 | * @return BrowserSessionController
|
| 8 | + * @throws \Exception |
10 | 9 | */
|
11 |
| - protected function getBrowserSessionController() |
| 10 | + private function tryGetBrowserSessionController() |
12 | 11 | {
|
13 |
| - return $this->browserSessionController; |
14 |
| - } |
| 12 | + $browserSessionController = null; |
| 13 | + if (method_exists($this, 'getContainer')) { |
| 14 | + $container = call_user_func([$this, 'getContainer']); |
| 15 | + if (isset($container['browserSessionController'])) { |
| 16 | + $browserSessionController = $container['browserSessionController']; |
| 17 | + } |
| 18 | + } elseif (method_exists($this, 'getBrowserSessionController')) { |
| 19 | + $browserSessionController = call_user_func([$this, 'getBrowserSessionController']); |
| 20 | + } |
15 | 21 |
|
16 |
| - /** |
17 |
| - * @param BrowserSessionController $browserSessionController |
18 |
| - * @return $this |
19 |
| - */ |
20 |
| - protected function setBrowserSessionController(BrowserSessionController $browserSessionController) |
21 |
| - { |
22 |
| - $this->browserSessionController = $browserSessionController; |
23 |
| - return $this; |
| 22 | + if ($browserSessionController instanceof BrowserSessionController) { |
| 23 | + return $browserSessionController; |
| 24 | + } |
| 25 | + |
| 26 | + throw new \Exception('You need to provide the trait a method to get the BrowserSessionController'); |
24 | 27 | }
|
25 | 28 |
|
26 | 29 | /**
|
27 | 30 | * @return null|BrowserSessionEntity
|
28 | 31 | */
|
29 | 32 | protected function getBrowserSession()
|
30 | 33 | {
|
31 |
| - if (!$session = $this->getBrowserSessionController()->getSession()) { |
32 |
| - $session = $this->getBrowserSessionController()->createSession(); |
| 34 | + $browserSessionController = $this->tryGetBrowserSessionController(); |
| 35 | + if (!$session = $browserSessionController->getSession()) { |
| 36 | + $session = $browserSessionController->createSession(); |
33 | 37 | } else {
|
34 |
| - $this->getBrowserSessionController()->flagSessionForUpdate($session); |
| 38 | + $browserSessionController->flagSessionForUpdate($session); |
35 | 39 | }
|
36 |
| - $this->getBrowserSessionController()->saveCookie($session); |
| 40 | + $browserSessionController->saveCookie($session); |
37 | 41 | return $session;
|
38 | 42 | }
|
39 | 43 |
|
40 | 44 | protected function updateBrowserSession()
|
41 | 45 | {
|
| 46 | + $browserSessionController = $this->tryGetBrowserSessionController(); |
42 | 47 | $session = $this->getBrowserSession();
|
43 |
| - $this->getBrowserSessionController()->updateSession($session); |
| 48 | + $browserSessionController->updateSession($session); |
44 | 49 | }
|
45 | 50 |
|
46 | 51 | protected function flagBrowserSessionForUpdate()
|
47 | 52 | {
|
| 53 | + $browserSessionController = $this->tryGetBrowserSessionController(); |
48 | 54 | $session = $this->getBrowserSession();
|
49 |
| - $this->getBrowserSessionController()->flagSessionForUpdate($session); |
| 55 | + $browserSessionController->flagSessionForUpdate($session); |
50 | 56 | }
|
51 | 57 | }
|
0 commit comments