UrlAnalyzer.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Troubleshooting\Helper;
  7. /**
  8. * Url analyzer helper for the config file.
  9. */
  10. class UrlAnalyzer
  11. {
  12. /**
  13. * Fix url if it does not have "/" at the end.
  14. *
  15. * @param string $key
  16. * @return array
  17. */
  18. public function fixLastSlash($key)
  19. {
  20. $message = [];
  21. $param = sprintf('%s" value="%s', $key, $_ENV[$key]);
  22. $fileContents = file_get_contents(MTF_PHPUNIT_FILE);
  23. $lastSymbol = substr($param, -1);
  24. if ($lastSymbol != '/') {
  25. $_ENV[$key] = $_ENV[$key] . '/';
  26. $fileContents = str_replace($param, $param . '/', $fileContents);
  27. file_put_contents(MTF_PHPUNIT_FILE, $fileContents);
  28. $message['info'][] = "Slash at the end of url was added in the config file.";
  29. }
  30. return $message;
  31. }
  32. /**
  33. * Add/remove 'index.php' as a part of url if needed.
  34. *
  35. * @param string $url
  36. * @return array
  37. */
  38. public function resolveIndexPhpProblem($url)
  39. {
  40. $fileContents = file_get_contents(MTF_PHPUNIT_FILE);
  41. $pattern = '/(backend_url.*?=")(.+)"/';
  42. $replacement = "$1{$url}\"";
  43. $fileContents = preg_replace($pattern, $replacement, $fileContents);
  44. file_put_contents(MTF_PHPUNIT_FILE, $fileContents);
  45. return ['info' => ['"app_backend_url" has been updated in the phpunit.xml.']];
  46. }
  47. /**
  48. * Check if url has subdomains.
  49. *
  50. * @param string $url
  51. * @return array
  52. */
  53. public function checkDomain($url)
  54. {
  55. $messages = [];
  56. $pattern = '/([-%\w]*?\.\w+)/';
  57. if (preg_match($pattern, $url) === false) {
  58. $messages['error'][] =
  59. 'Instance should have domain name with at least one subdomain to function correctly. Examples:'
  60. . PHP_EOL . "\tValid: http://magento.dev/, https://mage.local/."
  61. . PHP_EOL . "\tInvalid: http://localhost/, https://magento/.";
  62. }
  63. return $messages;
  64. }
  65. }