From 0b0ba21f2a864352ea965e6a86c08d1b87967c9d Mon Sep 17 00:00:00 2001 From: Aleksei Gagarin Date: Thu, 16 Dec 2021 17:52:51 +0300 Subject: [PATCH] Rename MACROS to LISTENERS (#8) - MacrosRenderer moved to ListenersRenderer - All MACROS renamed to LISTENERS --- .../Renderer/ListenersRenderer.php | 85 +++++++++++++++++++ .../Renderer/MacrosRenderer.php | 81 +----------------- src/OutputSchemaRenderer.php | 8 +- .../Renderer/MacrosRendererTest.php | 46 +++++----- .../{MacrosTest.php => ListenersTest.php} | 52 ++++++------ 5 files changed, 142 insertions(+), 130 deletions(-) create mode 100644 src/ConsoleRenderer/Renderer/ListenersRenderer.php rename tests/Schema/Renderer/OutputSchemaRenderer/{MacrosTest.php => ListenersTest.php} (79%) diff --git a/src/ConsoleRenderer/Renderer/ListenersRenderer.php b/src/ConsoleRenderer/Renderer/ListenersRenderer.php new file mode 100644 index 0000000..62d8308 --- /dev/null +++ b/src/ConsoleRenderer/Renderer/ListenersRenderer.php @@ -0,0 +1,85 @@ +property = $property; + $this->title = $title; + } + + public function render(Formatter $formatter, array $schema, string $role): ?string + { + if (! isset($schema[$this->property])) { + return null; + } + + $listeners = (array)($schema[$this->property] ?? []); + + if ($listeners === []) { + return null; + } + + $rows = [ + \sprintf('%s:', $formatter->title($this->title)), + ]; + + foreach ($listeners as $definition) { + $data = (array)$definition; + + // Listener class + $class = \is_string($data[0] ?? null) + ? $formatter->typecast($data[0]) + : $formatter->error('undefined'); + $rows[] = \sprintf('%s%s', $formatter->title(' '), $class); + + // Listener arguments + $args = $data[1] ?? null; + if ($args === null) { + continue; + } + if (\is_array($args)) { + foreach ($args as $key => $value) { + $row = $formatter->title(' ') . ' - '; + if (\is_string($key)) { + $row .= $formatter->property($key) . ' : '; + } + $row .= $formatter->info($this->printValue($value, $formatter)); + $rows[] = $row; + } + } elseif (is_string($args)) { + $rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($args, $formatter)); + } else { + $rows[] = $formatter->typecast($this->printValue($data, $formatter)); + } + } + + return \implode($formatter::LINE_SEPARATOR, $rows); + } + + /** + * @param mixed $value + */ + private function printValue($value, Formatter $formatter): string + { + $data = \trim(\var_export($value, true), '\''); + $data = \array_map( + static fn (string $row): string => $formatter->title(' ') . $row, + \explode("\n", $data) + ); + + return \ltrim( + \implode("\n", $data) + ); + } +} diff --git a/src/ConsoleRenderer/Renderer/MacrosRenderer.php b/src/ConsoleRenderer/Renderer/MacrosRenderer.php index 7f18572..36f8d37 100644 --- a/src/ConsoleRenderer/Renderer/MacrosRenderer.php +++ b/src/ConsoleRenderer/Renderer/MacrosRenderer.php @@ -4,82 +4,9 @@ namespace Cycle\Schema\Renderer\ConsoleRenderer\Renderer; -use Cycle\Schema\Renderer\ConsoleRenderer\Formatter; -use Cycle\Schema\Renderer\ConsoleRenderer\Renderer; - -class MacrosRenderer implements Renderer +/** + * @deprecated use {@see ListenersRenderer} instead + */ +class MacrosRenderer extends ListenersRenderer { - private int $property; - private string $title; - - public function __construct(int $property, string $title) - { - $this->property = $property; - $this->title = $title; - } - - public function render(Formatter $formatter, array $schema, string $role): ?string - { - if (! isset($schema[$this->property])) { - return null; - } - - $macrosList = (array)($schema[$this->property] ?? []); - - if ($macrosList === []) { - return null; - } - - $rows = [ - \sprintf('%s:', $formatter->title($this->title)), - ]; - - foreach ($macrosList as $definition) { - $data = (array)$definition; - - // macros class - $class = \is_string($data[0] ?? null) - ? $formatter->typecast($data[0]) - : $formatter->error('undefined'); - $rows[] = \sprintf('%s%s', $formatter->title(' '), $class); - - // macros params - $params = $data[1] ?? null; - if ($params === null) { - continue; - } - if (\is_array($params)) { - foreach ($params as $key => $value) { - $row = $formatter->title(' ') . ' - '; - if (\is_string($key)) { - $row .= $formatter->property($key) . ' : '; - } - $row .= $formatter->info($this->printValue($value, $formatter)); - $rows[] = $row; - } - } elseif (is_string($params)) { - $rows[] = $formatter->title(' ') . ' - ' . $formatter->info($this->printValue($params, $formatter)); - } else { - $rows[] = $formatter->typecast($this->printValue($data, $formatter)); - } - } - - return \implode($formatter::LINE_SEPARATOR, $rows); - } - - /** - * @param mixed $value - */ - private function printValue($value, Formatter $formatter): string - { - $data = \trim(\var_export($value, true), '\''); - $data = \array_map( - static fn (string $row): string => $formatter->title(' ') . $row, - \explode("\n", $data) - ); - - return \ltrim( - \implode("\n", $data) - ); - } } diff --git a/src/OutputSchemaRenderer.php b/src/OutputSchemaRenderer.php index ad9eeba..2531c21 100644 --- a/src/OutputSchemaRenderer.php +++ b/src/OutputSchemaRenderer.php @@ -11,7 +11,7 @@ use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ColumnsRenderer; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\CustomPropertiesRenderer; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\KeysRenderer; -use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\MacrosRenderer; +use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\PropertyRenderer; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\RelationsRenderer; use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\TitleRenderer; @@ -75,13 +75,13 @@ public function __construct(int $format = self::FORMAT_CONSOLE_COLOR, ?Constants $this->addRenderer(new PropertyRenderer($constants['TYPECAST_HANDLER'], 'Typecast')); } - if (isset($constants['MACROS'])) { - $this->addRenderer(new MacrosRenderer($constants['MACROS'], 'Macros')); + if (isset($constants['LISTENERS'])) { + $this->addRenderer(new ListenersRenderer($constants['LISTENERS'], 'Listeners')); } $this->addRenderer( new RelationsRenderer(), - new CustomPropertiesRenderer(array_values($constants)), + new CustomPropertiesRenderer(\array_values($constants)), ); } diff --git a/tests/Schema/Renderer/ConsoleRenderer/Renderer/MacrosRendererTest.php b/tests/Schema/Renderer/ConsoleRenderer/Renderer/MacrosRendererTest.php index 949d6ee..f2f2d72 100644 --- a/tests/Schema/Renderer/ConsoleRenderer/Renderer/MacrosRendererTest.php +++ b/tests/Schema/Renderer/ConsoleRenderer/Renderer/MacrosRendererTest.php @@ -6,10 +6,10 @@ use Cycle\Schema\Renderer\ConsoleRenderer\Formatter; use Cycle\Schema\Renderer\ConsoleRenderer\Formatter\PlainFormatter; -use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\MacrosRenderer; +use Cycle\Schema\Renderer\ConsoleRenderer\Renderer\ListenersRenderer; use Cycle\Schema\Renderer\Tests\BaseTest; -final class MacrosRendererTest extends BaseTest +final class ListenersRendererTest extends BaseTest { private Formatter $formatter; @@ -22,7 +22,7 @@ protected function setUp(): void public function testRenderNotExistsProperty() { - $renderer = new MacrosRenderer(1, 'Foo'); + $renderer = new ListenersRenderer(1, 'Foo'); $result = $renderer->render($this->formatter, [], 'baz'); @@ -31,7 +31,7 @@ public function testRenderNotExistsProperty() public function testRenderStringProperty() { - $renderer = new MacrosRenderer(1, 'Foo'); + $renderer = new ListenersRenderer(1, 'Foo'); $result = $renderer->render($this->formatter, [ 1 => 'bar', @@ -49,18 +49,18 @@ public function testRenderStringProperty() public function testRenderArrayProperty() { - $renderer = new MacrosRenderer(1, 'Foo'); + $renderer = new ListenersRenderer(1, 'Foo'); $result = $renderer->render($this->formatter, [ - 1 => ['foo-macros', 'bar-macros'], + 1 => ['foo-listener', 'bar-listener'], ], 'baz'); $this->assertSame( <<<'OUTPUT' - Foo: - foo-macros - bar-macros -OUTPUT + Foo: + foo-listener + bar-listener + OUTPUT , $result ); @@ -68,27 +68,27 @@ public function testRenderArrayProperty() public function testRenderAssocArrayProperty() { - $renderer = new MacrosRenderer(1, 'Foo'); + $renderer = new ListenersRenderer(1, 'Foo'); $result = $renderer->render($this->formatter, [ 1 => [ - ['foo-macros', []], - ['bar-macros', ['baz' => 'bar', 'baz1', 'baz2']], - ['baz-macros', 'baz'], + ['foo-listener', []], + ['bar-listener', ['baz' => 'bar', 'baz1', 'baz2']], + ['baz-listener', 'baz'], ], ], 'baz'); $this->assertSame( <<<'OUTPUT' - Foo: - foo-macros - bar-macros - - baz : bar - - baz1 - - baz2 - baz-macros - - baz -OUTPUT + Foo: + foo-listener + bar-listener + - baz : bar + - baz1 + - baz2 + baz-listener + - baz + OUTPUT , $result ); diff --git a/tests/Schema/Renderer/OutputSchemaRenderer/MacrosTest.php b/tests/Schema/Renderer/OutputSchemaRenderer/ListenersTest.php similarity index 79% rename from tests/Schema/Renderer/OutputSchemaRenderer/MacrosTest.php rename to tests/Schema/Renderer/OutputSchemaRenderer/ListenersTest.php index b6c8676..708bd83 100644 --- a/tests/Schema/Renderer/OutputSchemaRenderer/MacrosTest.php +++ b/tests/Schema/Renderer/OutputSchemaRenderer/ListenersTest.php @@ -12,18 +12,18 @@ use Cycle\Schema\Renderer\Tests\BaseTest; use Cycle\Schema\Renderer\Tests\Fixture\User; -final class MacrosTest extends BaseTest +final class ListenersTest extends BaseTest { protected function setUp(): void { parent::setUp(); - if (! $this->constantSupported('MACROS')) { - $this->markTestSkipped('Only CycleORM v2 supports macros'); + if (! $this->constantSupported('LISTENERS')) { + $this->markTestSkipped('Only CycleORM v2 supports listener'); } } - public function testSchemaWithMacrosPropertyAsStringShouldBeRendered() + public function testSchemaWithListenerPropertyAsStringShouldBeRendered() { $schemaArray = [ User::class => [ @@ -35,7 +35,7 @@ public function testSchemaWithMacrosPropertyAsStringShouldBeRendered() SchemaInterface::PRIMARY_KEY => 'id', SchemaInterface::COLUMNS => ['id', 'email', 'balance'], SchemaInterface::TYPECAST => ['id' => 'int', 'balance' => 'float'], - SchemaInterface::MACROS => 'foo-macros', + SchemaInterface::LISTENERS => 'foo-listener', ], ]; @@ -54,8 +54,8 @@ public function testSchemaWithMacrosPropertyAsStringShouldBeRendered() 0 -> id -> int 1 -> email 2 -> balance -> float - Macros: - foo-macros + Listeners: + foo-listener Relations: not defined @@ -65,7 +65,7 @@ public function testSchemaWithMacrosPropertyAsStringShouldBeRendered() ); } - public function testSchemaWithMacrosPropertyAsArrayShouldBeRendered() + public function testSchemaWithListenerPropertyAsArrayShouldBeRendered() { $schemaArray = [ User::class => [ @@ -77,9 +77,9 @@ public function testSchemaWithMacrosPropertyAsArrayShouldBeRendered() SchemaInterface::PRIMARY_KEY => 'id', SchemaInterface::COLUMNS => ['id', 'email', 'balance'], SchemaInterface::TYPECAST => ['id' => 'int', 'balance' => 'float'], - SchemaInterface::MACROS => [ - 'foo-macros', - 'bar-macros', + SchemaInterface::LISTENERS => [ + 'foo-listener', + 'bar-listener', ], ], ]; @@ -99,9 +99,9 @@ public function testSchemaWithMacrosPropertyAsArrayShouldBeRendered() 0 -> id -> int 1 -> email 2 -> balance -> float - Macros: - foo-macros - bar-macros + Listeners: + foo-listener + bar-listener Relations: not defined @@ -111,7 +111,7 @@ public function testSchemaWithMacrosPropertyAsArrayShouldBeRendered() ); } - public function testSchemaWithMacrosPropertyAsAssocArrayShouldBeRendered() + public function testSchemaWithListenerPropertyAsAssocArrayShouldBeRendered() { $schemaArray = [ User::class => [ @@ -123,11 +123,11 @@ public function testSchemaWithMacrosPropertyAsAssocArrayShouldBeRendered() SchemaInterface::PRIMARY_KEY => 'id', SchemaInterface::COLUMNS => ['id', 'email', 'balance'], SchemaInterface::TYPECAST => ['id' => 'int', 'balance' => 'float'], - SchemaInterface::MACROS => [ - ['foo-macros', []], - ['bar-macros', ['baz' => 'bar', 'baz1', 'baz2']], - ['baz-macros', 'baz'], - ['baz-macros', true], + SchemaInterface::LISTENERS => [ + ['foo-listener', []], + ['bar-listener', ['baz' => 'bar', 'baz1', 'baz2']], + ['baz-listener', 'baz'], + ['baz-listener', true], ], ], ]; @@ -147,17 +147,17 @@ public function testSchemaWithMacrosPropertyAsAssocArrayShouldBeRendered() 0 -> id -> int 1 -> email 2 -> balance -> float - Macros: - foo-macros - bar-macros + Listeners: + foo-listener + bar-listener - baz : bar - baz1 - baz2 - baz-macros + baz-listener - baz - baz-macros + baz-listener array ( - 0 => 'baz-macros', + 0 => 'baz-listener', 1 => true, ) Relations: not defined