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
5 changes: 2 additions & 3 deletions src/Attribute/ObjectType/Slots.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ public static function lateBindObjectToBuild(mixed $build, mixed $definition, ob
foreach ($definition->slots as $slot) {
if (true === $build->pintoHas($slot->name)) {
$slotValue = $build->pintoGet($slot->name);
if (\is_object($slotValue) && ($invokerMethod = $context->getBuildInvoker($slotValue::class)) !== null) {
// @phpstan-ignore-next-line
$build->set($slot->name, $slotValue->{$invokerMethod}());
if (\is_object($slotValue) && ($builder = $context->getBuilder($slotValue)) !== null) {
$build->set($slot->name, $builder());
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/ObjectType/LateBindObjectContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Pinto\PintoMapping;

/**
* Discovers ObjectTypeInterface attributes on a class, on a class or its methods.
*
* @internal
*/
final class LateBindObjectContext
Expand All @@ -27,13 +25,10 @@ public static function create(PintoMapping $pintoMapping): static
return new static($pintoMapping);
}

/**
* @param class-string $objectClassName
*/
public function getBuildInvoker(string $objectClassName): ?string
public function getBuilder(object $component): ?\Closure
{
try {
return $this->pintoMapping->getBuildInvoker($objectClassName);
return $this->pintoMapping->getBuilder($component);
} catch (PintoMissingObjectMapping) {
return null;
}
Expand Down
11 changes: 11 additions & 0 deletions src/PintoMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public function getBuildInvoker(string $objectClassName): string
return $this->buildInvokers[$objectClassName] ?? throw new PintoMissingObjectMapping($objectClassName);
}

/**
* @throws PintoMissingObjectMapping
*/
public function getBuilder(object $component): \Closure
{
$method = $this->buildInvokers[$component::class] ?? throw new PintoMissingObjectMapping($component::class);

// @phpstan-ignore-next-line
return $component->{$method}(...);
}

public function getResources(): ResourceCollectionInterface
{
return $this->resources;
Expand Down
31 changes: 31 additions & 0 deletions tests/PintoMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,35 @@ public function testGetBuildInvokerException(): void
static::expectException(PintoMissingObjectMapping::class);
$pintoMapping->getBuildInvoker(fixtures\Objects\PintoObject::class);
}

public function testGetBuilderException(): void
{
$pintoMapping = new PintoMapping([], [], [], [], []);
static::expectException(PintoMissingObjectMapping::class);
$component = fixtures\Objects\PintoObject::create('Foo');
$pintoMapping->getBuilder($component);
}

/**
* @covers \Pinto\PintoMapping::getBuilder
*/
public function testGetBuilder(): void
{
$pintoMapping = new PintoMapping(
resources: [],
definitions: [],
buildInvokers: [
fixtures\Objects\PintoObject::class => '__invoke',
],
types: [],
lsbFactoryCanonicalObjectClasses: [],
);

$component = fixtures\Objects\PintoObject::create('Foo');
$builder = $pintoMapping->getBuilder($component);
static::assertIsCallable($builder);

$result = $builder();
static::assertEquals('Foo', $result['#test_variable']);
}
}
Loading