1111
1212namespace WebDriver ;
1313
14+ use Psr \EventDispatcher \EventDispatcherInterface ;
1415use WebDriver \Exception as WebDriverException ;
16+ use WebDriver \Service \CurlServiceInterface ;
1517
1618/**
1719 * Abstract WebDriver\AbstractWebDriver class
@@ -34,6 +36,13 @@ abstract class AbstractWebDriver
3436 */
3537 private $ curlService ;
3638
39+ /**
40+ * Event Dispatcher
41+ *
42+ * @var \Psr\EventDispatcher\EventDispatcherInterface
43+ */
44+ private $ eventDispatcher ;
45+
3746 /**
3847 * Transient options
3948 *
@@ -77,6 +86,7 @@ public function __construct($url)
7786 $ this ->transientOptions = [];
7887 $ this ->extensions = [];
7988 $ this ->curlService = ServiceFactory::getInstance ()->getService ('service.curl ' );
89+ $ this ->eventDispatcher = ServiceFactory::getInstance ()->getService ('event_dispatcher ' );
8090 }
8191
8292 /**
@@ -104,7 +114,7 @@ public function getURL()
104114 *
105115 * @param \WebDriver\Service\CurlServiceInterface $curlService
106116 */
107- public function setCurlService ($ curlService )
117+ public function setCurlService (CurlServiceInterface $ curlService )
108118 {
109119 $ this ->curlService = $ curlService ;
110120 }
@@ -119,6 +129,26 @@ public function getCurlService()
119129 return $ this ->curlService ;
120130 }
121131
132+ /**
133+ * Set event dispatcher
134+ *
135+ * @param \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher
136+ */
137+ public function setEventDispatcher (EventDispatcherInterface $ eventDispatcher )
138+ {
139+ $ this ->eventDispatcher = $ eventDispatcher ;
140+ }
141+
142+ /**
143+ * Get curl service
144+ *
145+ * @return \Psr\EventDispatcher\EventDispatcherInterface
146+ */
147+ public function getEventDispatcher ()
148+ {
149+ return $ this ->eventDispatcher ;
150+ }
151+
122152 /**
123153 * Set transient options
124154 *
@@ -204,7 +234,7 @@ public function __call($name, $arguments)
204234 }
205235
206236 $ parameters = array_shift ($ arguments );
207- $ result = $ this ->curl ($ requestMethod , ' / ' . $ webdriverCommand , $ parameters );
237+ $ result = $ this ->curl ($ requestMethod , $ webdriverCommand , $ parameters );
208238
209239 return $ result ['value ' ];
210240 }
@@ -225,6 +255,32 @@ public function __get($name)
225255 trigger_error ('Undefined property: ' . __CLASS__ . '::$ ' . $ name , E_USER_WARNING );
226256 }
227257
258+ /**
259+ * Event firing command wrapper
260+ */
261+ protected function curl ($ requestMethod , $ command , $ parameters = null , $ extraOptions = [])
262+ {
263+ $ name = strtoupper ($ requestMethod ) . ': '
264+ . substr ($ className = get_class ($ this ), strrpos ($ className , '\\' ) + 1 ) . ': '
265+ . ($ command ?: substr ($ this ->url , strrpos ($ this ->url , '/ ' ) + 1 ));
266+
267+ $ this ->eventDispatcher ->dispatch ((object ) ['event ' => 'before ' , 'command ' => $ name , 'parameters ' => $ parameters ]);
268+
269+ try {
270+ $ result = $ this ->curlExec ($ requestMethod , $ command , $ parameters , $ extraOptions );
271+ } catch (\Exception $ e ) {
272+ $ source = substr ($ className = get_class ($ e ), strrpos ($ className , '\\' ) + 1 );
273+
274+ $ this ->eventDispatcher ->dispatch ((object ) ['event ' => 'error ' , 'command ' => $ name , 'error ' => $ source ]);
275+
276+ throw $ e ;
277+ }
278+
279+ $ this ->eventDispatcher ->dispatch ((object ) ['event ' => 'after ' , 'command ' => $ name , 'result ' => $ result ['value ' ]]);
280+
281+ return $ result ;
282+ }
283+
228284 /**
229285 * Curl request to webdriver server.
230286 *
@@ -238,7 +294,7 @@ public function __get($name)
238294 *
239295 * @throws \WebDriver\Exception if error
240296 */
241- protected function curl ($ requestMethod , $ command , $ parameters = null , $ extraOptions = [] )
297+ protected function curlExec ($ requestMethod , $ command , $ parameters , $ extraOptions )
242298 {
243299 if ($ parameters && is_array ($ parameters ) && $ requestMethod !== 'POST ' ) {
244300 throw WebDriverException::factory (
@@ -252,7 +308,13 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
252308 );
253309 }
254310
255- $ url = $ this ->url . $ command ;
311+ $ url = $ this ->url ;
312+
313+ if ($ command && substr ($ this ->url , -1 ) !== '/ ' ) {
314+ $ url .= '/ ' ;
315+ }
316+
317+ $ url .= $ command ;
256318
257319 if (($ requestMethod === 'GET ' || $ requestMethod === 'DELETE ' )
258320 && $ parameters && (is_int ($ parameters ) || is_string ($ parameters ))
0 commit comments