vendor/shopware/core/Framework/Adapter/Twig/functions.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Twig;
  3. use Shopware\Core\Framework\DataAbstractionLayer\FieldVisibility;
  4. use Shopware\Core\Framework\Struct\Struct;
  5. use Twig\Environment;
  6. use Twig\Source;
  7. use Twig\Template;
  8. if (!\function_exists('Shopware\Core\Framework\Adapter\Twig\sw_get_attribute')) {
  9.     /**
  10.      * Returns the attribute value for a given array/object.
  11.      *
  12.      * @param mixed  $object            The object or array from where to get the item
  13.      * @param mixed  $item              The item to get from the array or object
  14.      * @param array  $arguments         An array of arguments to pass if the item is an object method
  15.      * @param string $type              The type of attribute (@see \Twig\Template constants)
  16.      * @param bool   $isDefinedTest     Whether this is only a defined check
  17.      * @param bool   $ignoreStrictCheck Whether to ignore the strict attribute check or not
  18.      * @param int    $lineno            The template line where the attribute was called
  19.      *
  20.      * @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
  21.      *
  22.      * @internal
  23.      */
  24.     function sw_get_attribute(Environment $envSource $source$object$item, array $arguments = [], $type /* Template::ANY_CALL */ 'any'$isDefinedTest false$ignoreStrictCheck false$sandboxed falseint $lineno = -1)
  25.     {
  26.         try {
  27.             if ($object instanceof Struct) {
  28.                 FieldVisibility::$isInTwigRenderingContext true;
  29.                 if ($type === Template::METHOD_CALL) {
  30.                     return $object->$item(...$arguments);
  31.                 }
  32.                 $getter 'get' ucfirst($item);
  33.                 $isGetter 'is' ucfirst($item);
  34.                 if (method_exists($object$getter)) {
  35.                     return $object->$getter();
  36.                 } elseif (method_exists($object$isGetter)) {
  37.                     return $object->$isGetter();
  38.                 } elseif (method_exists($object$item)) {
  39.                     return $object->$item();    //property()
  40.                 }
  41.             }
  42.             return \twig_get_attribute($env$source$object$item$arguments$type$isDefinedTest$ignoreStrictCheck$sandboxed$lineno);
  43.         } catch (\Throwable $e) {
  44.             return \twig_get_attribute($env$source$object$item$arguments$type$isDefinedTest$ignoreStrictCheck$sandboxed$lineno);
  45.         } finally {
  46.             FieldVisibility::$isInTwigRenderingContext false;
  47.         }
  48.     }
  49. }
  50. if (!\function_exists('Shopware\Core\Framework\Adapter\Twig\sw_escape_filter')) {
  51.     /**
  52.      * Escapes a string.
  53.      *
  54.      * @param mixed  $string     The value to be escaped
  55.      * @param string $strategy   The escaping strategy
  56.      * @param ?string $charset    The charset
  57.      * @param bool   $autoescape Whether the function is called by the auto-escaping feature (true) or by the developer (false)
  58.      *
  59.      * @return string
  60.      */
  61.     function sw_escape_filter(Environment $env$stringstring $strategy 'html'$charset null$autoescape false)
  62.     {
  63.         if (\is_int($string)) {
  64.             $string = (string) $string;
  65.         }
  66.         static $strings = [];
  67.         $isString \is_string($string);
  68.         if ($isString && isset($strings[$string][$strategy])) {
  69.             return $strings[$string][$strategy];
  70.         }
  71.         $result twig_escape_filter($env$string$strategy$charset$autoescape);
  72.         if (!$isString) {
  73.             return $result;
  74.         }
  75.         $strings[$string][$strategy] = $result;
  76.         return $result;
  77.     }
  78. }