You are here

public function ProxyBuilder::build in Service Container 7.2

Same name and namespace in other branches
  1. 7 lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php \Drupal\Component\ProxyBuilder\ProxyBuilder::build()

Builds a proxy class string.

Parameters

string $class_name: The class name of the actual service.

string $proxy_class_name: (optional) The class name of the proxy service.

Return value

string The full string with namespace class and methods.

File

lib/Drupal/Component/ProxyBuilder/ProxyBuilder.php, line 62
Contains \Drupal\Component\ProxyBuilder\ProxyBuilder.

Class

ProxyBuilder
Generates the string representation of the proxy service.

Namespace

Drupal\Component\ProxyBuilder

Code

public function build($class_name, $proxy_class_name = '') {
  $reflection = new \ReflectionClass($class_name);
  if ($proxy_class_name) {
    $proxy_class_reflection = new \ReflectionClass($proxy_class_name);
    $proxy_namespace = $proxy_class_reflection
      ->getNamespaceName();
  }
  else {
    $proxy_class_name = $this
      ->buildProxyClassName($class_name);
    $proxy_namespace = $this
      ->buildProxyNamespace($class_name);
    $proxy_class_shortname = str_replace($proxy_namespace . '\\', '', $proxy_class_name);
  }
  $output = '';
  $class_documentation = <<<'EOS'

namespace {{ namespace }}{

    /**
     * Provides a proxy class for \{{ class_name }}.
     *
     * @see \Drupal\Component\ProxyBuilder
     */

EOS;
  $class_start = '    class {{ proxy_class_shortname }}';

  // For cases in which the implemented interface is a child of another
  // interface, getInterfaceNames() also returns the parent. This causes a
  // PHP error.
  // In order to avoid that, check for each interface, whether one of its
  // parents is also in the list and exclude it.
  if ($interfaces = $reflection
    ->getInterfaces()) {
    foreach ($interfaces as $interface_name => $interface) {

      // Exclude all parents from the list of implemented interfaces of the
      // class.
      if ($parent_interfaces = $interface
        ->getInterfaceNames()) {
        foreach ($parent_interfaces as $parent_interface) {
          if (isset($interfaces[$parent_interface])) {
          }
          unset($interfaces[$parent_interface]);
        }
      }
    }
    $interface_names = [];
    foreach ($interfaces as $interface) {
      $interface_names[] = '\\' . $interface
        ->getName();
    }
    $class_start .= ' implements ' . implode(', ', $interface_names);
  }
  $output .= $this
    ->buildUseStatements();

  // The actual class;
  $properties = <<<'EOS'
/**
 * The id of the original proxied service.
 *
 * @var string
 */
protected $drupalProxyOriginalServiceId;

/**
 * The real proxied service, after it was lazy loaded.
 *
 * @var \{{ class_name }}
 */
protected $service;

/**
 * The service container.
 *
 * @var \Symfony\Component\DependencyInjection\ContainerInterface
 */
protected $container;


EOS;
  $output .= $properties;

  // Add all the methods.
  $methods = [];
  $methods[] = $this
    ->buildConstructorMethod();
  $methods[] = $this
    ->buildLazyLoadItselfMethod();

  // Add all the methods of the proxied service.
  $reflection_methods = $reflection
    ->getMethods();
  foreach ($reflection_methods as $method) {
    if ($method
      ->getName() === '__construct') {
      continue;
    }
    if ($method
      ->isPublic()) {
      $methods[] = $this
        ->buildMethod($method) . "\n";
    }
  }
  $output .= implode("\n", $methods);

  // Indent the output.
  $output = implode("\n", array_map(function ($value) {
    if ($value === '') {
      return $value;
    }
    return "        {$value}";
  }, explode("\n", $output)));
  $final_output = $class_documentation . $class_start . "\n    {\n\n" . $output . "\n    }\n\n}\n";
  $final_output = str_replace('{{ class_name }}', $class_name, $final_output);
  $final_output = str_replace('{{ namespace }}', $proxy_namespace ? $proxy_namespace . ' ' : '', $final_output);
  $final_output = str_replace('{{ proxy_class_shortname }}', $proxy_class_shortname, $final_output);
  return $final_output;
}