Skip to content

Commit

Permalink
Fix definition parser to support static factories
Browse files Browse the repository at this point in the history
  • Loading branch information
yiiliveext committed Apr 15, 2021
1 parent 77d90bd commit f8c8c83
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Definition/Normalizer.php
Expand Up @@ -156,6 +156,10 @@ public static function parse($definition, array $allowedMeta): array
$definition = $newDefinition;
}

if (is_callable($definition)) {
return [$definition, $meta];
}

foreach ($definition as $key => $value) {
// Method.
if ($key === ArrayDefinition::CLASS_NAME || $key === ArrayDefinition::CONSTRUCTOR) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/StaticFactory.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Factory\Tests\Support;

class StaticFactory
{

public static function create(): StdClass
{
return new StdClass();
}
}

12 changes: 12 additions & 0 deletions tests/Unit/Definition/NormalizerTest.php
Expand Up @@ -9,6 +9,7 @@
use Yiisoft\Factory\Exception\InvalidConfigException;
use Yiisoft\Factory\Tests\Support\EngineMarkOne;
use Yiisoft\Factory\Tests\Support\Phone;
use Yiisoft\Factory\Tests\Support\StaticFactory;

/**
* NormalizerTest contains tests for Yiisoft\Factory\Definition\Normalizer
Expand All @@ -27,6 +28,17 @@ public function testParseCallableDefinition(): void
$this->assertSame(['tags' => ['one', 'two']], $meta);
}

public function testParseArrayCallableDefinition(): void
{
$definition = [
'definition' => [StaticFactory::class, 'create'],
'tags' => ['one', 'two'],
];
[$definition, $meta] = Normalizer::parse($definition, ['tags']);
$this->assertSame([StaticFactory::class, 'create'], $definition);
$this->assertSame(['tags' => ['one', 'two']], $meta);
}

public function testParseArrayDefinition(): void
{
$definition = [
Expand Down

0 comments on commit f8c8c83

Please sign in to comment.