/src/Stagehand/Temp/Temp.php

https://github.com/piece/stagehand-temp · PHP · 169 lines · 66 code · 24 blank · 79 comment · 9 complexity · 0ea8fb7ab8815341a3ffdbfa7c93ba02 MD5 · raw file

  1. <?php
  2. /**
  3. * PHP version 5
  4. *
  5. * Copyright (c) 2011 KUMAKURA Yousuke <kumatch@gmail.com>,
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @package Stagehand_Temp
  30. * @copyright 2011 KUMAKURA Yousuke <kumatch@gmail.com>
  31. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  32. * @version Release: @package_version@
  33. * @since File available since Release 1.0.0
  34. */
  35. namespace Stagehand\Temp;
  36. use Stagehand\Temp\FileInfo;
  37. use Stagehand\Temp\Exception;
  38. /**
  39. * @package Stagehand_Temp
  40. * @copyright 2011 KUMAKURA Yousuke <kumatch@gmail.com>
  41. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  42. * @version Release: @package_version@
  43. * @since File available since Release 1.0.0
  44. */
  45. class Temp
  46. {
  47. /**
  48. * @var boolean
  49. */
  50. public static $autoDrop = true;
  51. /**
  52. * @var string
  53. */
  54. protected $_temporaryDirectory;
  55. /**
  56. * @var array
  57. */
  58. protected static $_temporaries = array();
  59. /**
  60. * Constructor
  61. *
  62. * @param $temporaryDirectory
  63. * @throws \Stagehand\Temp\Exception
  64. */
  65. public function __construct($temporaryDirectory = null)
  66. {
  67. if (is_null($temporaryDirectory)) {
  68. $this->_temporaryDirectory = sys_get_temp_dir();
  69. } else {
  70. $temporaryDirectory = realpath($temporaryDirectory);
  71. if (!$temporaryDirectory) {
  72. throw new Exception();
  73. }
  74. if (!file_exists($temporaryDirectory)
  75. || !is_dir($temporaryDirectory)
  76. ) {
  77. throw new Exception();
  78. }
  79. $this->_temporaryDirectory = $temporaryDirectory;
  80. }
  81. }
  82. /**
  83. * Open new temporary file.
  84. *
  85. * @param string $prefix
  86. * @return string
  87. */
  88. public function open($prefix = null)
  89. {
  90. $path = $this->_generateUniquePath($prefix);
  91. if (!touch($path)) {
  92. throw new Exception('Permission denied [' . $this->_temporaryDirectory . ']');
  93. }
  94. chmod($path, 0600);
  95. $tmp = new FileInfo($path);
  96. array_push(self::$_temporaries, $tmp);
  97. return $tmp;
  98. }
  99. /**
  100. * Make new temporary directory.
  101. *
  102. * @param string $prefix
  103. * @return string
  104. */
  105. public function mkdir($prefix = null)
  106. {
  107. $path = $this->_generateUniquePath($prefix);
  108. if (!mkdir($path, 0700, true)) {
  109. throw new Exception('Permission denied [' . $this->_temporaryDirectory . ']');
  110. }
  111. $tmp = new FileInfo($path);
  112. array_push(self::$_temporaries, $tmp);
  113. return $tmp;
  114. }
  115. /**
  116. * Generates a unique path.
  117. *
  118. * @param string $prefix
  119. * @return string
  120. */
  121. public function _generateUniquePath($prefix = null)
  122. {
  123. $unique = false;
  124. while (!$unique) {
  125. $path = $this->_temporaryDirectory . '/' . $this->_generateName($prefix);
  126. if (!file_exists($path)) {
  127. $unique = true;
  128. }
  129. }
  130. return $path;
  131. }
  132. /**
  133. * Generates a random name.
  134. *
  135. * @param string $prefix
  136. * @return string
  137. */
  138. protected function _generateName($prefix = null)
  139. {
  140. $a = base_convert(microtime(true), 10, 36);
  141. $b = base_convert((mt_rand() / mt_getrandmax()), 10, 36);
  142. $c = base_convert((mt_rand() / mt_getrandmax()), 10, 36);
  143. return $prefix . $a . $b . $c;
  144. }
  145. }