FeatureDetection.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * League.Uri (https://uri.thephpleague.com)
  4. *
  5. * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace League\Uri;
  12. use finfo;
  13. use League\Uri\Exceptions\MissingFeature;
  14. use League\Uri\IPv4\Calculator;
  15. use const PHP_INT_SIZE;
  16. /**
  17. * Allow detecting features needed to make the packages work.
  18. */
  19. final class FeatureDetection
  20. {
  21. public static function supportsFileDetection(): void
  22. {
  23. static $isSupported = null;
  24. $isSupported = $isSupported ?? class_exists(finfo::class);
  25. if (!$isSupported) {
  26. throw new MissingFeature('Support for file type detection requires the `fileinfo` extension.');
  27. }
  28. }
  29. public static function supportsIdn(): void
  30. {
  31. static $isSupported = null;
  32. $isSupported = $isSupported ?? (function_exists('\idn_to_ascii') && defined('\INTL_IDNA_VARIANT_UTS46'));
  33. if (!$isSupported) {
  34. throw new MissingFeature('Support for IDN host requires the `intl` extension for best performance or run "composer require symfony/polyfill-intl-idn" to install a polyfill.');
  35. }
  36. }
  37. public static function supportsIPv4Conversion(): void
  38. {
  39. static $isSupported = null;
  40. $isSupported = $isSupported ?? (extension_loaded('gmp') || extension_loaded('bcmath') || (4 < PHP_INT_SIZE));
  41. if (!$isSupported) {
  42. throw new MissingFeature('A '.Calculator::class.' implementation could not be automatically loaded. To perform IPv4 conversion use a x.64 PHP build or install one of the following extension GMP or BCMath. You can also ship your own implmentation.');
  43. }
  44. }
  45. }