Skip to content

Commit 0b0ba21

Browse files
authored
Rename MACROS to LISTENERS (#8)
- MacrosRenderer moved to ListenersRenderer - All MACROS renamed to LISTENERS
1 parent 2d14ab2 commit 0b0ba21

File tree

5 files changed

+142
-130
lines changed

5 files changed

+142
-130
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
6+
7+
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
8+
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
9+
10+
class ListenersRenderer implements Renderer
11+
{
12+
private int $property;
13+
private string $title;
14+
15+
public function __construct(int $property, string $title)
16+
{
17+
$this->property = $property;
18+
$this->title = $title;
19+
}
20+
21+
public function render(Formatter $formatter, array $schema, string $role): ?string
22+
{
23+
if (! isset($schema[$this->property])) {
24+
return null;
25+
}
26+
27+
$listeners = (array)($schema[$this->property] ?? []);
28+
29+
if ($listeners === []) {
30+
return null;
31+
}
32+
33+
$rows = [
34+
\sprintf('%s:', $formatter->title($this->title)),
35+
];
36+
37+
foreach ($listeners as $definition) {
38+
$data = (array)$definition;
39+
40+
// Listener class
41+
$class = \is_string($data[0] ?? null)
42+
? $formatter->typecast($data[0])
43+
: $formatter->error('undefined');
44+
$rows[] = \sprintf('%s%s', $formatter->title(' '), $class);
45+
46+
// Listener arguments
47+
$args = $data[1] ?? null;
48+
if ($args === null) {
49+
continue;
50+
}
51+
if (\is_array($args)) {
52+
foreach ($args as $key => $value) {
53+
$row = $formatter->title(' ') . ' - ';
54+
if (\is_string($key)) {
55+
$row .= $formatter->property($key) . ' : ';
56+
}
57+
$row .= $formatter->info($this->printValue($value, $formatter));
58+
$rows[] = $row;
59+
}
60+
} elseif (is_string($args)) {
61+
$rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($args, $formatter));
62+
} else {
63+
$rows[] = $formatter->typecast($this->printValue($data, $formatter));
64+
}
65+
}
66+
67+
return \implode($formatter::LINE_SEPARATOR, $rows);
68+
}
69+
70+
/**
71+
* @param mixed $value
72+
*/
73+
private function printValue($value, Formatter $formatter): string
74+
{
75+
$data = \trim(\var_export($value, true), '\'');
76+
$data = \array_map(
77+
static fn (string $row): string => $formatter->title(' ') . $row,
78+
\explode("\n", $data)
79+
);
80+
81+
return \ltrim(
82+
\implode("\n", $data)
83+
);
84+
}
85+
}

src/ConsoleRenderer/Renderer/MacrosRenderer.php

+4-77
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,9 @@
44

55
namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
66

7-
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
8-
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer;
9-
10-
class MacrosRenderer implements Renderer
7+
/**
8+
* @deprecated use {@see ListenersRenderer} instead
9+
*/
10+
class MacrosRenderer extends ListenersRenderer
1111
{
12-
private int $property;
13-
private string $title;
14-
15-
public function __construct(int $property, string $title)
16-
{
17-
$this->property = $property;
18-
$this->title = $title;
19-
}
20-
21-
public function render(Formatter $formatter, array $schema, string $role): ?string
22-
{
23-
if (! isset($schema[$this->property])) {
24-
return null;
25-
}
26-
27-
$macrosList = (array)($schema[$this->property] ?? []);
28-
29-
if ($macrosList === []) {
30-
return null;
31-
}
32-
33-
$rows = [
34-
\sprintf('%s:', $formatter->title($this->title)),
35-
];
36-
37-
foreach ($macrosList as $definition) {
38-
$data = (array)$definition;
39-
40-
// macros class
41-
$class = \is_string($data[0] ?? null)
42-
? $formatter->typecast($data[0])
43-
: $formatter->error('undefined');
44-
$rows[] = \sprintf('%s%s', $formatter->title(' '), $class);
45-
46-
// macros params
47-
$params = $data[1] ?? null;
48-
if ($params === null) {
49-
continue;
50-
}
51-
if (\is_array($params)) {
52-
foreach ($params as $key => $value) {
53-
$row = $formatter->title(' ') . ' - ';
54-
if (\is_string($key)) {
55-
$row .= $formatter->property($key) . ' : ';
56-
}
57-
$row .= $formatter->info($this->printValue($value, $formatter));
58-
$rows[] = $row;
59-
}
60-
} elseif (is_string($params)) {
61-
$rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($params, $formatter));
62-
} else {
63-
$rows[] = $formatter->typecast($this->printValue($data, $formatter));
64-
}
65-
}
66-
67-
return \implode($formatter::LINE_SEPARATOR, $rows);
68-
}
69-
70-
/**
71-
* @param mixed $value
72-
*/
73-
private function printValue($value, Formatter $formatter): string
74-
{
75-
$data = \trim(\var_export($value, true), '\'');
76-
$data = \array_map(
77-
static fn (string $row): string => $formatter->title(' ') . $row,
78-
\explode("\n", $data)
79-
);
80-
81-
return \ltrim(
82-
\implode("\n", $data)
83-
);
84-
}
8512
}

src/OutputSchemaRenderer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ColumnsRenderer;
1212
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\CustomPropertiesRenderer;
1313
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\KeysRenderer;
14-
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\MacrosRenderer;
14+
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer;
1515
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\PropertyRenderer;
1616
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\RelationsRenderer;
1717
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\TitleRenderer;
@@ -75,13 +75,13 @@ public function __construct(int $format = self::FORMAT_CONSOLE_COLOR, ?Constants
7575
$this->addRenderer(new PropertyRenderer($constants['TYPECAST_HANDLER'], 'Typecast'));
7676
}
7777

78-
if (isset($constants['MACROS'])) {
79-
$this->addRenderer(new MacrosRenderer($constants['MACROS'], 'Macros'));
78+
if (isset($constants['LISTENERS'])) {
79+
$this->addRenderer(new ListenersRenderer($constants['LISTENERS'], 'Listeners'));
8080
}
8181

8282
$this->addRenderer(
8383
new RelationsRenderer(),
84-
new CustomPropertiesRenderer(array_values($constants)),
84+
new CustomPropertiesRenderer(\array_values($constants)),
8585
);
8686
}
8787

tests/Schema/Renderer/ConsoleRenderer/Renderer/MacrosRendererTest.php

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter;
88
use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\PlainFormatter;
9-
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\MacrosRenderer;
9+
use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer;
1010
use Cycle\Schema\Renderer\Tests\BaseTest;
1111

12-
final class MacrosRendererTest extends BaseTest
12+
final class ListenersRendererTest extends BaseTest
1313
{
1414
private Formatter $formatter;
1515

@@ -22,7 +22,7 @@ protected function setUp(): void
2222

2323
public function testRenderNotExistsProperty()
2424
{
25-
$renderer = new MacrosRenderer(1, 'Foo');
25+
$renderer = new ListenersRenderer(1, 'Foo');
2626

2727
$result = $renderer->render($this->formatter, [], 'baz');
2828

@@ -31,7 +31,7 @@ public function testRenderNotExistsProperty()
3131

3232
public function testRenderStringProperty()
3333
{
34-
$renderer = new MacrosRenderer(1, 'Foo');
34+
$renderer = new ListenersRenderer(1, 'Foo');
3535

3636
$result = $renderer->render($this->formatter, [
3737
1 => 'bar',
@@ -49,46 +49,46 @@ public function testRenderStringProperty()
4949

5050
public function testRenderArrayProperty()
5151
{
52-
$renderer = new MacrosRenderer(1, 'Foo');
52+
$renderer = new ListenersRenderer(1, 'Foo');
5353

5454
$result = $renderer->render($this->formatter, [
55-
1 => ['foo-macros', 'bar-macros'],
55+
1 => ['foo-listener', 'bar-listener'],
5656
], 'baz');
5757

5858
$this->assertSame(
5959
<<<'OUTPUT'
60-
Foo:
61-
foo-macros
62-
bar-macros
63-
OUTPUT
60+
Foo:
61+
foo-listener
62+
bar-listener
63+
OUTPUT
6464
,
6565
$result
6666
);
6767
}
6868

6969
public function testRenderAssocArrayProperty()
7070
{
71-
$renderer = new MacrosRenderer(1, 'Foo');
71+
$renderer = new ListenersRenderer(1, 'Foo');
7272

7373
$result = $renderer->render($this->formatter, [
7474
1 => [
75-
['foo-macros', []],
76-
['bar-macros', ['baz' => 'bar', 'baz1', 'baz2']],
77-
['baz-macros', 'baz'],
75+
['foo-listener', []],
76+
['bar-listener', ['baz' => 'bar', 'baz1', 'baz2']],
77+
['baz-listener', 'baz'],
7878
],
7979
], 'baz');
8080

8181
$this->assertSame(
8282
<<<'OUTPUT'
83-
Foo:
84-
foo-macros
85-
bar-macros
86-
- baz : bar
87-
- baz1
88-
- baz2
89-
baz-macros
90-
- baz
91-
OUTPUT
83+
Foo:
84+
foo-listener
85+
bar-listener
86+
- baz : bar
87+
- baz1
88+
- baz2
89+
baz-listener
90+
- baz
91+
OUTPUT
9292
,
9393
$result
9494
);

0 commit comments

Comments
 (0)