src/Twig/ToolsExtension.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Services\Core\Tools;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFunction;
  6. use Twig\TwigFilter;
  7. class ToolsExtension extends AbstractExtension
  8. {
  9. public function __construct(Tools $tools)
  10. {
  11. $this->tools = $tools;
  12. }
  13. public function getFunctions(): array
  14. {
  15. return [
  16. new TwigFunction('getCoreToolsTag', [$this, 'getToolsTag']),
  17. new TwigFunction('getCoreTagCategory', [$this, 'getTagCategory']),
  18. new TwigFunction('getCoreToolsList', [$this, 'getCoreToolsList']),
  19. ];
  20. }
  21. public function getFilters()
  22. {
  23. return [
  24. new TwigFilter('clean_n', [$this, 'clean_n']),
  25. new TwigFilter('image_dimensions', [$this, 'getImageDimensions']),
  26. ];
  27. }
  28. public function getImageDimensions(string $imagePath): array
  29. {
  30. $fullPath = $_SERVER['DOCUMENT_ROOT'] . $imagePath;
  31. if (file_exists($fullPath)) {
  32. $imageInfo = getimagesize($fullPath);
  33. if ($imageInfo) {
  34. return ['width' => $imageInfo[0], 'height' => $imageInfo[1]];
  35. }
  36. }
  37. return ['width' => 500, 'height' => 281]; // Valeurs par défaut
  38. }
  39. public function getCoreToolsList($category)
  40. {
  41. return $this->tools->getList($category);
  42. }
  43. public function getToolsTag($tag)
  44. {
  45. return $this->tools->getTag($tag);
  46. }
  47. public function getTagCategory($tag,$typeWebsite)
  48. {
  49. return $this->tools->getTagCategory($tag,$typeWebsite);
  50. }
  51. }