AbstractTarVersionProbe.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of Zippy.
  4. *
  5. * (c) Alchemy <info@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. */
  11. namespace Alchemy\Zippy\Adapter\VersionProbe;
  12. use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
  13. use Symfony\Component\Process\Process;
  14. abstract class AbstractTarVersionProbe implements VersionProbeInterface
  15. {
  16. private $isSupported;
  17. private $inflator;
  18. private $deflator;
  19. public function __construct(ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
  20. {
  21. $this->inflator = $inflator;
  22. $this->deflator = $deflator;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getStatus()
  28. {
  29. if (null !== $this->isSupported) {
  30. return $this->isSupported;
  31. }
  32. if (null === $this->inflator || null === $this->deflator) {
  33. return $this->isSupported = VersionProbeInterface::PROBE_NOTSUPPORTED;
  34. }
  35. $good = true;
  36. foreach (array($this->inflator, $this->deflator) as $builder) {
  37. /** @var Process $process */
  38. $process = $builder
  39. ->create()
  40. ->add('--version')
  41. ->getProcess();
  42. $process->run();
  43. if (!$process->isSuccessful()) {
  44. return $this->isSupported = VersionProbeInterface::PROBE_NOTSUPPORTED;
  45. }
  46. $lines = explode("\n", $process->getOutput(), 2);
  47. $good = false !== stripos($lines[0], $this->getVersionSignature());
  48. if (!$good) {
  49. break;
  50. }
  51. }
  52. $this->isSupported = $good ? VersionProbeInterface::PROBE_OK : VersionProbeInterface::PROBE_NOTSUPPORTED;
  53. return $this->isSupported;
  54. }
  55. /**
  56. * Returns the signature of inflator/deflator
  57. *
  58. * @return string
  59. */
  60. abstract protected function getVersionSignature();
  61. }