1 | <?php |
||
29 | class SessionBuilder |
||
30 | { |
||
31 | protected $configuration; |
||
32 | protected $converter_holder; |
||
33 | |||
34 | /** |
||
35 | * __construct |
||
36 | * |
||
37 | * Instantiate builder. |
||
38 | * |
||
39 | * Mandatory configuration options are: |
||
40 | * dsn: connection parameters |
||
41 | * name: database logical name |
||
42 | * |
||
43 | * @param array $configuration |
||
44 | * @param ConverterHolder $converter_holder |
||
45 | */ |
||
46 | public function __construct(array $configuration, ConverterHolder $converter_holder = null) |
||
62 | |||
63 | /** |
||
64 | * addParameter |
||
65 | * |
||
66 | * Add a configuration parameter. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * @param mixed $value |
||
70 | * @return SessionBuilder $this |
||
71 | */ |
||
72 | public function addParameter($name, $value) |
||
78 | |||
79 | /** |
||
80 | * getConverterHolder |
||
81 | * |
||
82 | * Return the converter holder. |
||
83 | * |
||
84 | * @return ConverterHolder |
||
85 | */ |
||
86 | public function getConverterHolder() |
||
90 | |||
91 | /** |
||
92 | * buildSession |
||
93 | * |
||
94 | * Build a new session. |
||
95 | * |
||
96 | * @final |
||
97 | * @param string $stamp |
||
98 | * @return Session |
||
99 | */ |
||
100 | final public function buildSession($stamp = null) |
||
101 | { |
||
102 | $this->preConfigure(); |
||
103 | $dsn = $this |
||
104 | ->configuration->mustHave('dsn')->getParameter('dsn'); |
||
105 | $connection_configuration = |
||
106 | $this->configuration |
||
107 | ->mustHave('connection:configuration') |
||
108 | ->getParameter('connection:configuration') |
||
109 | ; |
||
110 | $persist = |
||
111 | $this->configuration |
||
112 | ->mustHave('connection:persist') |
||
113 | ->getParameter('connection:persist') |
||
114 | ; |
||
115 | $session = $this->createSession( |
||
116 | $this->createConnection($dsn, $persist, $connection_configuration), |
||
117 | $this->createClientHolder(), |
||
118 | $stamp |
||
119 | ); |
||
120 | $this->postConfigure($session); |
||
121 | |||
122 | return $session; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * getDefaultConfiguration |
||
127 | * |
||
128 | * This must return the default configuration for new sessions. Default |
||
129 | * parameters are overrided by the configuration passed as parameter to |
||
130 | * this builder. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | protected function getDefaultConfiguration() |
||
135 | { |
||
136 | return |
||
137 | [ |
||
138 | "connection:configuration" => |
||
139 | [ |
||
140 | 'bytea_output' => 'hex', |
||
141 | 'intervalstyle' => 'ISO_8601', |
||
142 | 'datestyle' => 'ISO', |
||
143 | 'standard_conforming_strings' => 'true', |
||
144 | 'timezone' => date_default_timezone_get(), |
||
145 | ], |
||
146 | 'connection:persist' => false, |
||
147 | ]; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * preConfigure |
||
152 | * |
||
153 | * If any computation to the configuration must be done before each session |
||
154 | * creation, it goes here. |
||
155 | * |
||
156 | * @return SessionBuilder $this |
||
157 | */ |
||
158 | protected function preConfigure() |
||
159 | { |
||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * createConnection |
||
165 | * |
||
166 | * Connection instantiation. |
||
167 | * |
||
168 | * @param string $dsn |
||
169 | * @param bool $persist |
||
170 | * @param string|array $connection_configuration |
||
171 | * @return Connection |
||
172 | */ |
||
173 | protected function createConnection($dsn, $persist, $connection_configuration) |
||
174 | { |
||
175 | return new Connection($dsn, $persist, $connection_configuration); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * createSession |
||
180 | * |
||
181 | * Session instantiation. |
||
182 | * |
||
183 | * @param Connection $connection |
||
184 | * @param ClientHolder $client_holder |
||
185 | * @param string|null $stamp |
||
186 | * @return Session |
||
187 | */ |
||
188 | protected function createSession(Connection $connection, ClientHolder $client_holder, $stamp) |
||
194 | |||
195 | /** |
||
196 | * createClientHolder |
||
197 | * |
||
198 | * Instantiate ClientHolder. |
||
199 | * |
||
200 | * @return ClientHolder |
||
201 | */ |
||
202 | protected function createClientHolder() |
||
206 | |||
207 | /** |
||
208 | * postConfigure |
||
209 | * |
||
210 | * Session configuration once created. |
||
211 | * All pooler registration stuff goes here. |
||
212 | * |
||
213 | * @param Session $session |
||
214 | * @return SessionBuilder $this |
||
215 | */ |
||
216 | protected function postConfigure(Session $session) |
||
220 | |||
221 | /** |
||
222 | * initializeConverterHolder |
||
223 | * |
||
224 | * Converter initialization at startup. |
||
225 | * If new converters are to be registered, it goes here. |
||
226 | * |
||
227 | * @param ConverterHolder $converter_holder |
||
228 | * @return SessionBuilder $this |
||
229 | */ |
||
230 | protected function initializeConverterHolder(ConverterHolder $converter_holder) |
||
234 | } |
||
235 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.