ServiceWithDependency.php

Same filename in other branches
  1. 9 core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php
  2. 10 core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php
  3. 11.x core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php

Namespace

Drupal\new_dependency_test

File

core/modules/system/tests/modules/new_dependency_test/src/ServiceWithDependency.php

View source
<?php

namespace Drupal\new_dependency_test;


/**
 * Service that gets the other service of the same module injected.
 *
 * This service indirectly depends on a not-yet-defined service.
 */
class ServiceWithDependency {
    
    /**
     * The injected service.
     *
     * @var \Drupal\new_dependency_test\InjectedService
     */
    protected $service;
    
    /**
     * ServiceWithDependency constructor.
     *
     * @param \Drupal\new_dependency_test\InjectedService|null $service
     *   The service of the same module which has the new dependency.
     */
    public function __construct(InjectedService $service = NULL) {
        $this->service = $service;
    }
    
    /**
     * Gets a greeting from the injected service and adds to it.
     *
     * @return string
     *   The greeting.
     */
    public function greet() {
        if (isset($this->service)) {
            return $this->service
                ->greet() . ' World';
        }
        return 'Sorry, no service.';
    }

}

Classes

Title Deprecated Summary
ServiceWithDependency Service that gets the other service of the same module injected.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.