components) > 0) { $method = explode('_', $method, 2); if (sizeof($method) === 2 && ! empty($method[0]) && ! empty($method[1])) { $component = $this->getComponent($method[0]); if ($component) { return $this->forwardCallTo($component, $method[1], $parameters); } } } return null; } /** * Get the component object * @param string $name * @return Component|null */ public function getComponent(string $name) { $name = mb_strtolower($name); if (isset($this->components[$name])) { if (($component = $this->components[$name]) instanceof Closure) { $this->components[$name] = $component(); } return $this->components[$name]; } return null; } /** * Attach the components. * @param array $components ['name' => Component|Closure, ...] * @return void */ public function attachComponents(array $components) { foreach ($components as $name => $component) { $this->attachComponent($name, $component); } } /** * Attach the component. * @param string $name. * @param Component|Closure $component. * @return mixed */ public function attachComponent(string $name, $component) { if (($exists = $this->getComponent($name)) !== null) { return $exists; } if ($component instanceof Component) { if (! $component->getIsInitialized()) { $component->init(); } return ($this->components[$name] = $component); } elseif ($component instanceof Closure) { return ($this->components[$name] = $component); } else { return null; } } /** * Detach all the attached components. * @return void */ public function detachComponents() { if (sizeof($this->components) > 0) { foreach ($this->components as $name => $component) { $this->detachComponent($name); } $this->components = []; } } /** * Detach the component. * @param string $name component name. * @return mixed */ public function detachComponent(string $name) { if (($component = $this->getComponent($name)) !== null) { unset($this->components[mb_strtolower($name)]); return $component; } return null; } }