You are here

private function PhpDumper::addService in Service Container 7

Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php \Symfony\Component\DependencyInjection\Dumper\PhpDumper::addService()

Adds a service.

Parameters

string $id:

Definition $definition:

Return value

string

1 call to PhpDumper::addService()
PhpDumper::addServices in modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Adds multiple services.

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php, line 552

Class

PhpDumper
PhpDumper dumps a service container as a PHP class.

Namespace

Symfony\Component\DependencyInjection\Dumper

Code

private function addService($id, $definition) {
  $this->definitionVariables = new \SplObjectStorage();
  $this->referenceVariables = array();
  $this->variableCount = 0;
  $return = array();
  if ($definition
    ->isSynthetic()) {
    $return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
  }
  elseif ($class = $definition
    ->getClass()) {
    $return[] = sprintf('@return %s A %s instance.', 0 === strpos($class, '%') ? 'object' : '\\' . $class, $class);
  }
  elseif ($definition
    ->getFactory()) {
    $factory = $definition
      ->getFactory();
    if (is_string($factory)) {
      $return[] = sprintf('@return object An instance returned by %s().', $factory);
    }
    elseif (is_array($factory) && (is_string($factory[0]) || $factory[0] instanceof Definition || $factory[0] instanceof Reference)) {
      if (is_string($factory[0]) || $factory[0] instanceof Reference) {
        $return[] = sprintf('@return object An instance returned by %s::%s().', (string) $factory[0], $factory[1]);
      }
      elseif ($factory[0] instanceof Definition) {
        $return[] = sprintf('@return object An instance returned by %s::%s().', $factory[0]
          ->getClass(), $factory[1]);
      }
    }
  }
  elseif ($definition
    ->getFactoryClass(false)) {
    $return[] = sprintf('@return object An instance returned by %s::%s().', $definition
      ->getFactoryClass(false), $definition
      ->getFactoryMethod(false));
  }
  elseif ($definition
    ->getFactoryService(false)) {
    $return[] = sprintf('@return object An instance returned by %s::%s().', $definition
      ->getFactoryService(false), $definition
      ->getFactoryMethod(false));
  }
  $scope = $definition
    ->getScope();
  if (!in_array($scope, array(
    ContainerInterface::SCOPE_CONTAINER,
    ContainerInterface::SCOPE_PROTOTYPE,
  ))) {
    if ($return && 0 === strpos($return[count($return) - 1], '@return')) {
      $return[] = '';
    }
    $return[] = sprintf("@throws InactiveScopeException when the '%s' service is requested while the '%s' scope is not active", $id, $scope);
  }
  $return = implode("\n     * ", $return);
  $doc = '';
  if (ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
    $doc .= <<<EOF

     *
     * This service is shared.
     * This method always returns the same instance of the service.
EOF;
  }
  if (!$definition
    ->isPublic()) {
    $doc .= <<<EOF

     *
     * This service is private.
     * If you want to be able to request this service from the container directly,
     * make it public, otherwise you might end up with broken code.
EOF;
  }
  if ($definition
    ->isLazy()) {
    $lazyInitialization = '$lazyLoad = true';
    $lazyInitializationDoc = "\n     * @param bool    \$lazyLoad whether to try lazy-loading the service with a proxy\n     *";
  }
  else {
    $lazyInitialization = '';
    $lazyInitializationDoc = '';
  }

  // with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
  $isProxyCandidate = $this
    ->getProxyDumper()
    ->isProxyCandidate($definition);
  $visibility = $isProxyCandidate ? 'public' : 'protected';
  $code = <<<EOF

    /**
     * Gets the '{<span class="php-variable">$id</span>}' service.{<span class="php-variable">$doc</span>}
     *{<span class="php-variable">$lazyInitializationDoc</span>}
     * {<span class="php-variable">$return</span>}
     */
    {<span class="php-variable">$visibility</span>} function get{<span class="php-variable">$this</span>
  -&gt;<span class="php-function-or-constant function member-of-self">camelize</span>(<span class="php-variable">$id</span>)}Service({<span class="php-variable">$lazyInitialization</span>})
    {

EOF;
  $code .= $isProxyCandidate ? $this
    ->getProxyDumper()
    ->getProxyFactoryCode($definition, $id) : '';
  if (!in_array($scope, array(
    ContainerInterface::SCOPE_CONTAINER,
    ContainerInterface::SCOPE_PROTOTYPE,
  ))) {
    $code .= <<<EOF
        if (!isset(\$this->scopedServices['{<span class="php-variable">$scope</span>}'])) {
            throw new InactiveScopeException('{<span class="php-variable">$id</span>}', '{<span class="php-variable">$scope</span>}');
        }


EOF;
  }
  if ($definition
    ->isSynthetic()) {
    $code .= sprintf("        throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n    }\n", $id);
  }
  else {
    $code .= $this
      ->addServiceInclude($id, $definition) . $this
      ->addServiceLocalTempVariables($id, $definition) . $this
      ->addServiceInlinedDefinitions($id, $definition) . $this
      ->addServiceInstance($id, $definition) . $this
      ->addServiceInlinedDefinitionsSetup($id, $definition) . $this
      ->addServiceMethodCalls($id, $definition) . $this
      ->addServiceProperties($id, $definition) . $this
      ->addServiceConfigurator($id, $definition) . $this
      ->addServiceReturn($id, $definition);
  }
  $this->definitionVariables = null;
  $this->referenceVariables = null;
  return $code;
}