HtaccessAnalyzer.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Troubleshooting;
  7. use Magento\Mtf\ObjectManagerInterface;
  8. use Magento\Mtf\Util\Protocol\CurlInterface;
  9. use Magento\Mtf\Util\Protocol\CurlTransport;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. /**
  13. * Checks if .htaccess is identical to .htaccess.sample.
  14. */
  15. class HtaccessAnalyzer extends \Symfony\Component\Console\Command\Command
  16. {
  17. /**
  18. * Object manager instance.
  19. *
  20. * @var ObjectManagerInterface
  21. */
  22. private $objectManager;
  23. /**
  24. * Command path.
  25. *
  26. * @var string
  27. */
  28. private $commandPath = 'dev/tests/functional/utils/command.php?command=';
  29. /**
  30. * HTTP CURL Adapter.
  31. *
  32. * @var CurlTransport
  33. */
  34. private $curl;
  35. /**
  36. * @param ObjectManagerInterface $objectManager
  37. * @param CurlTransport $curl
  38. */
  39. public function __construct(
  40. ObjectManagerInterface $objectManager,
  41. CurlTransport $curl
  42. ) {
  43. parent::__construct();
  44. $this->objectManager = $objectManager;
  45. $this->curl = $curl;
  46. }
  47. /**
  48. * Configure command.
  49. *
  50. * @return void
  51. */
  52. protected function configure()
  53. {
  54. parent::configure();
  55. $this->setName('troubleshooting:check-htaccess')
  56. ->setDescription('Check .htaccess file is present. It is needed to run cli commands via browser url.');
  57. }
  58. /**
  59. * Execute command.
  60. *
  61. * @param InputInterface $input
  62. * @param OutputInterface $output
  63. * @return void
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. */
  66. protected function execute(InputInterface $input, OutputInterface $output)
  67. {
  68. \PHPUnit\Util\Configuration::getInstance(MTF_PHPUNIT_FILE)->handlePHPConfiguration();
  69. $output = $this->objectManager->create(
  70. \Magento\Mtf\Console\Output::class,
  71. ['output' => $output]
  72. );
  73. try {
  74. $output->writeln("Checking .htaccess file...");
  75. $this->curl->write($_ENV['app_frontend_url'] . $this->commandPath, [], CurlInterface::GET);
  76. $this->curl->read();
  77. $responseCode = $this->curl->getInfo(CURLINFO_HTTP_CODE);
  78. if ($responseCode != 200) {
  79. $message['error'][] = 'Your .htaccess file doesn\'t exist. '
  80. . 'Please, create it from to .htaccess.sample.';
  81. $output->outputMessages($message);
  82. }
  83. $this->curl->close();
  84. } catch (\Exception $e) {
  85. $output->outputMessages(['error' => [$e->getMessage()]]);
  86. }
  87. $output->writeln(".htaccess check finished.");
  88. }
  89. }