Skip to content
Permalink
Browse files

Container: added getComponentIfExists()

  • Loading branch information...
dg committed Nov 1, 2019
1 parent 6ba5e7e commit 4a666a8a43758604bd39a8828c0271485cf94df3
@@ -48,7 +48,7 @@ public function offsetGet($name): IComponent
public function offsetExists($name): bool
{
$name = is_int($name) ? (string) $name : $name;
return $this->getComponent($name, false) !== null;
return $this->getComponentIfExists($name) !== null;
}
@@ -59,7 +59,7 @@ public function offsetExists($name): bool
public function offsetUnset($name): void
{
$name = is_int($name) ? (string) $name : $name;
if ($component = $this->getComponent($name, false)) {
if ($component = $this->getComponentIfExists($name)) {
$this->removeComponent($component);
}
}
@@ -106,42 +106,65 @@ public function removeComponent(IComponent $component): void
*/
final public function getComponent(string $name, bool $throw = true): ?IComponent
{
if (!$throw) {
return $component->getComponentIfExists($name);
}
[$name] = $parts = explode(self::NAME_SEPARATOR, $name, 2);
if (!isset($this->components[$name])) {
if (!preg_match(self::NAME_REGEXP, $name)) {
if ($throw) {
throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
}
return null;
}
$component = $this->createComponent($name);
if ($component && !isset($this->components[$name])) {
$this->addComponent($component, $name);
}
$this->doCreateComponent($name);
}
$component = $this->components[$name] ?? null;
if ($component !== null) {
if (!isset($parts[1])) {
return $component;
} elseif ($component instanceof IContainer) {
return $component->getComponent($parts[1], $throw);
} elseif ($throw) {
throw new Nette\InvalidArgumentException("Component with name '$name' is not container and cannot have '$parts[1]' component.");
}
} elseif ($throw) {
if (!$component) {
$hint = Nette\Utils\ObjectHelpers::getSuggestion(array_merge(
array_keys($this->components),
array_map('lcfirst', preg_filter('#^createComponent([A-Z0-9].*)#', '$1', get_class_methods($this)))
), $name);
throw new Nette\InvalidArgumentException("Component with name '$name' does not exist" . ($hint ? ", did you mean '$hint'?" : '.'));
} elseif (isset($parts[1])) {
if (!$component instanceof IContainer) {
throw new Nette\InvalidArgumentException("Component with name '$name' is not container and cannot have '$parts[1]' component.");
}
return $component->getComponent($parts[1]);
}
return $component;
}
/**
* Returns component specified by name or path.
*/
final public function getComponentIfExists(string $name): ?IComponent
{
[$name] = $parts = explode(self::NAME_SEPARATOR, $name, 2);
if (!isset($this->components[$name]) && preg_match(self::NAME_REGEXP, $name)) {
$this->doCreateComponent($name);
}
$component = $this->components[$name] ?? null;
if (isset($parts[1])) {
return $component instanceof IContainer
? ($component instanceof self ? $component->getComponentIfExists($parts[1]) : $component->getComponent($parts[1]))
: null;
}
return $component;
}
private function doCreateComponent($name): void
{
if (!preg_match(self::NAME_REGEXP, $name)) {
throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
}
$component = $this->createComponent($name);
if ($component && !isset($this->components[$name])) {
$this->addComponent($component, $name);
}
return null;
}
@@ -26,3 +26,6 @@ class TestClass extends Container
$a = new TestClass;
Assert::same('b', $a->getComponent('b')->getName());
$a = new TestClass;
Assert::same('b', $a->getComponentIfExists('b')->getName());
@@ -26,3 +26,6 @@ class TestClass extends Container
$a = new TestClass;
Assert::same('b', $a->getComponent('b')->getName());
$a = new TestClass;
Assert::same('b', $a->getComponentIfExists('b')->getName());
@@ -29,3 +29,12 @@ $a->addComponent(new TestClass, 'a');
Assert::exception(function () use ($a) {
$a->getComponent('b');
}, Nette\InvalidStateException::class, "Component 'a' already has a parent.");
$a = new TestClass;
$a->addComponent(new TestClass, 'a');
Assert::exception(function () use ($a) {
$a->getComponentIfExists('b');
}, Nette\InvalidStateException::class, "Component 'a' already has a parent.");
@@ -29,3 +29,12 @@ $a->addComponent(new TestClass, 'a');
Assert::exception(function () use ($a) {
$a->getComponent('b');
}, TypeError::class);
$a = new TestClass;
$a->addComponent(new TestClass, 'a');
Assert::exception(function () use ($a) {
$a->getComponentIfExists('b');
}, TypeError::class);
@@ -25,3 +25,7 @@ class TestClass extends Container
$a = new TestClass;
Assert::same('b', $a->getComponent('b')->getName());
$a = new TestClass;
Assert::same('b', $a->getComponentIfExists('b')->getName());
@@ -24,8 +24,11 @@ class TestClass extends Container
$a = new TestClass;
Assert::same('b', $a->getComponent('b')->getName());
Assert::same('b', $a->getComponentIfExists('b')->getName());
Assert::exception(function () use ($a) {
$a->getComponent('B')->getName();
}, InvalidArgumentException::class, "Component with name 'B' does not exist, did you mean 'b'?");
Assert::null($a->getComponentIfExists('B'));
@@ -25,8 +25,11 @@ class TestClass extends Container
$a = new TestClass;
Assert::same('b', $a->getComponent('b')->getName());
Assert::same('b', $a->getComponentIfExists('b')->getName());
Assert::exception(function () use ($a) {
$a->getComponent('B')->getName();
}, InvalidArgumentException::class, "Component with name 'B' does not exist, did you mean 'b'?");
Assert::null($a->getComponentIfExists('B'));
@@ -25,3 +25,9 @@ Assert::exception(function () {
$a = new TestClass;
$a->getComponent('b');
}, Nette\UnexpectedValueException::class, 'Method TestClass::createComponentB() did not return or create the desired component.');
Assert::exception(function () {
$a = new TestClass;
$a->getComponentIfExists('b');
}, Nette\UnexpectedValueException::class, 'Method TestClass::createComponentB() did not return or create the desired component.');
@@ -26,3 +26,6 @@ class TestClass extends Container
$a = new TestClass;
Assert::same('b', $a->getComponent('b')->getName());
$a = new TestClass;
Assert::same('b', $a->getComponentIfExists('b')->getName());
@@ -16,3 +16,4 @@ require __DIR__ . '/../bootstrap.php';
$container = new Container;
$container->addComponent(new Container, '0');
Assert::same('0', $container->getComponent('0')->getName());
Assert::same('0', $container->getComponentIfExists('0')->getName());

0 comments on commit 4a666a8

Please sign in to comment.
You can’t perform that action at this time.