LintManager.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS;
  11. use Symfony\Component\Process\Process;
  12. use Symfony\Component\Process\ProcessUtils;
  13. /**
  14. * Handle PHP code linting process.
  15. *
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. */
  18. class LintManager
  19. {
  20. /**
  21. * Temporary file for code linting.
  22. *
  23. * @var string|null
  24. */
  25. private $temporaryFile;
  26. public function __destruct()
  27. {
  28. if (null !== $this->temporaryFile) {
  29. unlink($this->temporaryFile);
  30. }
  31. }
  32. /**
  33. * Create process that lint PHP file.
  34. *
  35. * @param string $path path to file
  36. *
  37. * @return Process
  38. */
  39. public function createProcessForFile($path)
  40. {
  41. // in case php://stdin
  42. if (!is_file($path)) {
  43. return $this->createProcessForSource(file_get_contents($path));
  44. }
  45. $process = new Process('php -l '.ProcessUtils::escapeArgument($path));
  46. $process->setTimeout(null);
  47. $process->run();
  48. return $process;
  49. }
  50. /**
  51. * Create process that lint PHP code.
  52. *
  53. * @param string $source code
  54. *
  55. * @return Process
  56. */
  57. public function createProcessForSource($source)
  58. {
  59. if (null === $this->temporaryFile) {
  60. $this->temporaryFile = tempnam('.', 'tmp');
  61. }
  62. file_put_contents($this->temporaryFile, $source);
  63. return $this->createProcessForFile($this->temporaryFile);
  64. }
  65. }