1<?php 2 3namespace ComboStrap; 4 5use ComboStrap\Web\Url; 6 7/** 8 * Static class of the fetcher system 9 */ 10class FetcherSystem 11{ 12 13 /** 14 * 15 * @param Url $fetchUrl 16 * @return IFetcherPath 17 * @throws ExceptionBadArgument 18 * @throws ExceptionNotFound 19 * @throws ExceptionInternal 20 */ 21 public static function createPathFetcherFromUrl(Url $fetchUrl): IFetcherPath 22 { 23 24 try { 25 $fetcherName = $fetchUrl->getQueryPropertyValue(IFetcher::FETCHER_KEY); 26 try { 27 $fetchers = ClassUtility::getObjectImplementingInterface(IFetcherPath::class); 28 } catch (\ReflectionException $e) { 29 throw new ExceptionInternal("We could read fetch classes via reflection Error: {$e->getMessage()}"); 30 } 31 foreach ($fetchers as $fetcher) { 32 /** 33 * @var IFetcherPath $fetcher 34 */ 35 if ($fetcher->getFetcherName() === $fetcherName) { 36 $fetcher->buildFromUrl($fetchUrl); 37 return $fetcher; 38 } 39 } 40 } catch (ExceptionNotFound $e) { 41 // no fetcher property 42 } 43 44 45 try { 46 $fetchDoku = FetcherRawLocalPath::createLocalFromFetchUrl($fetchUrl); 47 $dokuPath = $fetchDoku->getSourcePath(); 48 } catch (ExceptionBadArgument $e) { 49 throw new ExceptionNotFound("No fetcher could be matched to the url ($fetchUrl)"); 50 } 51 try { 52 $mime = FileSystems::getMime($dokuPath); 53 } catch (ExceptionNotFound $e) { 54 LogUtility::warning("Warning: The mime is unknown for the path ($dokuPath).", LogUtility::SUPPORT_CANONICAL, $e); 55 $mime = new Mime(Mime::BINARY_MIME); 56 } 57 switch ($mime->toString()) { 58 case Mime::SVG: 59 return FetcherSvg::createSvgFromFetchUrl($fetchUrl); 60 default: 61 if ($mime->isImage()) { 62 return FetcherRaster::createRasterFromFetchUrl($fetchUrl); 63 } else { 64 return $fetchDoku; 65 } 66 } 67 68 } 69 70 /** 71 * @throws ExceptionInternal - if we can't reflect the class 72 * @throws ExceptionNotFound - if the fetcher is unknown 73 * @throws ExceptionBadArgument - if the fetcher is not set 74 */ 75 public static function createFetcherStringFromUrl(Url $fetchUrl): IFetcherString 76 { 77 78 try { 79 $fetcherName = $fetchUrl->getQueryPropertyValue(IFetcher::FETCHER_KEY); 80 } catch (ExceptionNotFound $e) { 81 throw new ExceptionBadArgument("No fetcher name found"); 82 } 83 try { 84 $fetchers = ClassUtility::getObjectImplementingInterface(IFetcherString::class); 85 } catch (\ReflectionException $e) { 86 throw new ExceptionInternal("We could read fetch classes via reflection Error: {$e->getMessage()}"); 87 } 88 foreach ($fetchers as $fetcher) { 89 /** 90 * @var IFetcherString $fetcher 91 */ 92 if ($fetcher->getFetcherName() === $fetcherName) { 93 $fetcher->buildFromUrl($fetchUrl); 94 return $fetcher; 95 } 96 } 97 throw new ExceptionNotFound("No fetcher found with the name ($fetcherName)"); 98 99 } 100 101} 102