|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Pomm's Foundation package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
|
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 PommProject\Foundation\Client; |
|
11
|
|
|
|
|
12
|
|
|
use PommProject\Foundation\Exception\FoundationException; |
|
13
|
|
|
use PommProject\Foundation\Session\Session; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* ClientPoolerTrait |
|
17
|
|
|
* |
|
18
|
|
|
* Trait for client poolers. ClientPooler instances are factories for |
|
19
|
|
|
* Session's Client. The workflow of the ClientPooler is the following: |
|
20
|
|
|
* When a client is called |
|
21
|
|
|
* 1 It is fetched from the pool |
|
22
|
|
|
* 2.0 If no client is found: |
|
23
|
|
|
* 2.1 The createClient() method is triggered. |
|
24
|
|
|
* 2.2 The client is registered to the session |
|
25
|
|
|
* 3 The client is returned back |
|
26
|
|
|
* |
|
27
|
|
|
* @package Foundation |
|
28
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
|
29
|
|
|
* @author Grégoire HUBERT |
|
30
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
|
31
|
|
|
* @see ClientPoolerInterface |
|
32
|
|
|
* @abstract |
|
33
|
|
|
*/ |
|
34
|
|
|
trait ClientPoolerTrait |
|
35
|
|
|
{ |
|
36
|
|
|
private $session; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* getPoolerType |
|
40
|
|
|
* |
|
41
|
|
|
* @see ClientPoolerInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract public function getPoolerType(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* createClient |
|
47
|
|
|
* |
|
48
|
|
|
* Create a new client. |
|
49
|
|
|
* |
|
50
|
|
|
* @abstract |
|
51
|
|
|
* @access protected |
|
52
|
|
|
* @param string $identifier |
|
53
|
|
|
* @return Client |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract protected function createClient($identifier); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* register |
|
59
|
|
|
* |
|
60
|
|
|
* @see ClientPoolerInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function register(Session $session) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->session = $session; |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* getClient |
|
71
|
|
|
* |
|
72
|
|
|
* Basic getClient method. |
|
73
|
|
|
* |
|
74
|
|
|
* @access public |
|
75
|
|
|
* @param string $identifier |
|
76
|
|
|
* @return Client |
|
77
|
|
|
* @see ClientInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getClient($identifier) |
|
80
|
|
|
{ |
|
81
|
|
|
$client = $this->getClientFromPool($identifier); |
|
82
|
|
|
|
|
83
|
|
|
if ($client === null) { |
|
84
|
|
|
$client = $this->createClient($identifier); |
|
85
|
|
|
$this->getSession()->registerClient($client); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $client; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* getClientFromPool |
|
93
|
|
|
* |
|
94
|
|
|
* How the pooler fetch a client from the pool. |
|
95
|
|
|
* |
|
96
|
|
|
* @access protected |
|
97
|
|
|
* @param string $identifier |
|
98
|
|
|
* @return Client|null |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getClientFromPool($identifier) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this |
|
103
|
|
|
->getSession() |
|
104
|
|
|
->getClient($this->getPoolerType(), $identifier) |
|
105
|
|
|
; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* getSession |
|
110
|
|
|
* |
|
111
|
|
|
* Check if the session is set and return it. |
|
112
|
|
|
* |
|
113
|
|
|
* @access protected |
|
114
|
|
|
* @return Session |
|
115
|
|
|
* @throws FoundationException |
|
116
|
|
|
*/ |
|
117
|
|
View Code Duplication |
protected function getSession() |
|
118
|
|
|
{ |
|
119
|
|
|
if ($this->session === null) { |
|
120
|
|
|
throw new FoundationException(sprintf("Client pooler '%s' is not initialized, session not set.", get_class($this))); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this->session; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|