TemporaryFileByteStream.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @author Romain-Geissler
  11. */
  12. class Swift_ByteStream_TemporaryFileByteStream extends Swift_ByteStream_FileByteStream
  13. {
  14. public function __construct()
  15. {
  16. $filePath = tempnam(sys_get_temp_dir(), 'FileByteStream');
  17. if (false === $filePath) {
  18. throw new Swift_IoException('Failed to retrieve temporary file name.');
  19. }
  20. parent::__construct($filePath, true);
  21. }
  22. public function getContent()
  23. {
  24. if (false === ($content = file_get_contents($this->getPath()))) {
  25. throw new Swift_IoException('Failed to get temporary file content.');
  26. }
  27. return $content;
  28. }
  29. public function __destruct()
  30. {
  31. if (file_exists($this->getPath())) {
  32. @unlink($this->getPath());
  33. }
  34. }
  35. public function __sleep()
  36. {
  37. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  38. }
  39. public function __wakeup()
  40. {
  41. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  42. }
  43. }