vendor/easycorp/easyadmin-bundle/src/Twig/EasyAdminTwigExtension.php line 139

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Twig;
  3. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  4. use Symfony\Component\DependencyInjection\ServiceLocator;
  5. use Twig\Environment;
  6. use Twig\Extension\AbstractExtension;
  7. use Twig\Extension\ExtensionInterface;
  8. use Twig\Extension\RuntimeExtensionInterface;
  9. use Twig\TwigFilter;
  10. use Twig\TwigFunction;
  11. /**
  12.  * Defines the filters and functions used to render the bundle's templates.
  13.  *
  14.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  15.  */
  16. class EasyAdminTwigExtension extends AbstractExtension
  17. {
  18.     private $serviceLocator;
  19.     public function __construct(ServiceLocator $serviceLocator)
  20.     {
  21.         $this->serviceLocator $serviceLocator;
  22.     }
  23.     /**
  24.      * @return array
  25.      */
  26.     public function getFunctions()
  27.     {
  28.         return [
  29.             new TwigFunction('ea_url', [$this'getAdminUrlGenerator']),
  30.             new TwigFunction('ea_call_function_if_exists', [$this'callFunctionIfExists'], ['needs_environment' => true'is_safe' => ['html' => true]]),
  31.         ];
  32.     }
  33.     /**
  34.      * @return array
  35.      */
  36.     public function getFilters()
  37.     {
  38.         return [
  39.             new TwigFilter('ea_flatten_array', [$this'flattenArray']),
  40.             new TwigFilter('ea_filesize', [$this'fileSize']),
  41.             new TwigFilter('ea_apply_filter_if_exists', [$this'applyFilterIfExists'], ['needs_environment' => true]),
  42.             new TwigFilter('ea_as_string', [$this'representAsString']),
  43.         ];
  44.     }
  45.     /**
  46.      * Transforms ['a' => 'foo', 'b' => ['c' => ['d' => 7]]] into ['a' => 'foo', 'b[c][d]' => 7]
  47.      * It's useful to submit nested arrays (e.g. query string parameters) as form fields.
  48.      */
  49.     public function flattenArray($array$parentKey null)
  50.     {
  51.         $flattenedArray = [];
  52.         foreach ($array as $flattenedKey => $value) {
  53.             $flattenedKey null !== $parentKey sprintf('%s[%s]'$parentKey$flattenedKey) : $flattenedKey;
  54.             if (\is_array($value)) {
  55.                 $flattenedArray array_merge($flattenedArray$this->flattenArray($value$flattenedKey));
  56.             } else {
  57.                 $flattenedArray[$flattenedKey] = $value;
  58.             }
  59.         }
  60.         return $flattenedArray;
  61.     }
  62.     public function fileSize(int $bytes): string
  63.     {
  64.         $size = ['B''K''M''G''T''P''E''Z''Y'];
  65.         $factor = (int) floor(log($bytes) / log(1024));
  66.         return (int) ($bytes / (1024 ** $factor)).@$size[$factor];
  67.     }
  68.     // Code adapted from https://stackoverflow.com/a/48606773/2804294 (License: CC BY-SA 3.0)
  69.     public function applyFilterIfExists(Environment $environment$valuestring $filterName, ...$filterArguments)
  70.     {
  71.         $filter $environment->getFilter($filterName);
  72.         if (false === $filter || null === $filter) {
  73.             return $value;
  74.         }
  75.         list($class$method) = $filter->getCallable();
  76.         if ($class instanceof ExtensionInterface) {
  77.             return $filter->getCallable()($value, ...$filterArguments);
  78.         }
  79.         $object $environment->getRuntime($class);
  80.         if ($object instanceof RuntimeExtensionInterface && method_exists($object$method)) {
  81.             return $object->$method($value, ...$filterArguments);
  82.         }
  83.         return null;
  84.     }
  85.     public function representAsString($value): string
  86.     {
  87.         if (null === $value) {
  88.             return '';
  89.         }
  90.         if (\is_string($value)) {
  91.             return $value;
  92.         }
  93.         if (is_numeric($value)) {
  94.             return (string) $value;
  95.         }
  96.         if (\is_bool($value)) {
  97.             return $value 'true' 'false';
  98.         }
  99.         if (\is_array($value)) {
  100.             return sprintf('Array (%d items)'\count($value));
  101.         }
  102.         if (\is_object($value)) {
  103.             if (method_exists($value'__toString')) {
  104.                 return (string) $value;
  105.             }
  106.             if (method_exists($value'getId')) {
  107.                 return sprintf('%s #%s'\get_class($value), $value->getId());
  108.             }
  109.             return sprintf('%s #%s'\get_class($value), substr(md5(spl_object_hash($value)), 07));
  110.         }
  111.         return '';
  112.     }
  113.     public function callFunctionIfExists(Environment $environmentstring $functionName, ...$functionArguments)
  114.     {
  115.         if (false === $function $environment->getFunction($functionName)) {
  116.             return '';
  117.         }
  118.         return $function->getCallable()(...$functionArguments);
  119.     }
  120.     public function getAdminUrlGenerator(array $queryParameters = []): AdminUrlGenerator
  121.     {
  122.         return $this->serviceLocator->get(AdminUrlGenerator::class)->setAll($queryParameters);
  123.     }
  124. }