You are here

protected function Twig_Template::getAttribute in Translation template extractor 7.3

Same name and namespace in other branches
  1. 6.3 vendor/Twig/Template.php \Twig_Template::getAttribute()

Returns the attribute value for a given array/object.

Parameters

mixed $object The object or array from where to get the item:

mixed $item The item to get from the array or object:

array $arguments An array of arguments to pass if the item is an object method:

string $type The type of attribute (@see Twig_Template constants):

bool $isDefinedTest Whether this is only a defined check:

bool $ignoreStrictCheck Whether to ignore the strict attribute check or not:

Return value

mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true

Throws

Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false

File

vendor/Twig/Template.php, line 456

Class

Twig_Template
Default base class for compiled templates.

Code

protected function getAttribute($object, $item, array $arguments = array(), $type = self::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) {

  // array
  if (self::METHOD_CALL !== $type) {
    $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
    if (is_array($object) && array_key_exists($arrayItem, $object) || $object instanceof ArrayAccess && isset($object[$arrayItem])) {
      if ($isDefinedTest) {
        return true;
      }
      return $object[$arrayItem];
    }
    if (self::ARRAY_CALL === $type || !is_object($object)) {
      if ($isDefinedTest) {
        return false;
      }
      if ($ignoreStrictCheck || !$this->env
        ->isStrictVariables()) {
        return;
      }
      if ($object instanceof ArrayAccess) {
        $message = sprintf('Key "%s" in object with ArrayAccess of class "%s" does not exist', $arrayItem, get_class($object));
      }
      elseif (is_object($object)) {
        $message = sprintf('Impossible to access a key "%s" on an object of class "%s" that does not implement ArrayAccess interface', $item, get_class($object));
      }
      elseif (is_array($object)) {
        if (empty($object)) {
          $message = sprintf('Key "%s" does not exist as the array is empty', $arrayItem);
        }
        else {
          $message = sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object)));
        }
      }
      elseif (self::ARRAY_CALL === $type) {
        if (null === $object) {
          $message = sprintf('Impossible to access a key ("%s") on a null variable', $item);
        }
        else {
          $message = sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
        }
      }
      elseif (null === $object) {
        $message = sprintf('Impossible to access an attribute ("%s") on a null variable', $item);
      }
      else {
        $message = sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
      }
      throw new Twig_Error_Runtime($message, -1, $this
        ->getTemplateName());
    }
  }
  if (!is_object($object)) {
    if ($isDefinedTest) {
      return false;
    }
    if ($ignoreStrictCheck || !$this->env
      ->isStrictVariables()) {
      return;
    }
    if (null === $object) {
      $message = sprintf('Impossible to invoke a method ("%s") on a null variable', $item);
    }
    else {
      $message = sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object);
    }
    throw new Twig_Error_Runtime($message, -1, $this
      ->getTemplateName());
  }

  // object property
  if (self::METHOD_CALL !== $type && !$object instanceof self) {

    // Twig_Template does not have public properties, and we don't want to allow access to internal ones
    if (isset($object->{$item}) || array_key_exists((string) $item, $object)) {
      if ($isDefinedTest) {
        return true;
      }
      if ($this->env
        ->hasExtension('sandbox')) {
        $this->env
          ->getExtension('sandbox')
          ->checkPropertyAllowed($object, $item);
      }
      return $object->{$item};
    }
  }
  $class = get_class($object);

  // object method
  if (!isset(self::$cache[$class]['methods'])) {

    // get_class_methods returns all methods accessible in the scope, but we only want public ones to be accessible in templates
    if ($object instanceof self) {
      $ref = new ReflectionClass($class);
      $methods = array();
      foreach ($ref
        ->getMethods(ReflectionMethod::IS_PUBLIC) as $refMethod) {
        $methodName = strtolower($refMethod->name);

        // Accessing the environment from templates is forbidden to prevent untrusted changes to the environment
        if ('getenvironment' !== $methodName) {
          $methods[$methodName] = true;
        }
      }
      self::$cache[$class]['methods'] = $methods;
    }
    else {
      self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
    }
  }
  $call = false;
  $lcItem = strtolower($item);
  if (isset(self::$cache[$class]['methods'][$lcItem])) {
    $method = (string) $item;
  }
  elseif (isset(self::$cache[$class]['methods']['get' . $lcItem])) {
    $method = 'get' . $item;
  }
  elseif (isset(self::$cache[$class]['methods']['is' . $lcItem])) {
    $method = 'is' . $item;
  }
  elseif (isset(self::$cache[$class]['methods']['__call'])) {
    $method = (string) $item;
    $call = true;
  }
  else {
    if ($isDefinedTest) {
      return false;
    }
    if ($ignoreStrictCheck || !$this->env
      ->isStrictVariables()) {
      return;
    }
    throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this
      ->getTemplateName());
  }
  if ($isDefinedTest) {
    return true;
  }
  if ($this->env
    ->hasExtension('sandbox')) {
    $this->env
      ->getExtension('sandbox')
      ->checkMethodAllowed($object, $method);
  }

  // Some objects throw exceptions when they have __call, and the method we try
  // to call is not supported. If ignoreStrictCheck is true, we should return null.
  try {
    $ret = call_user_func_array(array(
      $object,
      $method,
    ), $arguments);
  } catch (BadMethodCallException $e) {
    if ($call && ($ignoreStrictCheck || !$this->env
      ->isStrictVariables())) {
      return;
    }
    throw $e;
  }

  // useful when calling a template method from a template
  // this is not supported but unfortunately heavily used in the Symfony profiler
  if ($object instanceof Twig_TemplateInterface) {
    return $ret === '' ? '' : new Twig_Markup($ret, $this->env
      ->getCharset());
  }
  return $ret;
}