MimetypeIconService.php (1157B)
1 <?php 2 3 namespace PartKeepr\MimetypeIconsBundle\Services; 4 5 use Symfony\Component\Config\FileLocator; 6 use Symfony\Component\DependencyInjection\Container; 7 use Symfony\Component\DependencyInjection\ContainerAware; 8 9 class MimetypeIconService extends ContainerAware 10 { 11 /** 12 * @var Container 13 */ 14 protected $container; 15 16 public function __construct(Container $container) 17 { 18 $this->setContainer($container); 19 } 20 21 /** 22 * Returns the file path to an image file which represents the passed mimetype. 23 * 24 * @param string $mimetype The mimetype 25 * 26 * @return string A path to the mimetype icon 27 */ 28 public function getMimetypeIcon($mimetype) 29 { 30 $file = str_replace('/', '-', $mimetype).'.svg'; 31 32 $iconDirectory = $this->container->getParameter('partkeepr.directories.mimetype_icons'); 33 34 $fileLocator = new FileLocator($iconDirectory); 35 36 try { 37 $iconFile = $fileLocator->locate($file); 38 } catch (\InvalidArgumentException $e) { 39 $file = 'empty.svg'; 40 $iconFile = $fileLocator->locate($file); 41 } 42 43 return $iconFile; 44 } 45 }