Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 37f288b

Browse filesBrowse files
committed
Add a RdKafka caster to Var-Dumper
1 parent b350c80 commit 37f288b
Copy full SHA for 37f288b

File tree

7 files changed

+554
-0
lines changed
Filter options

7 files changed

+554
-0
lines changed

‎src/Symfony/Component/VarDumper/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* added `RdKafka` support
8+
49
4.4.0
510
-----
611

+179Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use RdKafka;
15+
use RdKafka\Conf;
16+
use RdKafka\Exception as RdKafkaException;
17+
use RdKafka\KafkaConsumer;
18+
use RdKafka\Message;
19+
use RdKafka\Metadata\Broker as BrokerMetadata;
20+
use RdKafka\Metadata\Partition as PartitionMetadata;
21+
use RdKafka\Metadata\Topic as TopicMetadata;
22+
use RdKafka\Topic;
23+
use RdKafka\TopicConf;
24+
use RdKafka\TopicPartition;
25+
use Symfony\Component\VarDumper\Cloner\Stub;
26+
27+
/**
28+
* Casts RdKafka related classes to array representation.
29+
*
30+
* @author Romain Neutron <imprec@gmail.com>
31+
*
32+
* @experimental
33+
*/
34+
class RdKafkaCaster
35+
{
36+
public static function castKafkaConsumer(KafkaConsumer $c, array $a, Stub $stub, $isNested)
37+
{
38+
$prefix = Caster::PREFIX_VIRTUAL;
39+
40+
$topics = $c->getSubscription();
41+
42+
try {
43+
$assignment = $c->getAssignment();
44+
} catch (RdKafkaException $e) {
45+
$assignment = [];
46+
}
47+
48+
$a += [
49+
$prefix.'subscription' => $topics,
50+
$prefix.'assignment' => self::prepareAssignments($assignment),
51+
];
52+
53+
$a += self::extractMetadata($c);
54+
55+
return $a;
56+
}
57+
58+
public static function castTopic(Topic $c, array $a, Stub $stub, $isNested)
59+
{
60+
$prefix = Caster::PREFIX_VIRTUAL;
61+
62+
$a += [
63+
$prefix.'name' => $c->getName(),
64+
];
65+
66+
return $a;
67+
}
68+
69+
public static function castTopicPartition(TopicPartition $c, array $a)
70+
{
71+
$prefix = Caster::PREFIX_VIRTUAL;
72+
73+
$a += [
74+
$prefix.'offset' => $c->getOffset(),
75+
$prefix.'partition' => $c->getPartition(),
76+
$prefix.'topic' => $c->getTopic(),
77+
];
78+
79+
return $a;
80+
}
81+
82+
public static function castMessage(Message $c, array $a, Stub $stub, $isNested)
83+
{
84+
$prefix = Caster::PREFIX_VIRTUAL;
85+
86+
$a += [
87+
$prefix.'errstr' => $c->errstr(),
88+
];
89+
90+
return $a;
91+
}
92+
93+
public static function castConf(Conf $c, array $a, Stub $stub, $isNested)
94+
{
95+
$prefix = Caster::PREFIX_VIRTUAL;
96+
97+
$conf = [];
98+
99+
foreach ($c->dump() as $key => $value) {
100+
$conf[$prefix.$key] = $value;
101+
}
102+
103+
$a += $conf;
104+
105+
return $a;
106+
}
107+
108+
public static function castTopicConf(TopicConf $c, array $a, Stub $stub, $isNested)
109+
{
110+
$prefix = Caster::PREFIX_VIRTUAL;
111+
112+
$conf = [];
113+
114+
foreach ($c->dump() as $key => $value) {
115+
$conf[$prefix.$key] = $value;
116+
}
117+
118+
$a += $conf;
119+
120+
return $a;
121+
}
122+
123+
public static function castRdKafka(\RdKafka $c, array $a, Stub $stub, $isNested)
124+
{
125+
$prefix = Caster::PREFIX_VIRTUAL;
126+
127+
$a += [
128+
$prefix.'out_q_len' => $c->getOutQLen(),
129+
];
130+
131+
$a += self::extractMetadata($c);
132+
133+
return $a;
134+
}
135+
136+
private static function prepareAssignments(iterable $partitions)
137+
{
138+
return array_map(function (TopicPartition $topicPartition) {
139+
return self::castTopicPartition($topicPartition, []);
140+
}, $partitions);
141+
}
142+
143+
private static function extractMetadata($c)
144+
{
145+
try {
146+
$m = $c->getMetadata(true, null, 500);
147+
} catch (RdKafkaException $e) {
148+
$m = null;
149+
}
150+
151+
if (!$m) {
152+
return [];
153+
}
154+
155+
return [
156+
'orig_broker_id' => $m->getOrigBrokerId(),
157+
'orig_broker_name' => $m->getOrigBrokerName(),
158+
'brokers' => array_map(function (BrokerMetadata $broker) {
159+
return [
160+
'id' => $broker->getId(),
161+
'host' => $broker->getHost(),
162+
'port' => $broker->getPort(),
163+
];
164+
}, iterator_to_array($m->getBrokers())),
165+
'topics' => array_map(function (TopicMetadata $topic) {
166+
return [
167+
'name' => $topic->getTopic(),
168+
'partitions' => array_map(function (PartitionMetadata $partition) {
169+
return [
170+
'id' => $partition->getId(),
171+
'err' => $partition->getErr(),
172+
'leader' => $partition->getLeader(),
173+
];
174+
}, iterator_to_array($topic->getPartitions())),
175+
];
176+
}, iterator_to_array($m->getTopics())),
177+
];
178+
}
179+
}

‎src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ abstract class AbstractCloner implements ClonerInterface
160160
':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
161161
':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'],
162162
':xml' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],
163+
164+
'RdKafka' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castRdKafka'],
165+
'RdKafka\Conf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castConf'],
166+
'RdKafka\KafkaConsumer' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castKafkaConsumer'],
167+
'RdKafka\Message' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castMessage'],
168+
'RdKafka\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopic'],
169+
'RdKafka\TopicPartition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicPartition'],
170+
'RdKafka\TopicConf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicConf'],
163171
];
164172

165173
protected $maxItems = 2500;
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ARG PHP_VERSION=7.3
2+
3+
FROM php:${PHP_VERSION}-cli
4+
5+
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
6+
7+
RUN apt-get update && apt-get install --no-install-recommends -y git unzip librdkafka-dev \
8+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
9+
10+
ARG PHP_RDKAFKA_VERSION
11+
ENV PHP_RDKAFKA_VERSION ${PHP_RDKAFKA_VERSION:-4.0.2}
12+
13+
RUN pecl install rdkafka-${PHP_RDKAFKA_VERSION} && \
14+
echo "extension=rdkafka.so" > /usr/local/etc/php/conf.d/ext-rdkafka.ini
15+
16+
WORKDIR /Component/VarDumper
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.DEFAULT_GOAL := help
2+
SHELL=/bin/bash
3+
4+
php_version ?= 7.3
5+
php_rdkafka_version ?= 4.0.2
6+
7+
ifeq ($(MAKE_USE_DOCKER),false)
8+
PHP=
9+
else
10+
PHP=docker-compose --project-directory . --project-name vardumper run --rm php
11+
endif
12+
13+
build-docker-image:
14+
@docker-compose build --build-arg PHP_VERSION=$(php_version) --build-arg PHP_RDKAFKA_VERSION=$(php_rdkafka_version)
15+
.PHONY: build-docker-image
16+
17+
test: build-docker-image ## build-docker-image ## Run the Symfony VarDumper testsuite
18+
$(eval args ?= )
19+
@$(PHP) php -v
20+
@$(PHP) php --ri rdkafka
21+
@$(PHP) ./phpunit $(args)
22+
.PHONY: test
23+
24+
help:
25+
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
26+
.PHONY: help

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.