Класс streamWrapper

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅

ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Ρ‹Π²Π°Ρ‚ΡŒ собствСнныС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»ΠΎΠ² ΠΈ ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ² для дальнСйшСго использования со всСми функциями ΠΏΠΎ Ρ€Π°Π±ΠΎΡ‚Π΅ с Ρ„Π°ΠΉΠ»ΠΎΠ²ΠΎΠΉ систСмой (Ρ‚Π°ΠΊΠΈΠΌΠΈ ΠΊΠ°ΠΊ fopen(), fread() ΠΈ Ρ‚.ΠΏ.).

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π­Ρ‚ΠΎ НЕ Ρ€Π΅Π°Π»ΡŒΠ½Ρ‹ΠΉ класс, Π° лишь ΠΏΡ€ΠΎΡ‚ΠΎΡ‚ΠΈΠΏ, прСдоставлСнный Π² качСствС наглядного пособия.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

РСализация ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ², отличная ΠΎΡ‚ описанной здСсь, ΠΌΠΎΠΆΠ΅Ρ‚ привСсти ΠΊ Π½Π΅ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½ΠΎΠΌΡƒ повСдСнию.

ΠžΠ±ΡŠΠ΅ΠΊΡ‚ класса инициализируСтся Π² Ρ‚ΠΎΡ‚ ΠΌΠΎΠΌΠ΅Π½Ρ‚, ΠΊΠΎΠ³Π΄Π° потоковая функция пытаСтся ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ доступ ΠΊ ΠΏΡ€ΠΎΡ‚ΠΎΠΊΠΎΠ»Ρƒ, с ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ этот класс ассоциирован.

ΠžΠ±Π·ΠΎΡ€ класса

class streamWrapper {
/* Бвойства */
/* ΠœΠ΅Ρ‚ΠΎΠ΄Ρ‹ */
public function __construct()
public function dir_closedir(): bool
public function dir_opendir(string $path, int $options): bool
public function dir_readdir(): string|bool
public function dir_rewinddir(): bool
public function mkdir(string $path, int $mode, int $options): bool
public function rename(string $path_from, string $path_to): bool
public function rmdir(string $path, int $options): bool
public function stream_cast(int $cast_as): resource|false
public function stream_close(): void
public function stream_eof(): bool
public function stream_flush(): bool
public function stream_lock(int $operation): bool
public function stream_metadata(string $path, int $option, mixed $value): bool
public function stream_open(
Β Β Β Β string $path,
Β Β Β Β string $mode,
Β Β Β Β int $options,
Β Β Β Β ?string &$opened_path
): bool
public function stream_read(int $count): string|false
public function stream_seek(int $offset, int $whence): bool
public function stream_set_option(int $option, int $arg1, int $arg2): bool
public function stream_stat(): array|false
public function stream_tell(): int
public function stream_truncate(int $new_size): bool
public function stream_write(string $data): int
public function unlink(string $path): bool
public function url_stat(string $path, int $flags): array|false
public function __destruct()
}

Бвойства

resource context

Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ контСкст ΠΈΠ»ΠΈ null, Ссли Π² Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‰ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ Π½Π΅ Π±Ρ‹Π»ΠΎ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½ΠΎ Π½ΠΈΠΊΠ°ΠΊΠΎΠ³ΠΎ контСкста.

Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ stream_context_get_options() для Π°Π½Π°Π»ΠΈΠ·Π° ΠΈ Ρ€Π°Π·Π±ΠΎΡ€Π° контСкста.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π­Ρ‚ΠΎ свойство Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±Ρ‹Ρ‚ΡŒ общСдоступным (ΠΈΠΌΠ΅Ρ‚ΡŒ ΠΌΠΎΠ΄ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ public), Ρ‡Ρ‚ΠΎΠ±Ρ‹ PHP ΠΌΠΎΠ³ Π°ΡΡΠΎΡ†ΠΈΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ Π΅Π³ΠΎ с Π°ΠΊΡ‚ΡƒΠ°Π»ΡŒΠ½Ρ‹ΠΌ контСкстом.

Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠ°Π½ΠΈΠ΅

  • streamWrapper::__construct β€” Π‘ΠΎΠ·Π΄Π°Ρ‘Ρ‚ Π½ΠΎΠ²Ρ‹ΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠΈ ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::__destruct β€” Π£Π½ΠΈΡ‡Ρ‚ΠΎΠΆΠ°Π΅Ρ‚ ΡΡƒΡ‰Π΅ΡΡ‚Π²ΡƒΡŽΡ‰ΡƒΡŽ ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΡƒ ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::dir_closedir β€” Π—Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ дСскриптор Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ
  • streamWrapper::dir_opendir β€” ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ дСскриптор Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ
  • streamWrapper::dir_readdir β€” Π§Ρ‚Π΅Π½ΠΈΠ΅ записи ΠΈΠ· дСскриптора Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ
  • streamWrapper::dir_rewinddir β€” ДСскриптор Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ пСрСмСщСния Π½Π° Π΅Ρ‘ Π² Π½Π°Ρ‡Π°Π»ΠΎ
  • streamWrapper::mkdir β€” Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ
  • streamWrapper::rename β€” ΠŸΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Ρ‹Π²Π°Π΅Ρ‚ Ρ„Π°ΠΉΠ» ΠΈΠ»ΠΈ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΡŽ
  • streamWrapper::rmdir β€” УдаляСт Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΡŽ
  • streamWrapper::stream_cast β€” Π˜Π·Π²Π»Π΅ΠΊΠ°Π΅Ρ‚ Π½ΠΈΠ·ΠΊΠΎΡƒΡ€ΠΎΠ²Π½Π΅Π²Ρ‹ΠΉ дСскриптор
  • streamWrapper::stream_close β€” Π—Π°ΠΊΡ€Ρ‹Π²Π°Π΅Ρ‚ рСсурс
  • streamWrapper::stream_eof β€” ΠŸΡ€ΠΎΠ²Π΅Ρ€ΡΠ΅Ρ‚ достиТСниС ΠΊΠΎΠ½Ρ†Π° Ρ„Π°ΠΉΠ»Π° ΠΏΠΎ Ρ„Π°ΠΉΠ»ΠΎΠ²ΠΎΠΌΡƒ ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŽ
  • streamWrapper::stream_flush β€” БбрасываСт Π²Ρ‹Π²ΠΎΠ΄
  • streamWrapper::stream_lock β€” ΠšΠΎΠ½ΡΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΈΠ²Π½Π°Ρ Π±Π»ΠΎΠΊΠΈΡ€ΠΎΠ²ΠΊΠ° Ρ„Π°ΠΉΠ»Π°
  • streamWrapper::stream_metadata β€” Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ ΠΌΠ΅Ρ‚Π°Π΄Π°Π½Π½Ρ‹Π΅ ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::stream_open β€” ΠžΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅Ρ‚ Ρ„Π°ΠΉΠ» ΠΈΠ»ΠΈ URL
  • streamWrapper::stream_read β€” Π§ΠΈΡ‚Π°Π΅Ρ‚ ΠΈΠ· ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::stream_seek β€” ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°Π΅Ρ‚ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»ΠΎΠ²ΠΎΠ³ΠΎ указатСля Π² ΠΏΠΎΡ‚ΠΎΠΊΠ΅
  • streamWrapper::stream_set_option β€” ИзмСнСниС настроСк ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::stream_stat β€” ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ ΠΎ Ρ„Π°ΠΉΠ»ΠΎΠ²ΠΎΠΌ рСсурсС
  • streamWrapper::stream_tell β€” ΠžΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::stream_truncate β€” УсСчСниС ΠΏΠΎΡ‚ΠΎΠΊΠ°
  • streamWrapper::stream_write β€” Π—Π°ΠΏΠΈΡΡŒ Π² ΠΏΠΎΡ‚ΠΎΠΊ
  • streamWrapper::unlink β€” Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π°
  • streamWrapper::url_stat β€” ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ ΠΎ Ρ„Π°ΠΉΠ»Π΅
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
14
isaac dot z dot no dot foster at spam dot gmail dot please dot com ΒΆ
15 years ago
It's worth noting that the interface defined by yannick at gmail should not always be implemented by a stream wrapper class, as several of the methods should not be implemented if the class has no use for them (as per the manual). 

Specifically, mkdir, rename, rmdir, and unlink are methods that "should not be defined" if the wrapper has no use for them. The consequence is that the appropriate error message will not be returned. 

If the interface is implemented, you won't have the flexibility to not implement those methods.

Not trying to be academic, but it was useful for me.
up
3
Anonymous ΒΆ
14 years ago
Here is a very simple stream wrapper which calls your callback function for reads:

<?php

class CallbackUrl
{
    const WRAPPER_NAME = 'callback';

    public $context;
    private $_cb;
    private $_eof = false;

    private static $_isRegistered = false;

    public static function getContext($cb)
    {
        if (!self::$_isRegistered) {
            stream_wrapper_register(self::WRAPPER_NAME, get_class());
            self::$_isRegistered = true;
        }
        if (!is_callable($cb)) return false;
        return stream_context_create(array(self::WRAPPER_NAME => array('cb' => $cb)));
    }

    public function stream_open($path, $mode, $options, &$opened_path)
    {
        if (!preg_match('/^r[bt]?$/', $mode) || !$this->context) return false;
        $opt = stream_context_get_options($this->context);
        if (!is_array($opt[self::WRAPPER_NAME]) ||
            !isset($opt[self::WRAPPER_NAME]['cb']) ||
            !is_callable($opt[self::WRAPPER_NAME]['cb'])) return false;
        $this->_cb = $opt[self::WRAPPER_NAME]['cb'];
        return true;
    }

    public function stream_read($count)
    {
        if ($this->_eof || !$count) return '';
        if (($s = call_user_func($this->_cb, $count)) == '') $this->_eof = true;
        return $s;
    }

    public function stream_eof()
    {
        return $this->_eof;
    }
}

class Test {
    private $_s;
    public function __construct($s)
    {
        $this->_s = $s;
    }
    public function read($count) {
        return fread($this->_s, $count);
    }
}

$t = new Test(fopen('/etc/services', 'r'));
$fd = fopen('callback://', 'r', false, CallbackUrl::getContext(array($t, 'read')));
while(($buf = fread($fd, 128)) != '') {
    print $buf;
}
?>
up
1
info at ensostudio dot ru ΒΆ
5 years ago
THIS METHODS NOT REQUIRED, you can implement only part of their: directories, files, etc. 
For example, "glob://" support minimal syntax, glob() more powerful, you can replace/extend native wrapper: check options in table https://www.php.net/manual/ru/wrappers.glob , you need create wrapper only with 'dir_...dir' methods. For more info, see https://www.php.net/manual/en/class.globiterator.php#125220
up
2
yannick dot battail at gmail dot com ΒΆ
16 years ago
a php interface for wrapper

<?php
interface WrapperInterface
{
    /**
     * resource context
     *
     * @var resource
     */
    //public $context;

    /**
     * constructor
     *
     */
    public function __construct();

    /**
     *
     *
     * @return bool
     */
    public function dir_closedir();

    /**
     * Enter description here...
     *
     * @param string $path
     * @param int $options
     * @return bool
     */
    public function dir_opendir($path , $options);

    /**
     * Enter description here...
     *
     * @return string
     */
    public function dir_readdir();

    /**
     * Enter description here...
     *
     * @return bool
     */
    public function dir_rewinddir();

    /**
     * Enter description here...
     *
     * @param string $path
     * @param int $mode
     * @param int $options
     * @return bool
     */
    public function mkdir($path , $mode , $options);

    /**
     * Enter description here...
     *
     * @param string $path_from
     * @param string $path_to
     * @return bool
     */
    public function rename($path_from , $path_to);

    /**
     * Enter description here...
     *
     * @param string $path
     * @param int $options
     * @return bool
     */
    public function rmdir($path , $options);

    /**
     * Enter description here...
     *
     * @param int $cast_as
     * @return resource
     */
    public function stream_cast($cast_as);

    /**
     * Enter description here...
     *
     */
    public function stream_close();

    /**
     * Enter description here...
     *
     * @return bool
     */
    public function stream_eof();

    /**
     * Enter description here...
     *
     * @return bool
     */
    public function stream_flush();

    /**
     * Enter description here...
     *
     * @param mode $operation
     * @return bool
     */
    public function stream_lock($operation);

    /**
     * Enter description here...
     *
     * @param string $path
     * @param string $mode
     * @param int $options
     * @param string &$opened_path
     * @return bool
     */
    public function stream_open($path , $mode , $options , &$opened_path);

    /**
     * Enter description here...
     *
     * @param int $count
     * @return string
     */
    public function stream_read($count);

    /**
     * Enter description here...
     *
     * @param int $offset
     * @param int $whence = SEEK_SET
     * @return bool
     */
    public function stream_seek($offset , $whence = SEEK_SET);

    /**
     * Enter description here...
     *
     * @param int $option
     * @param int $arg1
     * @param int $arg2
     * @return bool
     */
    public function stream_set_option($option , $arg1 , $arg2);

    /**
     * Enter description here...
     *
     * @return array
     */
    public function stream_stat();

    /**
     * Enter description here...
     *
     * @return int
     */
    public function stream_tell();

    /**
     * Enter description here...
     *
     * @param string $data
     * @return int
     */
    public function stream_write($data);

    /**
     * Enter description here...
     *
     * @param string $path
     * @return bool
     */
    public function unlink($path);

    /**
     * Enter description here...
     *
     * @param string $path
     * @param int $flags
     * @return array
     */
    public function url_stat($path , $flags);
}

?>