diff --git a/src/ComponentModel/Component.php b/src/ComponentModel/Component.php index 82e6d9e..b0c089e 100644 --- a/src/ComponentModel/Component.php +++ b/src/ComponentModel/Component.php @@ -49,6 +49,7 @@ final public function lookup(?string $type, bool $throw = true): ?IComponent if ($type ? $obj instanceof $type : $parent === null) { break; } + $path = self::NAME_SEPARATOR . $obj->getName() . $path; $depth++; $obj = $parent; // IComponent::getParent() @@ -96,6 +97,7 @@ final public function monitor(string $type, callable $attached = null, callable $attached = [$this, 'attached']; $detached = [$this, 'detached']; } + if ( ($obj = $this->lookup($type, false)) && $attached @@ -103,6 +105,7 @@ final public function monitor(string $type, callable $attached = null, callable ) { $attached($obj); } + $this->monitors[$type][3][] = [$attached, $detached]; // mark as monitored } @@ -191,6 +194,7 @@ public function setParent(?IContainer $parent, string $name = null) $tmp = []; $this->refreshMonitors(0, $tmp); } + return $this; } @@ -233,7 +237,6 @@ private function refreshMonitors(int $depth, array &$missing = null, array &$lis } } } - } else { // attaching foreach ($this->monitors as $type => $rec) { if (isset($rec[0])) { // is in cache yet @@ -254,6 +257,7 @@ private function refreshMonitors(int $depth, array &$missing = null, array &$lis } else { $missing[$type] = true; } + $this->monitors[$type][3] = $rec[3]; // mark as monitored } } @@ -287,7 +291,6 @@ public function __clone() if ($this->parent === null) { // not cloning $this->refreshMonitors(0); } - } else { $this->parent = null; $this->refreshMonitors(0); diff --git a/src/ComponentModel/Container.php b/src/ComponentModel/Container.php index d2fd161..b7d3c17 100644 --- a/src/ComponentModel/Container.php +++ b/src/ComponentModel/Container.php @@ -59,6 +59,7 @@ public function addComponent(IComponent $component, ?string $name, string $inser if ($obj === $component) { throw new Nette\InvalidStateException("Circular reference detected while adding component '$name'."); } + $obj = $obj->getParent(); } while ($obj !== null); @@ -71,8 +72,10 @@ public function addComponent(IComponent $component, ?string $name, string $inser if ((string) $k === $insertBefore) { $tmp[$name] = $component; } + $tmp[$k] = $v; } + $this->components = $tmp; } else { $this->components[$name] = $component; @@ -84,6 +87,7 @@ public function addComponent(IComponent $component, ?string $name, string $inser unset($this->components[$name]); // undo throw $e; } + return $this; } @@ -116,6 +120,7 @@ final public function getComponent(string $name, bool $throw = true): ?IComponen if ($throw) { throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given."); } + return null; } @@ -136,7 +141,6 @@ final public function getComponent(string $name, bool $throw = true): ?IComponen } elseif ($throw) { throw new Nette\InvalidArgumentException("Component with name '$name' is not container and cannot have '$parts[1]' component."); } - } elseif ($throw) { $hint = Nette\Utils\ObjectHelpers::getSuggestion(array_merge( array_map('strval', array_keys($this->components)), @@ -144,6 +148,7 @@ final public function getComponent(string $name, bool $throw = true): ?IComponen ), $name); throw new Nette\InvalidArgumentException("Component with name '$name' does not exist" . ($hint ? ", did you mean '$hint'?" : '.')); } + return null; } @@ -165,8 +170,10 @@ protected function createComponent(string $name): ?IComponent $class = static::class; throw new Nette\UnexpectedValueException("Method $class::$method() did not return or create the desired component."); } + return $component; } + return null; } @@ -181,11 +188,13 @@ final public function getComponents(bool $deep = false, string $filterType = nul if ($deep) { $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST); } + if ($filterType) { $iterator = new \CallbackFilterIterator($iterator, function ($item) use ($filterType) { return $item instanceof $filterType; }); } + return $iterator; } @@ -214,8 +223,10 @@ public function __clone() foreach ($this->components as $name => $component) { $this->components[$name] = clone $component; } + $oldMyself->cloning = null; } + parent::__clone(); } diff --git a/tests/ComponentModel/Container.clone-new.phpt b/tests/ComponentModel/Container.clone-new.phpt index a97e8f6..3817582 100644 --- a/tests/ComponentModel/Container.clone-new.phpt +++ b/tests/ComponentModel/Container.clone-new.phpt @@ -40,6 +40,7 @@ function export($obj) $res['children'][$name] = export($child); } } + return $res; } diff --git a/tests/ComponentModel/Container.clone.phpt b/tests/ComponentModel/Container.clone.phpt index ba2dbb3..5a4527f 100644 --- a/tests/ComponentModel/Container.clone.phpt +++ b/tests/ComponentModel/Container.clone.phpt @@ -40,6 +40,7 @@ function export($obj) $res['children'][$name] = export($child); } } + return $res; }