vendor/liip/imagine-bundle/Imagine/Filter/PostProcessor/AbstractPostProcessor.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the `liip/LiipImagineBundle` project.
  4. *
  5. * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6. *
  7. * For the full copyright and license information, please view the LICENSE.md
  8. * file that was distributed with this source code.
  9. */
  10. namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor;
  11. use Liip\ImagineBundle\Binary\BinaryInterface;
  12. use Liip\ImagineBundle\Binary\FileBinaryInterface;
  13. use Symfony\Component\Filesystem\Exception\IOException;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\Process\Process;
  16. abstract class AbstractPostProcessor implements PostProcessorInterface
  17. {
  18. /**
  19. * @var string
  20. */
  21. protected $executablePath;
  22. /**
  23. * @var string|null
  24. */
  25. protected $temporaryRootPath;
  26. /**
  27. * @var Filesystem
  28. */
  29. private $filesystem;
  30. public function __construct(string $executablePath, string $temporaryRootPath = null)
  31. {
  32. $this->executablePath = $executablePath;
  33. $this->temporaryRootPath = $temporaryRootPath;
  34. $this->filesystem = new Filesystem();
  35. }
  36. protected function createProcess(array $arguments = [], array $options = []): Process
  37. {
  38. $process = new Process($arguments);
  39. if (!isset($options['process'])) {
  40. return $process;
  41. }
  42. if (isset($options['process']['timeout'])) {
  43. $process->setTimeout($options['process']['timeout']);
  44. }
  45. if (isset($options['process']['working_directory'])) {
  46. $process->setWorkingDirectory($options['process']['working_directory']);
  47. }
  48. if (isset($options['process']['environment_variables']) && \is_array($options['process']['environment_variables'])) {
  49. $process->setEnv($options['process']['environment_variables']);
  50. }
  51. return $process;
  52. }
  53. protected function isBinaryTypeJpgImage(BinaryInterface $binary): bool
  54. {
  55. return $this->isBinaryTypeMatch($binary, ['image/jpeg', 'image/jpg']);
  56. }
  57. protected function isBinaryTypePngImage(BinaryInterface $binary): bool
  58. {
  59. return $this->isBinaryTypeMatch($binary, ['image/png']);
  60. }
  61. protected function isBinaryTypeMatch(BinaryInterface $binary, array $types): bool
  62. {
  63. return \in_array($binary->getMimeType(), $types, true);
  64. }
  65. protected function writeTemporaryFile(BinaryInterface $binary, array $options = [], string $prefix = null): string
  66. {
  67. $temporary = $this->acquireTemporaryFilePath($options, $prefix);
  68. if ($binary instanceof FileBinaryInterface) {
  69. $this->filesystem->copy($binary->getPath(), $temporary, true);
  70. } else {
  71. $this->filesystem->dumpFile($temporary, $binary->getContent());
  72. }
  73. return $temporary;
  74. }
  75. protected function acquireTemporaryFilePath(array $options, string $prefix = null): string
  76. {
  77. $root = $options['temp_dir'] ?? $this->temporaryRootPath ?: sys_get_temp_dir();
  78. if (!is_dir($root)) {
  79. try {
  80. $this->filesystem->mkdir($root);
  81. } catch (IOException $exception) {
  82. // ignore failure as "tempnam" function will revert back to system default tmp path as last resort
  83. }
  84. }
  85. if (false === $file = @tempnam($root, $prefix ?: 'post-processor')) {
  86. throw new \RuntimeException(sprintf('Temporary file cannot be created in "%s"', $root));
  87. }
  88. return $file;
  89. }
  90. /**
  91. * @param int[] $validReturns
  92. * @param string[] $errors
  93. */
  94. protected function isSuccessfulProcess(Process $process, array $validReturns = [0], array $errors = ['ERROR']): bool
  95. {
  96. if (\count($validReturns) > 0 && !\in_array($process->getExitCode(), $validReturns, true)) {
  97. return false;
  98. }
  99. foreach ($errors as $string) {
  100. if (false !== mb_strpos($process->getOutput(), $string)) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. protected function triggerSetterMethodDeprecation(string $method): void
  107. {
  108. @trigger_error(sprintf('The %s() method was deprecated in 2.2 and will be removed in 3.0. You must '
  109. .'setup the class state via its __construct() method. You can still pass filter-specific options to the '.
  110. 'process() method to overwrite behavior.', $method), E_USER_DEPRECATED);
  111. }
  112. }