Skip to content

Commit 1f16cac

Browse files
committed
Close #177
1 parent 3a56655 commit 1f16cac

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Schema/Container.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public function register($type, $schema)
8181

8282
$isOk = (
8383
(is_string($schema) === true && empty($schema) === false) ||
84-
$schema instanceof Closure ||
84+
is_callable($schema) ||
8585
$schema instanceof SchemaProviderInterface
8686
);
8787
if ($isOk === false) {
8888
throw new InvalidArgumentException(T::t(
89-
'Schema for type \'%s\' must be non-empty string, Closure or SchemaProviderInterface instance.',
89+
'Schema for type \'%s\' must be non-empty string, callable or SchemaProviderInterface instance.',
9090
[$type]
9191
));
9292
}
@@ -146,11 +146,12 @@ public function getSchemaByType($type)
146146
throw new InvalidArgumentException(T::t('Schema is not registered for type \'%s\'.', [$type]));
147147
}
148148

149-
$classNameOrClosure = $this->getProviderMapping($type);
150-
if ($classNameOrClosure instanceof Closure) {
151-
$schema = $this->createSchemaFromClosure($classNameOrClosure);
149+
$classNameOrCallable = $this->getProviderMapping($type);
150+
if (is_string($classNameOrCallable) === true) {
151+
$schema = $this->createSchemaFromClassName($classNameOrCallable);
152152
} else {
153-
$schema = $this->createSchemaFromClassName($classNameOrClosure);
153+
assert('is_callable($classNameOrCallable) === true');
154+
$schema = $this->createSchemaFromCallable($classNameOrCallable);
154155
}
155156
$this->setCreatedProvider($type, $schema);
156157

@@ -313,6 +314,7 @@ protected function getResourceType($resource)
313314
}
314315

315316
/**
317+
* @deprecated Use `createSchemaFromCallable` method instead.
316318
* @param Closure $closure
317319
*
318320
* @return SchemaProviderInterface
@@ -324,6 +326,19 @@ protected function createSchemaFromClosure(Closure $closure)
324326
return $schema;
325327
}
326328

329+
/**
330+
* @param callable $callable
331+
*
332+
* @return SchemaProviderInterface
333+
*/
334+
protected function createSchemaFromCallable(callable $callable)
335+
{
336+
$schema = $callable instanceof Closure ?
337+
$this->createSchemaFromClosure($callable) : call_user_func($callable, $this->getFactory());
338+
339+
return $schema;
340+
}
341+
327342
/**
328343
* @param string $className
329344
*

tests/Schema/ContainerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,28 @@ public function testRegisterSchemaInstance()
138138

139139
$this->assertSame($authorSchema, $container->getSchema(new Author()));
140140
}
141+
142+
/**
143+
* Test container.
144+
*
145+
* @link https://github.com/neomerx/json-api/issues/177
146+
*/
147+
public function testRegisterCallableSchemeFactory()
148+
{
149+
$container = new Container($this->factory, [
150+
Author::class => [static::class, 'AuthorShemeFactory'],
151+
]);
152+
153+
$this->assertNotNull($container->getSchema(new Author()));
154+
}
155+
156+
/**
157+
* @param SchemaFactoryInterface $factory
158+
*
159+
* @return AuthorSchema
160+
*/
161+
public static function AuthorShemeFactory(SchemaFactoryInterface $factory)
162+
{
163+
return new AuthorSchema($factory);
164+
}
141165
}

0 commit comments

Comments
 (0)