@@ -81,84 +81,62 @@ class Dal implements Core\Parameter\Parameterizable, Core\Event\Source
8181 *
8282 * @var array
8383 */
84- private static $ _instance = [];
84+ private static $ _instance = [];
8585
8686 /**
8787 * Current singleton ID.
8888 *
8989 * @var string
9090 */
91- private static $ _id = null ;
91+ private static $ _id = null ;
9292
9393 /**
9494 * Current ID.
9595 *
9696 * @var string
9797 */
98- protected $ __id = null ;
98+ protected $ __id = null ;
9999
100100 /**
101101 * The layer instance.
102102 *
103103 * @var \Hoa\Database\IDal\Wrapper
104104 */
105- protected $ _layer = null ;
105+ protected $ _layer = null ;
106106
107107 /**
108108 * Parameter of \Hoa\Database\Dal.
109109 *
110110 * @var \Hoa\Core\Parameter
111111 */
112- protected static $ _parameters = null ;
112+ protected static $ _parameters = null ;
113+
114+ /**
115+ * The layer connection parameter.
116+ *
117+ * @var array
118+ */
119+ protected $ _connectionParameters = [];
113120
114121
115122
116123 /**
117124 * Create a DAL instance, representing a connection to a database.
118125 * The constructor is private to make a multiton.
119126 *
120- * @param string $dalName The database abstract layer name.
121- * @param string $dsn The DSN of database.
122- * @param string $username The username to connect to database.
123- * @param string $password The password to connect to database.
124- * @param array $driverOptions The driver options.
127+ * @param array $connectionParameters The layer connection parameter.
125128 * @return void
126- * @throws \Hoa\Database\Exception
127129 */
128- private function __construct (
129- $ dalName ,
130- $ dsn ,
131- $ username ,
132- $ password ,
133- Array $ driverOptions = []
134- ) {
135- // Please see https://bugs.php.net/55154.
136- if (0 !== preg_match ('#^sqlite:(.+)$#i ' , $ dsn , $ matches )) {
137- $ dsn = 'sqlite: ' . resolve ($ matches [1 ]);
138- }
130+ private function __construct (Array $ connectionParameters )
131+ {
132+ $ this ->_connectionParameters = $ connectionParameters ;
139133
140134 $ id = $ this ->__id = self ::$ _id ;
141135 $ event = 'hoa://Event/Database/ ' . $ id ;
142136
143137 Core \Event::register ($ event . ':opened ' , $ this );
144138 Core \Event::register ($ event . ':closed ' , $ this );
145139
146- $ this ->setDal (dnew (
147- '\Hoa\Database\Layer \\' . $ dalName ,
148- [$ dsn , $ username , $ password , $ driverOptions ]
149- ));
150-
151- Core \Event::notify (
152- $ event . ':opened ' ,
153- $ this ,
154- new Core \Event \Bucket ([
155- 'id ' => $ id ,
156- 'dsn ' => $ dsn ,
157- 'username ' => $ username ,
158- 'driverOptions ' => $ driverOptions
159- ])
160- );
161-
162140 return ;
163141 }
164142
@@ -244,13 +222,13 @@ public static function getInstance(
244222 $ driverOptions = @$ handle ['options ' ] ?: [];
245223 }
246224
247- return self ::$ _instance [$ id ] = new self (
225+ return self ::$ _instance [$ id ] = new self ([
248226 $ dalName ,
249227 $ dsn ,
250228 $ username ,
251229 $ password ,
252230 $ driverOptions
253- );
231+ ] );
254232 }
255233
256234 /**
@@ -297,6 +275,46 @@ public function getParameters()
297275 return self ::$ _parameters ;
298276 }
299277
278+ /**
279+ * Open a connection to the database.
280+ *
281+ * @return void
282+ */
283+ private function open ()
284+ {
285+ list (
286+ $ dalName ,
287+ $ dsn ,
288+ $ username ,
289+ $ password ,
290+ $ driverOptions
291+ ) = $ this ->_connectionParameters ;
292+
293+ // Please see https://bugs.php.net/55154.
294+ if (0 !== preg_match ('#^sqlite:(.+)$#i ' , $ dsn , $ matches )) {
295+ $ dsn = 'sqlite: ' . resolve ($ matches [1 ]);
296+ }
297+
298+ $ this ->setDal (dnew (
299+ '\Hoa\Database\Layer \\' . $ dalName ,
300+ [$ dsn , $ username , $ password , $ driverOptions ]
301+ ));
302+
303+ $ id = $ this ->getId ();
304+ Core \Event::notify (
305+ 'hoa://Event/Database/ ' . $ id . ':opened ' ,
306+ $ this ,
307+ new Core \Event \Bucket ([
308+ 'id ' => $ id ,
309+ 'dsn ' => $ dsn ,
310+ 'username ' => $ username ,
311+ 'driverOptions ' => $ driverOptions
312+ ])
313+ );
314+
315+ return ;
316+ }
317+
300318 /**
301319 * Close connection to the database.
302320 *
@@ -344,6 +362,10 @@ protected function setDal(IDal\Wrapper $dal)
344362 */
345363 protected function getDal ()
346364 {
365+ if (null === $ this ->_layer ) {
366+ $ this ->open ();
367+ }
368+
347369 return $ this ->_layer ;
348370 }
349371
0 commit comments