Nette\ComponentModel\Container::getComponent PHP Method

getComponent() public method

Returns component specified by name or path.
public getComponent ( $name, $need = TRUE ) : Nette\ComponentModel\IComponent | null
return Nette\ComponentModel\IComponent | null
    public function getComponent($name, $need = TRUE)
    {
        if (isset($this->components[$name])) {
            return $this->components[$name];
        }
        if (is_int($name)) {
            $name = (string) $name;
        } elseif (!is_string($name)) {
            throw new Nette\InvalidArgumentException(sprintf('Component name must be integer or string, %s given.', gettype($name)));
        } else {
            $a = strpos($name, self::NAME_SEPARATOR);
            if ($a !== FALSE) {
                $ext = (string) substr($name, $a + 1);
                $name = substr($name, 0, $a);
            }
            if ($name === '') {
                if ($need) {
                    throw new Nette\InvalidArgumentException('Component or subcomponent name must not be empty string.');
                }
                return;
            }
        }
        if (!isset($this->components[$name])) {
            $component = $this->createComponent($name);
            if ($component) {
                if (!$component instanceof IComponent) {
                    throw new Nette\UnexpectedValueException('Method createComponent() did not return Nette\\ComponentModel\\IComponent.');
                } elseif (!isset($this->components[$name])) {
                    $this->addComponent($component, $name);
                }
            }
        }
        if (isset($this->components[$name])) {
            if (!isset($ext)) {
                return $this->components[$name];
            } elseif ($this->components[$name] instanceof IContainer) {
                return $this->components[$name]->getComponent($ext, $need);
            } elseif ($need) {
                throw new Nette\InvalidArgumentException("Component with name '{$name}' is not container and cannot have '{$ext}' component.");
            }
        } elseif ($need) {
            $hint = Nette\Utils\ObjectMixin::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}'?" : '.'));
        }
    }

Usage Example

Example #1
0
 /**
  * Returns component specified by name or path.
  *
  * @param  string
  * @param  bool   throw exception if component doesn't exist?
  * @return IComponent|NULL
  */
 public function getComponent($name, $need = TRUE)
 {
     $origName = $name;
     if (($pos = strpos($name, self::NAME_SEPARATOR)) !== FALSE) {
         $name = substr($name, 0, $pos);
     }
     if (array_search($name, $this->registered) === FALSE && $this->widgetList->exists($name)) {
         $this->registered[] = $name;
         $this->addComponent($this->widgetList->get($name), $name);
     }
     return parent::getComponent($origName, $need);
 }