buildProxyClassName example

class ProxyServicesPass implements CompilerPassInterface {

  /** * {@inheritdoc} * * phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn * @return void */
  public function process(ContainerBuilder $container) {
    foreach ($container->getDefinitions() as $service_id => $definition) {
      if ($definition->isLazy()) {
        $proxy_class = ProxyBuilder::buildProxyClassName($definition->getClass());
        if (class_exists($proxy_class)) {
          // Copy the existing definition to a new entry.           $definition->setLazy(FALSE);
          // Ensure that the service is accessible.           $definition->setPublic(TRUE);
          $new_service_id = 'drupal.proxy_original_service.' . $service_id;
          $container->setDefinition($new_service_id$definition);

          $container->register($service_id$proxy_class)
            ->setArguments([new Reference('service_container')$new_service_id]);
        }
        
/** * Generates the used proxy namespace from a given class name. * * @param string $class_name * The class name of the actual service. * * @return string * The namespace name of the proxy. */
  public static function buildProxyNamespace($class_name) {
    $proxy_classname = static::buildProxyClassName($class_name);

    preg_match('/(.+)\\\\[a-zA-Z0-9]+/', $proxy_classname$match);
    $proxy_namespace = $match[1];
    return $proxy_namespace;
  }

  /** * Builds a proxy class string. * * @param string $class_name * The class name of the actual service. * * @return string * The full string with namespace class and methods. */

  protected function setUp(): void {
    parent::setUp();

    $this->proxyBuilder = new ProxyBuilder();
  }

  /** * @covers ::buildProxyClassName */
  public function testBuildProxyClassName() {
    $class_name = $this->proxyBuilder->buildProxyClassName('Drupal\Tests\Component\ProxyBuilder\TestServiceNoMethod');
    $this->assertEquals('Drupal\Tests\ProxyClass\Component\ProxyBuilder\TestServiceNoMethod', $class_name);
  }

  /** * @covers ::buildProxyClassName */
  public function testBuildProxyClassNameForModule() {
    $class_name = $this->proxyBuilder->buildProxyClassName('Drupal\views_ui\ParamConverter\ViewUIConverter');
    $this->assertEquals('Drupal\views_ui\ProxyClass\ParamConverter\ViewUIConverter', $class_name);
  }

  
Home | Imprint | This part of the site doesn't use cookies.