Просмотр файла vendor/neutron/temporary-filesystem/src/Neutron/TemporaryFilesystem/Manager.php

Размер файла: 3.95Kb
  1. <?php
  2.  
  3. /*
  4. * This file is part of TemporaryFilesystem.
  5. *
  6. * (c) Romain Neutron <imprec@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace Neutron\TemporaryFilesystem;
  13.  
  14. use Symfony\Component\Filesystem\Filesystem;
  15. use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
  16.  
  17. class Manager implements TemporaryFilesystemInterface
  18. {
  19. /** @var Filesystem */
  20. private $fs;
  21. /** @var TemporaryFilesystem */
  22. private $tmpFs;
  23. /** @var array */
  24. private $files = array();
  25.  
  26. const DEFAULT_SCOPE = '_tmp_fs_';
  27.  
  28. public function __construct(TemporaryFilesystemInterface $tmpFs, Filesystem $fs)
  29. {
  30. $this->fs = $fs;
  31. $this->tmpFs = $tmpFs;
  32.  
  33. register_shutdown_function(array($this, 'clean'), null, false);
  34. }
  35.  
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function createEmptyFile($basePath, $prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536)
  40. {
  41. $file = $this->tmpFs->createEmptyFile($basePath, $prefix, $suffix, $extension, $maxTry);
  42. $this->add($file, $prefix);
  43.  
  44. return $file;
  45. }
  46.  
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = self::DEFAULT_SCOPE)
  51. {
  52. $dir = $this->tmpFs->createTemporaryDirectory($mode, $maxTry, $prefix);
  53. $this->add($dir, $prefix);
  54.  
  55. return $dir;
  56. }
  57.  
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function createTemporaryFile($prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536)
  62. {
  63. $file = $this->tmpFs->createTemporaryFile($prefix, $suffix, $extension, $maxTry);
  64. $this->add($file, $prefix);
  65.  
  66. return $file;
  67. }
  68.  
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function createTemporaryFiles($quantity = 1, $prefix = self::DEFAULT_SCOPE, $suffix = null, $extension = null, $maxTry = 65536)
  73. {
  74. $files = $this->tmpFs->createTemporaryFiles($quantity, $prefix, $suffix, $extension, $maxTry);
  75. $this->add($files, $prefix);
  76.  
  77. return $files;
  78. }
  79.  
  80. /**
  81. * Adds file to be handled by the manager.
  82. *
  83. * @param string|array $files
  84. * @param string $scope
  85. *
  86. * @return Manager
  87. */
  88. public function add($files, $scope = self::DEFAULT_SCOPE)
  89. {
  90. if (!is_array($files)) {
  91. $files = array($files);
  92. }
  93. if ('' === trim($scope)) {
  94. $scope = self::DEFAULT_SCOPE;
  95. }
  96. if (!isset($this->files[$scope])) {
  97. $this->files[$scope] = array();
  98. }
  99.  
  100. $this->files[$scope] = array_merge($this->files[$scope], $files);
  101.  
  102. return $this;
  103. }
  104.  
  105. /**
  106. * Removes all managed files in a scope. If no scope provided, all scopes
  107. * are cleared.
  108. *
  109. * @param string $scope
  110. *
  111. * @return Manager
  112. *
  113. * @throws IOException
  114. */
  115. public function clean($scope = null, $throwException = true)
  116. {
  117. if (null !== $scope) {
  118. $this->cleanScope($scope, $throwException);
  119. } else {
  120. foreach ($this->files as $scope => $files) {
  121. $this->cleanScope($scope, $throwException);
  122. }
  123. }
  124.  
  125. return $this;
  126. }
  127.  
  128. /**
  129. * Factory for the Manager
  130. *
  131. * @return Manager
  132. */
  133. public static function create()
  134. {
  135. $fs = new Filesystem();
  136.  
  137. return new static(new TemporaryFilesystem($fs), $fs);
  138. }
  139.  
  140. private function cleanScope($scope, $throwException)
  141. {
  142. if (!isset($this->files[$scope])) {
  143. return;
  144. }
  145.  
  146. try {
  147. $this->fs->remove($this->files[$scope]);
  148. unset($this->files[$scope]);
  149. } catch (SfIOException $e) {
  150. unset($this->files[$scope]);
  151. if ($throwException) {
  152. throw new IOException('Unable to remove all the files', $e->getCode(), $e);
  153. }
  154. }
  155. }
  156. }