Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Http/Stream.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan\Http;
 12: 
 13: use Psr\Http\Message\StreamInterface;
 14: 
 15: /**
 16:  * Data stream class (PSR-7).
 17:  *
 18:  * @author Goragod Wiriya <admin@goragod.com>
 19:  *
 20:  * @since 1.0
 21:  */
 22: class Stream implements StreamInterface
 23: {
 24:     /**
 25:      * stream metadata.
 26:      *
 27:      * @var array
 28:      */
 29:     protected $meta;
 30:     /**
 31:      * stream readable.
 32:      *
 33:      * @var bool
 34:      */
 35:     protected $readable;
 36:     /**
 37:      * stream seekable.
 38:      *
 39:      * @var bool
 40:      */
 41:     protected $seekable;
 42:     /**
 43:      * stream size.
 44:      *
 45:      * @var null|int
 46:      */
 47:     protected $size;
 48:     /**
 49:      * stream resource.
 50:      *
 51:      * @var resource
 52:      */
 53:     protected $stream;
 54:     /**
 55:      * stream writable.
 56:      *
 57:      * @var bool
 58:      */
 59:     protected $writable;
 60: 
 61:     /**
 62:      * Create a new Stream.
 63:      *
 64:      * @param resource $stream
 65:      * @param string   $mode
 66:      *
 67:      * @throws InvalidArgumentException $stream ไม่ใช่ resource
 68:      */
 69:     public function __construct($stream, $mode = 'r')
 70:     {
 71:         if (is_string($stream)) {
 72:             set_error_handler(function ($e) use (&$error) {
 73:                 $error = $e;
 74:             }, E_WARNING);
 75:             $stream = fopen($stream, $mode);
 76:             restore_error_handler();
 77:         }
 78:         if (!is_resource($stream)) {
 79:             throw new \InvalidArgumentException('Stream must be a resource');
 80:         }
 81:         $this->stream = $stream;
 82:     }
 83: 
 84:     /**
 85:      * อ่านข้อมูลทั้งหมดของ stream ส่งออกเป็น string.
 86:      *
 87:      * @return string
 88:      */
 89:     public function __toString()
 90:     {
 91:         if (is_resource($this->stream)) {
 92:             try {
 93:                 $this->rewind();
 94: 
 95:                 return $this->getContents();
 96:             } catch (\RuntimeException $e) {
 97:             }
 98:         }
 99: 
100:         return '';
101:     }
102: 
103:     /**
104:      * ยกเลิก stream คืนหน่วยความจำ.
105:      */
106:     public function close()
107:     {
108:         if (isset($this->stream)) {
109:             if (is_resource($this->stream)) {
110:                 fclose($this->stream);
111:             }
112:             $this->detach();
113:         }
114:     }
115: 
116:     /**
117:      * reset ข้อมูลของ Class กลับเป็นค่าเริ่มต้น
118:      * คืนค่า resource เดิม
119:      *
120:      * @return resource|null
121:      */
122:     public function detach()
123:     {
124:         $tmp = $this->stream;
125:         $this->meta = null;
126:         $this->readable = null;
127:         $this->seekable = null;
128:         $this->size = null;
129:         $this->stream = null;
130:         $this->writable = null;
131: 
132:         return $tmp;
133:     }
134: 
135:     /**
136:      * ตรวจสอบว่า pointer อยู่ที่จุดสุดท้ายของ stream หรือยัง
137:      * คืนค่า true ถ้าอยู่ที่จุดสิ้นสุดของไฟล์.
138:      *
139:      * @return bool
140:      */
141:     public function eof()
142:     {
143:         return is_resource($this->stream) ? feof($this->stream) : true;
144:     }
145: 
146:     /**
147:      * อ่านข้อมูลทั้งหมดจาก stream.
148:      *
149:      * @throws \RuntimeException ถ้าไม่สามารถอ่านได้
150:      *
151:      * @return string
152:      */
153:     public function getContents()
154:     {
155:         $contents = stream_get_contents($this->stream);
156:         if ($contents === false) {
157:             throw new \RuntimeException('Unable to read stream contents');
158:         }
159: 
160:         return $contents;
161:     }
162: 
163:     /**
164:      * อ่านข้อมูลประจำตัวของ stream
165:      * array คืนค่าข้อมูลทั้งหมด ถ้าไม่ระบุ $key
166:      * mixed คืนค่าข้อมูลจาก $key ที่กำหนด
167:      * null ไม่พบ $key หรือไม่ใช่ stream.
168:      *
169:      * @param string $key
170:      *
171:      * @return array|mixed|null
172:      */
173:     public function getMetadata($key = null)
174:     {
175:         if ($this->meta === null) {
176:             $this->meta = is_resource($this->stream) ? stream_get_meta_data($this->stream) : null;
177:         }
178:         if ($key === null) {
179:             return $this->meta;
180:         } else {
181:             return isset($this->meta[$key]) ? $this->meta[$key] : null;
182:         }
183:     }
184: 
185:     /**
186:      * อ่านขนาดของ stream
187:      * คืนค่าขนาดเป็น byte หรือ null ถ้าไม่รู้ขนาด.
188:      *
189:      * @return int|null
190:      */
191:     public function getSize()
192:     {
193:         if ($this->size === null) {
194:             if (is_resource($this->stream)) {
195:                 $stats = fstat($this->stream);
196:                 $this->size = isset($stats['size']) ? $stats['size'] : null;
197:             } else {
198:                 $this->size = null;
199:             }
200:         }
201: 
202:         return $this->size;
203:     }
204: 
205:     /**
206:      * ตรวจสอบว่าสามารถอ่านข้อมูล stream ได้หรือไม่
207:      * คืนค่า true ถ้าอ่านได้.
208:      *
209:      * @return bool
210:      */
211:     public function isReadable()
212:     {
213:         if ($this->readable === null) {
214:             $mode = $this->getMetadata('mode');
215:             $this->readable = $mode === null ? false : (strstr($mode, 'r') || strstr($mode, '+'));
216:         }
217: 
218:         return $this->readable;
219:     }
220: 
221:     /**
222:      * อ่านความสามารถในการกำหนดตำแหน่งของ pointer
223:      * คืนค่า true ถ้าสามารถ seek ได้.
224:      *
225:      * @return bool
226:      */
227:     public function isSeekable()
228:     {
229:         if ($this->seekable === null) {
230:             $this->seekable = $this->getMetadata('seekable');
231:         }
232: 
233:         return $this->seekable;
234:     }
235: 
236:     /**
237:      * ตรวจสอบว่าสามารถเขียน stream ได้หรือไม่
238:      * คืนค่า true ถ้าเขียนได้.
239:      *
240:      * @return bool
241:      */
242:     public function isWritable()
243:     {
244:         if ($this->writable === null) {
245:             $mode = $this->getMetadata('mode');
246:             $this->writable = $mode === null ? false : (strstr($mode, 'x') || strstr($mode, 'w') || strstr($mode, 'c') || strstr($mode, 'a') || strstr($mode, '+'));
247:         }
248: 
249:         return $this->writable;
250:     }
251: 
252:     /**
253:      * อ่านข้อมูล stream ตามจำนวนที่กำหนด.
254:      *
255:      * @param int $length จำนวนที่ต้องการ
256:      *
257:      * @throws \RuntimeException ถ้าไม่สามารถอ่านได้
258:      *
259:      * @return string
260:      */
261:     public function read($length)
262:     {
263:         $data = is_resource($this->stream) ? fread($this->stream, $length) : false;
264:         if ($data === false) {
265:             throw new \RuntimeException('Unable to read stream contents');
266:         }
267: 
268:         return $data;
269:     }
270: 
271:     /**
272:      * เลื่อน pointer ไปยังจุดเริ่มต้นของ stream.
273:      *
274:      * @throws \RuntimeException on failure
275:      */
276:     public function rewind()
277:     {
278:         return $this->seek(0);
279:     }
280: 
281:     /**
282:      * เลื่อน pointer ไปยังตำแหน่งที่กำหนด.
283:      *
284:      * @param int $offset ตำแหน่งของ pointer
285:      * @param int $whence
286:      *
287:      * @throws \RuntimeException on failure
288:      */
289:     public function seek($offset, $whence = SEEK_SET)
290:     {
291:         if (fseek($this->stream, $offset, $whence) === -1) {
292:             throw new \RuntimeException('Error seeking within stream');
293:         }
294:     }
295: 
296:     /**
297:      * คืนค่าตำแหน่งของ pointer ปัจจุบัน.
298:      *
299:      * @throws \RuntimeException on error
300:      *
301:      * @return int
302:      */
303:     public function tell()
304:     {
305:         $position = is_resource($this->stream) ? ftell($this->stream) : false;
306:         if ($position === false) {
307:             throw new \RuntimeException('Unable to determine stream position');
308:         }
309: 
310:         return $position;
311:     }
312: 
313:     /**
314:      * เขียนข้อมูลลงบน stream
315:      * คืนค่าจำนวน byte ที่เขียน.
316:      *
317:      * @param string $string ข้อมูลที่เขียน
318:      *
319:      * @throws \RuntimeException on failure
320:      *
321:      * @return int
322:      */
323:     public function write($string)
324:     {
325:         $result = is_resource($this->stream) ? fwrite($this->stream, $string) : false;
326:         if ($result === false) {
327:             throw new \RuntimeException('Unable to write to stream');
328:         } else {
329:             $this->size = null;
330:         }
331: 
332:         return $result;
333:     }
334: }
335: 
Kotchasan API documentation generated by ApiGen