1+ <?php
2+ /**
3+ * This file is part of webman.
4+ *
5+ * Licensed under The MIT License
6+ * For full copyright and license information, please see the MIT-LICENSE.txt
7+ * Redistributions of files must retain the above copyright notice.
8+ *
9+ * @author walkor<walkor@workerman.net>
10+ * @copyright walkor<walkor@workerman.net>
11+ * @link http://www.workerman.net/
12+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
13+ */
14+
15+ namespace Webman \Stomp \Process ;
16+
17+ use support \bootstrap \Container ;
18+ use Workerman \Stomp \Client as StompClient ;
19+ use Webman \Stomp \Client ;
20+
21+ /**
22+ * Class StompConsumer
23+ * @package process
24+ */
25+ class Consumer
26+ {
27+ /**
28+ * @var string
29+ */
30+ protected $ _consumerDir = '' ;
31+
32+ /**
33+ * StompConsumer constructor.
34+ * @param string $consumer_dir
35+ */
36+ public function __construct ($ consumer_dir = '' )
37+ {
38+ $ this ->_consumerDir = $ consumer_dir ;
39+ }
40+
41+ /**
42+ * onWorkerStart.
43+ */
44+ public function onWorkerStart ()
45+ {
46+ $ dir_iterator = new \RecursiveDirectoryIterator ($ this ->_consumerDir );
47+ $ iterator = new \RecursiveIteratorIterator ($ dir_iterator );
48+ foreach ($ iterator as $ file ) {
49+ if (is_dir ($ file )) {
50+ continue ;
51+ }
52+ $ fileinfo = new \SplFileInfo ($ file );
53+ $ ext = $ fileinfo ->getExtension ();
54+ if ($ ext === 'php ' ) {
55+ $ class = str_replace ('/ ' , "\\" , substr (substr ($ file , strlen (base_path ())), 0 , -4 ));
56+ if (!is_a ($ class , 'Webman\Stomp\Consumer ' , true )) {
57+ continue ;
58+ }
59+ $ consumer = Container::get ($ class );
60+ $ connection_name = $ consumer ->connection ?? 'default ' ;
61+ $ queue = $ consumer ->queue ;
62+ $ ack = $ consumer ->ack ?? 'auto ' ;
63+ $ connection = Client::connection ($ connection_name );
64+ $ cb = function ($ client , $ package , $ ack ) use ($ consumer ) {
65+ \call_user_func ([$ consumer , 'consume ' ], $ package ['body ' ], $ ack , $ client );
66+ };
67+ $ connection ->subscribe ($ queue , $ cb , ['ack ' => $ ack ]);
68+ /*if ($connection->getState() == StompClient::STATE_ESTABLISHED) {
69+ $connection->subscribe($queue, $cb, ['ack' => $ack]);
70+ } else {
71+ $connection->onConnect = function (Client $connection) use ($queue, $ack, $cb) {
72+ $connection->subscribe($queue, $cb, ['ack' => $ack]);
73+ };
74+ }*/
75+ }
76+ }
77+
78+ }
79+ }
0 commit comments