You are here

class Decorator in Devel 8

Same name and namespace in other branches
  1. 8.3 webprofiler/src/Decorator.php \Drupal\webprofiler\Decorator
  2. 8.2 webprofiler/src/Decorator.php \Drupal\webprofiler\Decorator
  3. 4.x webprofiler/src/Decorator.php \Drupal\webprofiler\Decorator

Generic class Decorator.

Hierarchy

Expanded class hierarchy of Decorator

1 file declares its use of Decorator
EntityDecorator.php in webprofiler/src/Entity/EntityDecorator.php

File

webprofiler/src/Decorator.php, line 8

Namespace

Drupal\webprofiler
View source
class Decorator {

  /**
   * @var
   */
  protected $object;

  /**
   * Class constructor.
   *
   * @param object $object
   *   The object to decorate.
   */
  public function __construct($object) {
    $this->object = $object;
  }

  /**
   * Return the original (i.e. non decorated) object.
   *
   * @return mixed
   *   The original object.
   */
  public function getOriginalObject() {
    $object = $this->object;
    while ($object instanceof Decorator) {
      $object = $object
        ->getOriginalObject();
    }
    return $object;
  }

  /**
   * Returns true if $method is a PHP callable.
   *
   * @param string $method
   *   The method name.
   * @param bool $checkSelf
   *
   * @return bool|mixed
   */
  public function isCallable($method, $checkSelf = FALSE) {

    //Check the original object
    $object = $this
      ->getOriginalObject();
    if (is_callable([
      $object,
      $method,
    ])) {
      return $object;
    }

    // Check Decorators.
    $object = $checkSelf ? $this : $this->object;
    while ($object instanceof Decorator) {
      if (is_callable([
        $object,
        $method,
      ])) {
        return $object;
      }
      $object = $this->object;
    }
    return FALSE;
  }

  /**
   * @param $method
   * @param $args
   *
   * @return mixed
   *
   * @throws \Exception
   */
  public function __call($method, $args) {
    if ($object = $this
      ->isCallable($method)) {
      return call_user_func_array([
        $object,
        $method,
      ], $args);
    }
    throw new \Exception('Undefined method - ' . get_class($this
      ->getOriginalObject()) . '::' . $method);
  }

  /**
   * @param $property
   *
   * @return null
   */
  public function __get($property) {
    $object = $this
      ->getOriginalObject();
    if (property_exists($object, $property)) {
      return $object->{$property};
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Decorator::$object protected property @var
Decorator::getOriginalObject public function Return the original (i.e. non decorated) object.
Decorator::isCallable public function Returns true if $method is a PHP callable.
Decorator::__call public function
Decorator::__construct public function Class constructor. 2
Decorator::__get public function