Permalink
Browse files

Container: removed support for statically added createService* method…

…s (BC break)

All available services must be listed in $services
  • Loading branch information...
dg committed Dec 6, 2018
1 parent c298a5b commit cc7c6f9837ef059d7ef34029c2a813419a185d45
@@ -130,9 +130,7 @@ public function getServiceType(string $name): string
public function hasService(string $name): bool
{
$name = $this->aliases[$name] ?? $name;
return isset($this->registry[$name])
|| (method_exists($this, $method = self::getMethodName($name))
&& (new \ReflectionMethod($this, $method))->getName() === $method);
return isset($this->services[$name]) || isset($this->registry[$name]);
}
@@ -161,7 +159,7 @@ public function createService(string $name, array $args = [])
if (isset($this->creating[$name])) {
throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating))));
} elseif (!method_exists($this, $method) || (new \ReflectionMethod($this, $method))->getName() !== $method) {
} elseif (!isset($this->services[$name])) {
throw new MissingServiceException("Service '$name' not found.");
}
@@ -273,7 +271,6 @@ public function callMethod(callable $function, array $args = [])
public static function getMethodName(string $name): string
{
$uname = ucfirst($name);
return 'createService' . ($name === $uname ? '__' : '') . str_replace('.', '__', $uname);
return 'createService' . str_replace('.', '__', ucfirst($name));
}
}
@@ -15,6 +15,8 @@ require __DIR__ . '/../bootstrap.php';
class MyContainer extends Container
{
protected $services = ['one' => true, 'two' => true];
protected function createServiceOne()
{
return $this->getService('two');
@@ -17,14 +17,14 @@ class MyContainer extends Container
{
protected function createServiceOne()
{
return null;
return new stdClass;
}
}
$container = new MyContainer;
Assert::true($container->hasService('one'));
Assert::false($container->hasService('one'));
$container->addService('one', new stdClass);

This file was deleted.

Oops, something went wrong.

0 comments on commit cc7c6f9

Please sign in to comment.