Skip to content

Commit 0ce8bed

Browse files
committed
moves functions into static methods
1 parent e58705e commit 0ce8bed

File tree

9 files changed

+151
-128
lines changed

9 files changed

+151
-128
lines changed

PhpAmqpLib/Channel/AMQPChannel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpAmqpLib\Channel\AbstractChannel;
66
use PhpAmqpLib\Exception\AMQPChannelException;
7+
use PhpAmqpLib\Helper\MiscHelper;
78
use PhpAmqpLib\Helper\Protocol\FrameBuilder;
89

910
class AMQPChannel extends AbstractChannel
@@ -49,7 +50,7 @@ public function __construct($connection,
4950

5051
if($this->debug)
5152
{
52-
debug_msg("using channel_id: " . $channel_id);
53+
MiscHelper::debug_msg("using channel_id: " . $channel_id);
5354
}
5455

5556
$this->default_ticket = 0;
@@ -184,7 +185,7 @@ protected function open_ok($args)
184185
$this->is_open = true;
185186
if($this->debug)
186187
{
187-
debug_msg("Channel open");
188+
MiscHelper::debug_msg("Channel open");
188189
}
189190
}
190191

PhpAmqpLib/Channel/AbstractChannel.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpAmqpLib\Channel;
44

5+
use PhpAmqpLib\Helper\MiscHelper;
56
use PhpAmqpLib\Wire\AMQPReader;
67
use PhpAmqpLib\Message\AMQPMessage;
78

@@ -111,7 +112,7 @@ function next_frame()
111112
{
112113
if($this->debug)
113114
{
114-
debug_msg("waiting for a new frame");
115+
MiscHelper::debug_msg("waiting for a new frame");
115116
}
116117
if($this->frame_queue != NULL)
117118
return array_pop($this->frame_queue);
@@ -163,7 +164,7 @@ function wait_content()
163164
} catch (Exception $e) {
164165
if($this->debug)
165166
{
166-
debug_msg("Ignoring body decoding exception: " . $e->getMessage());
167+
MiscHelper::debug_msg("Ignoring body decoding exception: " . $e->getMessage());
167168
}
168169
}
169170
}
@@ -182,14 +183,14 @@ public function wait($allowed_methods=NULL)
182183
{
183184
if($this->debug)
184185
{
185-
debug_msg("waiting for " . implode(", ", $allowed_methods));
186+
MiscHelper::debug_msg("waiting for " . implode(", ", $allowed_methods));
186187
}
187188
}
188189
else
189190
{
190191
if($this->debug)
191192
{
192-
debug_msg("waiting for any method");
193+
MiscHelper::debug_msg("waiting for any method");
193194
}
194195
}
195196

@@ -198,7 +199,7 @@ public function wait($allowed_methods=NULL)
198199
{
199200
if($this->debug)
200201
{
201-
debug_msg("checking queue method " . $qk);
202+
MiscHelper::debug_msg("checking queue method " . $qk);
202203
}
203204

204205
$method_sig = $queued_method[0];
@@ -208,8 +209,8 @@ public function wait($allowed_methods=NULL)
208209

209210
if($this->debug)
210211
{
211-
debug_msg("Executing queued method: $method_sig: " .
212-
self::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
212+
MiscHelper::debug_msg("Executing queued method: $method_sig: " .
213+
self::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
213214
}
214215

215216
return $this->dispatch($queued_method[0],
@@ -237,7 +238,7 @@ public function wait($allowed_methods=NULL)
237238

238239
if($this->debug)
239240
{
240-
debug_msg("> $method_sig: " . self::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
241+
MiscHelper::debug_msg("> $method_sig: " . self::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
241242
}
242243

243244

@@ -256,7 +257,7 @@ public function wait($allowed_methods=NULL)
256257
// Wasn't what we were looking for? save it for later
257258
if($this->debug)
258259
{
259-
debug_msg("Queueing for later: $method_sig: " . self::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
260+
MiscHelper::debug_msg("Queueing for later: $method_sig: " . self::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
260261
}
261262
$this->method_queue[] = array($method_sig, $args, $content);
262263
}

PhpAmqpLib/Connection/AMQPConnection.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpAmqpLib\Channel\AMQPChannel;
77
use PhpAmqpLib\Connection\AMQPConnection;
88
use PhpAmqpLib\Exception\AMQPConnectionException;
9+
use PhpAmqpLib\Helper\MiscHelper;
910
use PhpAmqpLib\Wire\AMQPWriter;
1011
use PhpAmqpLib\Wire\AMQPReader;
1112

@@ -103,7 +104,7 @@ public function __construct($host, $port,
103104
// we were redirected, close the socket, loop and try again
104105
if($this->debug)
105106
{
106-
debug_msg("closing socket");
107+
MiscHelper::debug_msg("closing socket");
107108
}
108109

109110
@fclose($this->sock); $this->sock=NULL;
@@ -120,7 +121,7 @@ public function __destruct()
120121
{
121122
if($this->debug)
122123
{
123-
debug_msg("closing socket");
124+
MiscHelper::debug_msg("closing socket");
124125
}
125126

126127
@fclose($this->sock);
@@ -131,7 +132,7 @@ protected function write($data)
131132
{
132133
if($this->debug)
133134
{
134-
debug_msg("< [hex]:\n" . hexdump($data, $htmloutput = false, $uppercase = true, $return = true));
135+
MiscHelper::debug_msg("< [hex]:\n" . MiscHelper::hexdump($data, $htmloutput = false, $uppercase = true, $return = true));
135136
}
136137

137138
$len = strlen($data);
@@ -162,7 +163,7 @@ protected function do_close()
162163
{
163164
if($this->debug)
164165
{
165-
debug_msg("closing socket");
166+
MiscHelper::debug_msg("closing socket");
166167
}
167168

168169
@fclose($this->sock);
@@ -241,8 +242,8 @@ protected function send_channel_method_frame($channel, $method_sig, $args="")
241242

242243
if($this->debug)
243244
{
244-
debug_msg("< " . methodSig($method_sig) . ": " .
245-
AbstractChannel::$GLOBAL_METHOD_NAMES[methodSig($method_sig)]);
245+
MiscHelper::debug_msg("< " . MiscHelper::methodSig($method_sig) . ": " .
246+
AbstractChannel::$GLOBAL_METHOD_NAMES[MiscHelper::methodSig($method_sig)]);
246247
}
247248

248249
}
@@ -393,7 +394,7 @@ protected function open_ok($args)
393394
$this->known_hosts = $args->read_shortstr();
394395
if($this->debug)
395396
{
396-
debug_msg("Open OK! known_hosts: " . $this->known_hosts);
397+
MiscHelper::debug_msg("Open OK! known_hosts: " . $this->known_hosts);
397398
}
398399

399400
return NULL;
@@ -409,7 +410,7 @@ protected function redirect($args)
409410
$this->known_hosts = $args->read_shortstr();
410411
if($this->debug)
411412
{
412-
debug_msg("Redirected to [". $host . "], known_hosts [" . $this->known_hosts . "]" );
413+
MiscHelper::debug_msg("Redirected to [". $host . "], known_hosts [" . $this->known_hosts . "]" );
413414
}
414415
return $host;
415416
}
@@ -445,7 +446,7 @@ protected function start($args)
445446

446447
if($this->debug)
447448
{
448-
debug_msg(sprintf("Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s",
449+
MiscHelper::debug_msg(sprintf("Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s",
449450
$this->version_major,
450451
$this->version_minor,
451452
self::dump_table($this->server_properties),

PhpAmqpLib/Exception/AMQPException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//TODO refactor usage of static methods
66
use PhpAmqpLib\Channel\AbstractChannel;
7+
use PhpAmqpLib\Helper\MiscHelper;
78

89
class AMQPException extends \Exception
910
{
@@ -15,7 +16,7 @@ public function __construct($reply_code, $reply_text, $method_sig)
1516
$this->amqp_reply_text = $reply_text; // redundant, but kept for BC
1617
$this->amqp_method_sig = $method_sig;
1718

18-
$ms = methodSig($method_sig);
19+
$ms = MiscHelper::methodSig($method_sig);
1920
if(array_key_exists($ms, AbstractChannel::$GLOBAL_METHOD_NAMES))
2021
$mn = AbstractChannel::$GLOBAL_METHOD_NAMES[$ms];
2122
else

PhpAmqpLib/Helper/MiscHelper.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace PhpAmqpLib\Helper;
4+
5+
class MiscHelper
6+
{
7+
public static function debug_msg($s)
8+
{
9+
echo $s, "\n";
10+
}
11+
12+
public static function methodSig($a)
13+
{
14+
if(is_string($a))
15+
return $a;
16+
else
17+
return sprintf("%d,%d",$a[0] ,$a[1]);
18+
}
19+
20+
public static function saveBytes($bytes)
21+
{
22+
$fh = fopen('/tmp/bytes', 'wb');
23+
fwrite($fh, $bytes);
24+
fclose($fh);
25+
}
26+
27+
/**
28+
* View any string as a hexdump.
29+
*
30+
* This is most commonly used to view binary data from streams
31+
* or sockets while debugging, but can be used to view any string
32+
* with non-viewable characters.
33+
*
34+
* @version 1.3.2
35+
* @author Aidan Lister <[email protected]>
36+
* @author Peter Waller <[email protected]>
37+
* @link http://aidanlister.com/repos/v/function.hexdump.php
38+
* @param string $data The string to be dumped
39+
* @param bool $htmloutput Set to false for non-HTML output
40+
* @param bool $uppercase Set to true for uppercase hex
41+
* @param bool $return Set to true to return the dump
42+
*/
43+
public static function hexdump($data, $htmloutput = true, $uppercase = false, $return = false)
44+
{
45+
// Init
46+
$hexi = '';
47+
$ascii = '';
48+
$dump = ($htmloutput === true) ? '<pre>' : '';
49+
$offset = 0;
50+
$len = strlen($data);
51+
52+
// Upper or lower case hexidecimal
53+
$x = ($uppercase === false) ? 'x' : 'X';
54+
55+
// Iterate string
56+
for ($i = $j = 0; $i < $len; $i++)
57+
{
58+
// Convert to hexidecimal
59+
$hexi .= sprintf("%02$x ", ord($data[$i]));
60+
61+
// Replace non-viewable bytes with '.'
62+
if (ord($data[$i]) >= 32) {
63+
$ascii .= ($htmloutput === true) ?
64+
htmlentities($data[$i]) :
65+
$data[$i];
66+
} else {
67+
$ascii .= '.';
68+
}
69+
70+
// Add extra column spacing
71+
if ($j === 7) {
72+
$hexi .= ' ';
73+
$ascii .= ' ';
74+
}
75+
76+
// Add row
77+
if (++$j === 16 || $i === $len - 1) {
78+
// Join the hexi / ascii output
79+
$dump .= sprintf("%04$x %-49s %s", $offset, $hexi, $ascii);
80+
81+
// Reset vars
82+
$hexi = $ascii = '';
83+
$offset += 16;
84+
$j = 0;
85+
86+
// Add newline
87+
if ($i !== $len - 1) {
88+
$dump .= "\n";
89+
}
90+
}
91+
}
92+
93+
// Finish dump
94+
$dump .= $htmloutput === true ?
95+
'</pre>' :
96+
'';
97+
$dump .= "\n";
98+
99+
// Output method
100+
if ($return === false) {
101+
echo $dump;
102+
} else {
103+
return $dump;
104+
}
105+
}
106+
}

PhpAmqpLib/Helper/misc.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ Then to stop the consumer, send to it the `quit` message:
5656

5757
$ php amqp_publisher.php quit
5858

59+
## Loading Classes ##
60+
61+
The library uses the [Symfony ClassLoader component](https://github.com/symfony/ClassLoader) in order to use a standard way of class loading.
62+
63+
If you want to see how to use the component with this library you can take a look at the file `demo/autoload.php`:
64+
65+
<?php
66+
67+
require_once(__DIR__ . '/../vendor/symfony/Symfony/Component/ClassLoader/UniversalClassLoader.php');
68+
69+
use Symfony\Component\ClassLoader\UniversalClassLoader;
70+
71+
$loader = new UniversalClassLoader();
72+
$loader->registerNamespaces(array(
73+
'PhpAmqpLib' => __DIR__ . '/..',
74+
));
75+
76+
$loader->register();
77+
5978
# Debugging #
6079

6180
If you want to know what's going on at a protocol level then add the following constant to your code:

demo/autoload.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@
99
'PhpAmqpLib' => __DIR__ . '/..',
1010
));
1111

12-
$loader->register();
13-
14-
require_once(__DIR__ . '/../hexdump.inc');
15-
require_once(__DIR__ . '/../PhpAmqpLib/Helper/misc.php');
12+
$loader->register();

0 commit comments

Comments
 (0)