You are here

abstract class crumbs_DIC_AbstractServiceContainer in Crumbs, the Breadcrumbs suite 7.2

Dependency injection container for lazy-instantiated services.

Hierarchy

Expanded class hierarchy of crumbs_DIC_AbstractServiceContainer

File

lib/DIC/AbstractServiceContainer.php, line 6

View source
abstract class crumbs_DIC_AbstractServiceContainer {

  /**
   * @var object[]
   */
  private $services = array();

  /**
   * Magic method that is triggered when someone calls $container->$name.
   *
   * @param string $name
   *   The machine name of the service.
   *   Must be a valid PHP identifier, without commas and such.
   *
   * @return object
   */
  function __get($name) {
    return isset($this->services[$name]) ? $this->services[$name] : ($this->services[$name] = $this
      ->createService($name));
  }

  /**
   * @param string $name
   *
   * @return object
   *   The service.
   * @throws Exception
   */
  private function createService($name) {

    // Method to be implemented in a subclass.
    $method = $name;
    if (!method_exists($this, $method)) {
      throw new \Exception("Unknown service '{$name}'.");
    }
    return $this
      ->{$method}();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
crumbs_DIC_AbstractServiceContainer::$services private property
crumbs_DIC_AbstractServiceContainer::createService private function
crumbs_DIC_AbstractServiceContainer::__get function Magic method that is triggered when someone calls $container->$name.