1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ABM\Wasabi; |
4
|
|
|
|
5
|
|
|
use Analog\Analog; |
6
|
|
|
use Ratchet\ConnectionInterface; |
7
|
|
|
use Ratchet\MessageComponentInterface; |
8
|
|
|
|
9
|
|
|
class Prestashop implements MessageComponentInterface |
10
|
|
|
{ |
11
|
|
|
protected $clients; |
12
|
|
|
protected $dbConn; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
$this->clients = new \SplObjectStorage(); |
17
|
|
|
// Now connect to PS DB using Simplon on Composer |
18
|
|
|
$this->dbConn = new \Simplon\Mysql\Mysql( |
19
|
|
|
'127.0.0.1', |
20
|
|
|
_DB_USER_, |
21
|
|
|
_DB_PASSWD_, |
22
|
|
|
_DB_NAME_ |
23
|
|
|
); |
24
|
|
|
$log_file = 'wasabi.log'; |
25
|
|
|
Analog::handler(\Analog\Handler\File::init($log_file)); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function onOpen(ConnectionInterface $conn) |
29
|
|
|
{ |
30
|
|
|
// Store the new connection to send messages to later |
31
|
|
|
$this->clients->attach($conn); |
32
|
|
|
|
33
|
|
|
Analog::log("New connection: $conn->resourceId"); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function onMessage(ConnectionInterface $from, $msg) |
37
|
|
|
{ |
38
|
|
|
$result = null; |
39
|
|
|
foreach ($this->clients as $client) { |
40
|
|
|
if ($from == $client) { |
41
|
|
|
$type = strtolower(substr($msg, 0, strpos($msg, '|'))); |
42
|
|
|
$data = substr($msg, strpos($msg, '|') + 1); |
43
|
|
|
switch ($type) { |
44
|
|
|
case 'cart': $result = Cart::getCartData($data); |
|
|
|
|
45
|
|
|
break; |
46
|
|
|
case 'prod': $result = Product::getProductData($data); |
|
|
|
|
47
|
|
|
break; |
48
|
|
|
case 'comb': $result = Combination::getCombinationData($data); |
|
|
|
|
49
|
|
|
break; |
50
|
|
|
default: break; |
51
|
|
|
} |
52
|
|
|
if (!empty($result)) { |
53
|
|
|
$client->send(json_encode($result)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function onClose(ConnectionInterface $conn) |
61
|
|
|
{ |
62
|
|
|
// The connection is closed, remove it, as we can no longer send it messages |
63
|
|
|
$this->clients->detach($conn); |
64
|
|
|
Analog::log('Connection '.$conn->resourceId.' has disconnected'); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) |
68
|
|
|
{ |
69
|
|
|
echo "An error has occurred: {$e->getMessage()}\n"; |
70
|
|
|
Analog::log('Error: '.$e->getMessage().''); |
71
|
|
|
$conn->close(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: