Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public function __invoke(DeleteProjectionDocumentCommand $command): void
}

$this->repository->delete($document->getType(), $document->getId());
$this->dispatch(ProjectionDocumentDeletedEvent::class, [$document]);
$this->dispatch(ProjectionDocumentDeletedEvent::class, compact('document'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public function __invoke(SaveProjectionDocumentCommand $command): void
$document = new ProjectionDocument($command->type, $command->id, $command->body);

$this->repository->save($document);
$this->dispatch(ProjectionDocumentSavedEvent::class, [$document]);
$this->dispatch(ProjectionDocumentSavedEvent::class, compact('document'));
}
}
15 changes: 9 additions & 6 deletions src/Domain/Factory/ClassMethodResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @internal
*/
final class ClassMethodResolver
{
Expand All @@ -26,11 +28,11 @@ public static function resolve(string $class, string $method): array
throw InvalidClassException::createForMethod($class, $method);
}

if (null === $reflection) {
if (null === $reflection || !$reflection->getNumberOfParameters()) {
return self::$cache[$key] = [];
}

return self::$cache[$key] = array_map(function (\ReflectionParameter $param): array {
foreach ($reflection->getParameters() as $i => $param) {
$type = $param->getType();

if (null !== $type) {
Expand Down Expand Up @@ -61,13 +63,14 @@ public static function resolve(string $class, string $method): array
$required = true;
}

return [
'name' => $name = $param->getName(),
'key' => strtolower((string) preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $name)),
self::$cache[$key][$param->getName()] = [
'index' => $i,
'required' => $required,
'default' => $default,
'type' => $type,
];
}, $reflection->getParameters());
}

return self::$cache[$key];
}
}
29 changes: 11 additions & 18 deletions src/Domain/Factory/DomainObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,21 @@ private function resolveArguments(string $class, string $method, array $context)
{
$arguments = [];

foreach (ClassMethodResolver::resolve($class, $method) as $i => $argument) {
$given = true;
if (array_key_exists($name = $argument['name'], $context)) {
$value = $context[$name];
} elseif (array_key_exists($key = $argument['key'], $context)) {
$value = $context[$key];
} elseif (array_key_exists($i, $context)) {
$value = $context[$i];
} elseif (!$argument['required']) {
$value = $argument['default'];
foreach (ClassMethodResolver::resolve($class, $method) as $argument => $metadata) {
if (array_key_exists($argument, $context)) {
$given = true;
$value = $context[$argument];
} elseif (!$metadata['required']) {
$given = false;
$value = $metadata['default'];
} else {
throw new \LogicException(sprintf('No value available for argument $%s in class method "%s::%s()".', $name, $class, $method));
throw new \LogicException(sprintf('No value available for argument $%s in class method "%s::%s()".', $argument, $class, $method));
}

if ($given && isset($argument['type']) && (class_exists($argument['type']) || interface_exists($argument['type'], false)) && !\is_object($value)) {
try {
$arguments[] = ($this->factory ?? $this)->create($argument['type'], (array) $value);

continue;
} catch (InvalidClassException $e) {
}
$type = $metadata['type'];
if ($given && null !== $type && !\is_object($value) && (class_exists($type) || interface_exists($type, false))) {
$arguments[] = ($this->factory ?? $this)->create($metadata['type'], (array) $value);
continue;
}

$arguments[] = $value;
Expand Down
135 changes: 79 additions & 56 deletions src/Domain/Infra/Console/Context/ClassContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ final class ClassContextFactory implements ContextFactoryInterface
private $fieldMapping = [];
private $generatedValues = [];

public static function getFieldName(string $argument, bool $isOption = true): string
{
$field = strtolower((string) preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $argument));

return $isOption ? str_replace('_', '-', $field) : $field;
}

public static function getUniqueFieldName(InputDefinition $definition, string $field, bool $isOption = true): string
{
$known = $isOption ? $definition->getOptions() : $definition->getArguments();
Expand Down Expand Up @@ -62,33 +69,35 @@ public function configure(InputDefinition $definition): void
$origOptions = $origArgs = [];
}

foreach ($this->resolve() as $argument) {
foreach ($this->resolve() as $argument => $metadata) {
$isOption = true;
if ('bool' === $argument['type']) {
if ('bool' === $metadata['type']) {
$mode = InputOption::VALUE_NONE;
} elseif (self::isComplex($argument['type'])) {
} elseif (self::isComplex($metadata['type'])) {
$mode = InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY;
} elseif (!$argument['required'] || ($this->flags & self::ALWAYS_OPTIONAL)) {
} elseif (!$metadata['required'] || ($this->flags & self::ALWAYS_OPTIONAL)) {
$mode = InputOption::VALUE_OPTIONAL;
} else {
$mode = InputArgument::OPTIONAL;
$isOption = false;
}

$field = $isOption ? str_replace('_', '-', $argument['key']) : $argument['key'];
$field = self::getFieldName($argument, $isOption);
if (!isset($origOptions[$field]) && !isset($origArgs[$field])) {
$field = self::getUniqueFieldName($definition, $field, $isOption);
/** @var ContextElement $element */
$element = $metadata['element'];

if ($isOption) {
$definition->addOption(new InputOption($field, null, $mode, $argument['element']->description));
$definition->addOption(new InputOption($field, null, $mode, $element->description));
} else {
$definition->addArgument(new InputArgument($field, $mode, $argument['element']->description));
$definition->addArgument(new InputArgument($field, $mode, $element->description));
}
} else {
$isOption = isset($origOptions[$field]);
}

$this->fieldMapping[$argument['name']] = [$field, $isOption];
$this->fieldMapping[$argument] = [$field, $isOption];
}
}

Expand All @@ -97,51 +106,34 @@ public function getContext(InputInterface $input, StyleInterface $io, array $val
$context = $normalizers = [];
$interactive = $input->isInteractive();

foreach ($resolved ?? $this->resolve() as $argument) {
$key = $argument['name'];
foreach ($resolved ?? $this->resolve() as $argument => $metadata) {
if (null === $resolved) {
[$field, $isOption] = $this->fieldMapping[$key];
[$field, $isOption] = $this->fieldMapping[$argument];
$value = $isOption ? $input->getOption($field) : $input->getArgument($field);
} else {
$field = $key;
$value = $argument['value'] ?? null;
$field = $argument;
$value = $metadata['value'] ?? null;
}

if (array_key_exists($key, $values)) {
$context[$key] = $values[$key];
if (array_key_exists($argument, $values)) {
$context[$argument] = $values[$argument];
continue;
}

$isEmpty = null === $value || false === $value || [] === $value;
$given = !$isEmpty || $input->hasParameterOption('--'.$field);

$required = $metadata['required'] && !($this->flags & self::ALWAYS_OPTIONAL);
$type = $metadata['type'];
/** @var ContextElement $element */
$element = $argument['element'];
$required = $argument['required'] && !($this->flags & self::ALWAYS_OPTIONAL);

if (\is_array($value) && self::isObject($type = $argument['type']) && ($required || $given)) {
$method = is_subclass_of($type, DomainCollectionInterface::class) || is_subclass_of($type, DomainIdInterface::class) ? 'fromValue' : '__construct';
$context[$key] = $this->getContext($input, $io, [], array_map(function (array $argument, int $i) use ($type, $method, $value, $element): array {
if (array_key_exists($argument['name'], $value)) {
$argument['value'] = $value[$argument['name']];
} elseif (array_key_exists($i, $value)) {
$argument['value'] = $value[$i];
} elseif ('bool' === $argument['type']) {
$argument['value'] = false;
} elseif (self::isComplex($argument['type'])) {
$argument['value'] = [];
}

$child = $this->elementFactory->getElement($type, $method, $argument['name']);
$child->label = $element->label.' > '.$child->label;

return ['element' => $child] + $argument;
}, $objectResolved = ClassMethodResolver::resolve($type, $method), array_keys($objectResolved)));
$element = $metadata['element'];

if (\is_array($value) && self::isObject($type) && ($required || $given)) {
$context[$argument] = $this->getContext($input, $io, [], $this->resolveNested($type, $value, $element));
continue;
}

if (!$isEmpty) {
$context[$key] = $element->normalize($value);
$context[$argument] = $element->normalize($value);
continue;
}

Expand All @@ -152,9 +144,9 @@ public function getContext(InputInterface $input, StyleInterface $io, array $val

if ($element->generate($io, $generated)) {
$this->generatedValues[] = [$element->label, json_encode($generated)];
$context[$key] = $element->normalize($generated);
$context[$argument] = $element->normalize($generated);
} else {
$context[$key] = $this->askRequiredValue($io, $element, $value);
$context[$argument] = self::askRequiredValue($io, $element, $value);
}
continue;
}
Expand All @@ -163,7 +155,7 @@ public function getContext(InputInterface $input, StyleInterface $io, array $val
continue;
}

$context[$key] = $element->normalize($argument['default']);
$context[$argument] = $element->normalize($metadata['default']);
}

if ($this->generatedValues) {
Expand All @@ -185,6 +177,23 @@ private static function isObject(?string $type): bool
return null !== $type && (class_exists($type) || interface_exists($type, false));
}

private static function askRequiredValue(StyleInterface $io, ContextElement $element, $emptyValue)
{
if (null === $emptyValue) {
return $element->askString($io);
}

if (false === $emptyValue) {
return $element->askBool($io);
}

if ([] === $emptyValue) {
return $element->askIterable($io);
}

return $emptyValue;
}

private function resolve(): iterable
{
if (null !== $this->resolved) {
Expand All @@ -193,30 +202,44 @@ private function resolve(): iterable

$this->resolved = [];

foreach (ClassMethodResolver::resolve($this->class, $this->method) as $argument) {
$this->resolved[] = [
'element' => $this->elementFactory->getElement($this->class, $this->method, $argument['name']),
'type' => isset($argument['type']) ? ($this->classMapping[$argument['type']] ?? $argument['type']) : null,
] + $argument;
foreach (ClassMethodResolver::resolve($this->class, $this->method) as $argument => $metadata) {
$this->resolved[$argument] = [
'element' => $this->elementFactory->getElement($this->class, $this->method, $argument),
'type' => isset($metadata['type']) ? ($this->classMapping[$metadata['type']] ?? $metadata['type']) : null,
] + $metadata;
}

return $this->resolved;
}

private function askRequiredValue(StyleInterface $io, ContextElement $element, $emptyValue)
private function resolveNested(string $type, array $parentValue, ContextElement $parentElement): iterable
{
if (null === $emptyValue) {
return $element->askString($io);
}
$method = is_subclass_of($type, DomainCollectionInterface::class) || is_subclass_of($type, DomainIdInterface::class) ? 'fromValue' : '__construct';
$resolved = [];

foreach (ClassMethodResolver::resolve($type, $method) as $argument => $metadata) {
if (array_key_exists($argument, $parentValue)) {
$value = $parentValue[$argument];
} elseif (array_key_exists($metadata['index'], $parentValue)) {
$value = $parentValue[$metadata['index']];
} elseif ('bool' === $metadata['type']) {
$value = false;
} elseif (self::isComplex($metadata['type'])) {
$value = [];
} else {
$value = null;
}

if (false === $emptyValue) {
return $element->askBool($io);
}
$element = $this->elementFactory->getElement($type, $method, $argument);
$element->label = $parentElement->label.' > '.$element->label;

if ([] === $emptyValue) {
return $element->askIterable($io);
$resolved[$argument] = [
'element' => $element,
'type' => isset($metadata['type']) ? ($this->classMapping[$metadata['type']] ?? $metadata['type']) : null,
'value' => $value,
] + $metadata;
}

return $emptyValue;
return $resolved;
}
}
23 changes: 10 additions & 13 deletions src/Domain/Tests/Factory/ClassMethodResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public function testResolve(): void
$arguments = ClassMethodResolver::resolve(TestClass::class, '__construct');

self::assertSame([
['name' => 'fooBar', 'key' => 'foo_bar', 'required' => true, 'default' => null, 'type' => 'string'],
['name' => 'foo_bar', 'key' => 'foo_bar', 'required' => false, 'default' => null, 'type' => WrongCase::class],
['name' => 'fooBar_Baz', 'key' => 'foo_bar_baz', 'required' => false, 'default' => null, 'type' => null],
['name' => 'it', 'key' => 'it', 'required' => true, 'default' => [], 'type' => 'iterable'],
['name' => '_stdClass', 'key' => '_std_class', 'required' => true, 'default' => null, 'type' => \stdClass::class],
['name' => 'foo', 'key' => 'foo', 'required' => false, 'default' => 1, 'type' => 'int'],
['name' => 'bar', 'key' => 'bar', 'required' => false, 'default' => null, 'type' => TestClass::class],
['name' => 'baz', 'key' => 'baz', 'required' => false, 'default' => [1], 'type' => 'array'],
'fooBar' => ['index' => 0, 'required' => true, 'default' => null, 'type' => 'string'],
'foo_bar' => ['index' => 1, 'required' => false, 'default' => null, 'type' => WrongCase::class],
'fooBar_Baz' => ['index' => 2, 'required' => false, 'default' => null, 'type' => null],
'it' => ['index' => 3, 'required' => true, 'default' => [], 'type' => 'iterable'],
'_stdClass' => ['index' => 4, 'required' => true, 'default' => null, 'type' => \stdClass::class],
'foo' => ['index' => 5, 'required' => false, 'default' => 1, 'type' => 'int'],
'bar' => ['index' => 6, 'required' => false, 'default' => null, 'type' => TestClass::class],
'baz' => ['index' => 7, 'required' => false, 'default' => [1], 'type' => 'array'],
], $arguments);
}

Expand All @@ -39,17 +39,14 @@ public function testResolveWithUnknownClass(): void
{
$this->expectException(InvalidClassException::class);

ClassMethodResolver::resolve('foo', 'bar');
ClassMethodResolver::resolve(UnknownTestObject::class, 'bar');
}

public function testResolveWithUnknownMethod(): void
{
$object = new class() {
};

$this->expectException(InvalidClassException::class);

ClassMethodResolver::resolve(\get_class($object), 'bar');
ClassMethodResolver::resolve(\stdClass::class, 'bar');
}
}

Expand Down
Loading