UrlConverter.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\storage\helpers;
  8. class UrlConverter
  9. {
  10. public $replaceUrl;
  11. public $region;
  12. public function __construct($replaceUrl = '', $region = '')
  13. {
  14. $this->replaceUrl = $replaceUrl;
  15. $this->region = $region;
  16. }
  17. public function __invoke($objectUrl, $saveTo, $driver)
  18. {
  19. if ($this->replaceUrl == '') {
  20. return $objectUrl;
  21. }
  22. if ($this->region != '') {
  23. $objectUrl = str_replace($this->region, '', $objectUrl);
  24. }
  25. return static::replaceUrl($objectUrl, $this->replaceUrl);
  26. }
  27. public static function replaceUrl($url, $replaceTo)
  28. {
  29. $urlParts = parse_url($url);
  30. $replaceParts = parse_url($replaceTo);
  31. $finalParts = array_merge($urlParts, $replaceParts);
  32. return static::buildUrl($finalParts);
  33. }
  34. public static function buildUrl($parts)
  35. {
  36. return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '') . ((isset($parts['user']) || isset($parts['host'])) ? '//' : '') . (isset($parts['user']) ? "{$parts['user']}" : '') . (isset($parts['pass']) ? ":{$parts['pass']}" : '') . (isset($parts['user']) ? '@' : '') . (isset($parts['host']) ? "{$parts['host']}" : '') . (isset($parts['port']) ? ":{$parts['port']}" : '') . (isset($parts['path']) ? "{$parts['path']}" : '') . (isset($parts['query']) ? "?{$parts['query']}" : '') . (isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
  37. }
  38. }