Temporary.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\File\Temporary;
  36. use Hoa\Consistency;
  37. use Hoa\File;
  38. use Hoa\Stream;
  39. /**
  40. * Class \Hoa\File\Temporary.
  41. *
  42. * Temporary file handler.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Temporary extends File
  48. {
  49. /**
  50. * Temporary file index.
  51. *
  52. * @var int
  53. */
  54. private static $_i = 0;
  55. /**
  56. * Open a temporary file.
  57. *
  58. * @param string $streamName Stream name (or file descriptor).
  59. * @param string $mode Open mode, see the parent::MODE_*
  60. * constants.
  61. * @param string $context Context ID (please, see the
  62. * \Hoa\Stream\Context class).
  63. * @param bool $wait Differ opening or not.
  64. */
  65. public function __construct(
  66. $streamName,
  67. $mode,
  68. $context = null,
  69. $wait = false
  70. ) {
  71. if (null === $streamName) {
  72. $streamName = 'hoa://Library/File/Temporary.php#' . self::$_i++;
  73. }
  74. parent::__construct($streamName, $mode, $context, $wait);
  75. return;
  76. }
  77. /**
  78. * Open the stream and return the associated resource.
  79. *
  80. * @param string $streamName Stream name (here, it is
  81. * null).
  82. * @param \Hoa\Stream\Context $context Context.
  83. * @return resource
  84. * @throws \Hoa\File\Exception
  85. */
  86. protected function &_open($streamName, Stream\Context $context = null)
  87. {
  88. if (false === $out = @tmpfile()) {
  89. throw new File\Exception(
  90. 'Failed to open a temporary stream.',
  91. 0
  92. );
  93. }
  94. return $out;
  95. }
  96. /**
  97. * Create a unique temporary file, i.e. a file with a unique filename. It is
  98. * different of calling $this->__construct() that will create a temporary
  99. * file that will be destroy when calling the $this->close() method.
  100. *
  101. * @param string $directory Directory where the temporary filename
  102. * will be created. If the directory does not
  103. * exist, it may generate a file in the
  104. * system's temporary directory.
  105. * @param string $prefix Prefix of the generated temporary
  106. * filename.
  107. * @return string
  108. */
  109. public static function create($directory = null, $prefix = '__hoa_')
  110. {
  111. if (null === $directory ||
  112. false === is_dir($directory)) {
  113. $directory = static::getTemporaryDirectory();
  114. }
  115. return tempnam($directory, $prefix);
  116. }
  117. /**
  118. * Get the directory path used for temporary files.
  119. *
  120. * @return string
  121. */
  122. public static function getTemporaryDirectory()
  123. {
  124. return sys_get_temp_dir();
  125. }
  126. }
  127. /**
  128. * Flex entity.
  129. */
  130. Consistency::flexEntity('Hoa\File\Temporary\Temporary');