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