Nette\DI\Container::createService PHP Метод

createService() публичный Метод

Creates new instance of the service.
public createService ( $name, array $args = [] ) : object
$args array
Результат object
    public function createService($name, array $args = [])
    {
        $name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
        $method = self::getMethodName($name);
        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) {
            throw new MissingServiceException("Service '{$name}' not found.");
        }
        try {
            $this->creating[$name] = TRUE;
            $service = $this->{$method}(...$args);
        } finally {
            unset($this->creating[$name]);
        }
        if (!is_object($service)) {
            throw new Nette\UnexpectedValueException("Unable to create service '{$name}', value returned by method {$method}() is not object.");
        }
        return $service;
    }

Usage Example

 /**
  * Creates new presenter instance.
  *
  * @param  string presenter class name
  * @return Application\IPresenter
  */
 public function createPresenter($class)
 {
     $callInjects = $this->alwaysCallInjects;
     $services = array_keys($this->container->findByTag('nette.presenter'), $class);
     if (count($services) > 1) {
         throw new Application\InvalidPresenterException("Multiple services of type {$class} found: " . implode(', ', $services) . '.');
     } elseif (count($services)) {
         $presenter = $this->container->createService($services[0]);
         $callInjects = FALSE;
     } elseif (count($services = $this->container->findByType($class)) === 1) {
         $presenter = $this->container->createService($services[0]);
     } else {
         $presenter = $this->container->createInstance($class);
         $callInjects = TRUE;
     }
     if (!$presenter instanceof Application\IPresenter) {
         throw new UnexpectedValueException("Unable to create create presenter, returned value is not Nette\\Application\\IPresenter type.");
     }
     if ($callInjects) {
         $this->container->callInjects($presenter);
     }
     if ($presenter instanceof Application\UI\Presenter && $presenter->invalidLinkMode === NULL) {
         $presenter->invalidLinkMode = $this->invalidLinkMode;
     }
     return $presenter;
 }
All Usage Examples Of Nette\DI\Container::createService