FeatureDetection.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 function class_exists;
  16. use function defined;
  17. use function extension_loaded;
  18. use function function_exists;
  19. use const PHP_INT_SIZE;
  20. /**
  21. * Allow detecting features needed to make the packages work.
  22. */
  23. final class FeatureDetection
  24. {
  25. public static function supportsFileDetection(): void
  26. {
  27. static $isSupported = null;
  28. $isSupported = $isSupported ?? class_exists(finfo::class);
  29. $isSupported || throw new MissingFeature('Support for file type detection requires the `fileinfo` extension.');
  30. }
  31. public static function supportsIdn(): void
  32. {
  33. static $isSupported = null;
  34. $isSupported = $isSupported ?? (function_exists('\idn_to_ascii') && defined('\INTL_IDNA_VARIANT_UTS46'));
  35. $isSupported || 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.');
  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. $isSupported || 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 implementation.');
  42. }
  43. public static function supportsDom(): void
  44. {
  45. static $isSupported = null;
  46. $isSupported = $isSupported ?? extension_loaded('dom');
  47. $isSupported || throw new MissingFeature('To use a DOM related feature, the DOM extension must be installed in your system.');
  48. }
  49. }