Source of file FilesExtension.php

Size: 3,659 Bytes - Last Modified: 2023-11-16T22:56:02+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/twig-bundle/src/Extension/FilesExtension.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
<?php
namespace TIC\TwigBundle\Extension;

use TIC\TwigBundle\Base\TICTwigExtension as BaseExtension;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\TwigFilter;

/**
 * Filtres et fonctions twig de formattage et conversions.
 * https://symfony.com/doc/current/templating/twig_extension.html
 */
class FilesExtension extends BaseExtension
{
	private $appKernel;

	public function getFilters(): array
	{
		return [
			new TwigFilter('filePath',    [$this, 'filePathFilter']    ),
			new TwigFilter('fileContent', [$this, 'fileContentFilter'] ),
			new TwigFilter('b64image',    [$this, 'b64imageFilter']    ),
		];
	}

	public function __construct(KernelInterface $appKernel = null)
	{
		$this->appKernel = $appKernel;
	}

	/**
	 * Retourne le chemin absolu sur le serveur d'un emplacement relatif.
	 *
	 * @param  string   $filepath       Chemin relatif d'une ressource de bundle (commence par '@'),
	 *                                  ou depuis la racine du projet Symfony (commence par '/')
	 *                                  sinon depuis la racine web du projet (sous-dossier 'public' ?)
	 * @return string                   Chemin complet vers le fichier correspondant sur le serveur
	 */
	public function filePathFilter(string $filepath): ?string
	{
		try {
			switch (\substr($filepath, 0, 1)) {
				// recherche du chemin de la ressource si filepath commence par un '@'
				case '@' : return $this->appKernel->locateResource($filepath);
				// recherche du chemin complet du fichier relatif à la racine du projet
				case '/' : return $this->appKernel->getProjectDir() . $filepath;
				// recherche du chemin complet du fichier relatif à la racine web (public ?)
				default  :
					if (isset($_SERVER['SCRIPT_FILENAME'])) {
						$root = \dirname($_SERVER['SCRIPT_FILENAME']);
					} else {
						// TODO: si autre que './public' (paramètre web_dir ?)
						$root = $this->appKernel->getProjectDir() . '/public';
					}
					return $root . '/' . $filepath;
			}
		} catch (\Exception $e) {}
		return null;
	}

	/**
	 * Retourne le contenu d'un fichier.
	 *
	 * @param  string   $filepath       Chemin relatif d'une ressource de bundle (commence par '@'),
	 *                                  ou depuis la racine du projet Symfony (commence par '/')
	 *                                  sinon depuis la racine web du projet (sous-dossier 'public' ?)
	 * @return string                   Le contenu du fichier correspondant au chemin donné
	 */
	public function fileContentFilter($filepath): ?string
	{
		$fullpath = $this->filePathFilter($filepath);
		return (isset($fullpath) && \file_exists($fullpath)) ? \file_get_contents($fullpath) : null;
	}

	/**
	 * Retourne le contenu d'une image en base64 (avec l'entête 'data:' pour être embarqué dans une page html).
	 *
	 * @param  string   $filepath       Chemin relatif d'une ressource de bundle (commence par '@'),
	 *                                  ou depuis la racine du projet Symfony (commence par '/')
	 *                                  sinon depuis la racine web du projet (sous-dossier 'public' ?)
	 * @return string                   Données d'une image 'inline' en base64 à insérer dans un attribut 'src'
	 */
	public function b64imageFilter(string $filepath): string
	{
		$fullpath = $this->filePathFilter($filepath);
		
		// retourne l'image en base64 avec le préfixe 'data:' suivi du type mime
		if (isset($fullpath) && \file_exists($fullpath)) {
			$mime = \mime_content_type($fullpath);
			return \sprintf('data:%s;base64,', $mime) . \base64_encode(\file_get_contents($fullpath));
		}
		
		// à défaut, retourne le chemin demandé
		return $filepath;
	}

}