跳转到主要内容
云客 提交于 1 November 2016

在开始这个主题前我们做一个实验,在你的drupal8模块控制器中加一行代码:

file_put_contents("public://yunke.txt","Streams test");

然后访问这个控制器,看看发生了什么?没错页面上不会有什么改变,但也没有报告什么错误,那这行代码到底干了什么?

作为开发者你应该很熟悉file_put_contents()这个函数,代码意思是将"Streams test"这个字符串写入一个文件中,

可是文件名却是:"public://yunke.txt",它是什么意思?文件保存了吗?保存到哪里去了?

这就是本主题要讲的内容!

不知道"public://yunke.txt",很正常,但你可能知道php://input,没错,就是那个php读取原始post输入的东西

在微信公众账号管理系统中就是用这个来读取收到的原始XML数据,其实public://yunke.txt和php://input差不多,它们源自同一个概念

就是php流及流包装器,由于在一般的开发中很少用到它,所以很多开发者并不熟悉,但它功能强大,是php这门语言的一个重点内容

在java中也有类似概念,如果你对C语言熟悉,那么会倍感熟悉,它或多或少基于ANSI C 标准IO函数簇。

由于php流这个知识点属于php学习的范畴,并不限于drupal源码分析,所以我单独写了篇博客来介绍它:

请看:http://blog.csdn.net/u011474028/article/details/52814049

那是本文的一部分,如果你对流不熟悉请务必暂停去阅读学习后再继续,否则你很难理解后面的内容,在文末附加一个流包装器的例子。

这里开始假定你已经学习完了流相关的知识,来看一看它在drupal8中的运用:

还是从file_put_contents("public://yunke.txt","Streams test");说起

它是在公共文件目录里面建立一个叫做yunke.txt的文件,里面保存了字符串"Streams test",那么这个公共文件目录在哪里?

它可以在站点配置文件中指定,如果你没有指定,那么默认是\sites\default\files,如果你按上面的方法做了,看一看是不是有这个文件?

drupal使用流这个概念来储存公有及私有文件并提供操作上的方便,私有文件可以保存到一个web访客无法通过url访问到的地方,这样就比较安全了

如何在settings.php文件中配置公有及私有文件的主目录:

$settings['file_public_path'] = "./yunke"; //所有公共文件都将保存到站点根目录的yunke文件夹下,若无默认是\sites\default\files $settings['file_private_path'] = “/home/yunke”; //设置后需要做权限设定,以防止web访客通过url访问到文件,配置中若有此项就会在容器中注册私有流包装器

关于流方面的源码储存在:\core\lib\Drupal\Core\StreamWrapper里面

定义了一个php级别的流包装器接口:PhpStreamWrapperInterface

drupal用的包装器接口(继承自上面的接口):StreamWrapperInterface

LocalStream:抽象的本地流包装器

额外定义了公共流包装器、私有流包装器、临时流包装器、只读流

这些包装器用一个包装器管理器来管理:

StreamWrapperManager:实现了StreamWrapperManagerInterface接口,主要作用是注册流包装器

调用流包装器管理器的代码位于drupal核心:Drupal\Core\DrupalKernel的preHandle方法中,有以下代码:

// Register stream wrappers.     $this->container->get('stream_wrapper_manager')->register();

而preHandle方法的调用是在多层HttpKernel核心处理中的预处理层: http_middleware.kernel_pre_handle(Drupal\Core\StackMiddleware\KernelPreHandle) 请看前面的《云客Drupal8源码分析之HttpKernel堆栈》一文

 

以上就是本主题的全部了,下面提供一个流包装器案例,是我从drupal8中抽取出来,做了修改,可以独立使用的一个类,可以研究下它的实现:

 

class yunkeWrapper
{
    public $context;
    public $handle = null;
    public $uri = null;
    protected static $dir = "";
    //public $dir = "dir";//可以是相对路径或绝对路径,空表示当前路径

    public function dir_closedir()
    {
        closedir($this->handle);
        // We do not really have a way to signal a failure as closedir() does not
        // have a return value.
        return true;
    }

    /**
     * @return bool
     */
    public function dir_opendir($uri, $options)
    {
        $this->uri = $uri;
        $this->handle = opendir($this->getLocalPath());
        return (bool)$this->handle;
    }

    /**
     * @return string
     */
    public function dir_readdir()
    {
        return readdir($this->handle);
    }

    /**
     * @return bool
     */
    public function dir_rewinddir()
    {
        rewinddir($this->handle);
        // We do not really have a way to signal a failure as rewinddir() does not
        // have a return value and there is no way to read a directory handler
        // without advancing to the next file.
        return true;
    }

    /**
     * @return bool
     */
    public function mkdir($uri, $mode, $options)
    {
        $this->uri = $uri;
        $recursive = (bool)($options & STREAM_MKDIR_RECURSIVE);
        if ($recursive) {
            // $this->getLocalPath() fails if $uri has multiple levels of directories
            // that do not yet exist.
            $localpath = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
        } else {
            $localpath = $this->getLocalPath($uri);
        }
        if ($options & STREAM_REPORT_ERRORS) {
            return mkdir($localpath, $mode, $recursive);
        } else {
            return @mkdir($localpath, $mode, $recursive);
        }
    }

    /**
     * @return bool
     */
    public function rename($from_uri, $to_uri)
    {
        return rename($this->getLocalPath($from_uri), $this->getLocalPath($to_uri));
    }

    /**
     * @return bool
     */
    public function rmdir($uri, $options)
    {
        $this->uri = $uri;
        if ($options & STREAM_REPORT_ERRORS) {
            return rmdir($this->getLocalPath());
        } else {
            return @rmdir($this->getLocalPath());
        }
    }

    /**
     * Retrieve the underlying stream resource.
     *
     * This method is called in response to stream_select().
     *
     * @param int $cast_as
     *   Can be STREAM_CAST_FOR_SELECT when stream_select() is calling
     *   stream_cast() or STREAM_CAST_AS_STREAM when stream_cast() is called for
     *   other uses.
     *
     * @return resource|false
     *   The underlying stream resource or FALSE if stream_select() is not
     *   supported.
     *
     * @see stream_select()
     * @see http://php.net/manual/streamwrapper.stream-cast.php
     */
    public function stream_cast($cast_as)
    {
        return $this->handle ? $this->handle : false;
    }

    /**
     * Closes stream.
     */
    public function stream_close()
    {
        return fclose($this->handle);
    }

    /**
     * @return bool
     */
    public function stream_eof()
    {
        return feof($this->handle);
    }

    /**
     * @return bool
     */
    public function stream_flush()
    {
        return fflush($this->handle);
    }

    /**
     * @return bool
     */
    public function stream_lock($operation)
    {
        if (in_array($operation, array(
            LOCK_SH,
            LOCK_EX,
            LOCK_UN,
            LOCK_NB))) {
            return flock($this->handle, $operation);
        }

        return true;
    }

    /**
     * Sets metadata on the stream.
     *
     * @param string $path
     *   A string containing the URI to the file to set metadata on.
     * @param int $option
     *   One of:
     *   - STREAM_META_TOUCH: The method was called in response to touch().
     *   - STREAM_META_OWNER_NAME: The method was called in response to chown()
     *     with string parameter.
     *   - STREAM_META_OWNER: The method was called in response to chown().
     *   - STREAM_META_GROUP_NAME: The method was called in response to chgrp().
     *   - STREAM_META_GROUP: The method was called in response to chgrp().
     *   - STREAM_META_ACCESS: The method was called in response to chmod().
     * @param mixed $value
     *   If option is:
     *   - STREAM_META_TOUCH: Array consisting of two arguments of the touch()
     *     function.
     *   - STREAM_META_OWNER_NAME or STREAM_META_GROUP_NAME: The name of the owner
     *     user/group as string.
     *   - STREAM_META_OWNER or STREAM_META_GROUP: The value of the owner
     *     user/group as integer.
     *   - STREAM_META_ACCESS: The argument of the chmod() as integer.
     *
     * @return bool
     *   Returns TRUE on success or FALSE on failure. If $option is not
     *   implemented, FALSE should be returned.
     *
     * @see http://php.net/manual/streamwrapper.stream-metadata.php
     */
    public function stream_metadata($uri, $option, $value)
    {
        $target = $this->getLocalPath($uri);
        $return = false;
        switch ($option) {
            case STREAM_META_TOUCH:
                if (!empty($value)) {
                    $return = touch($target, $value[0], $value[1]);
                } else {
                    $return = touch($target);
                }
                break;

            case STREAM_META_OWNER_NAME:
            case STREAM_META_OWNER:
                $return = chown($target, $value);
                break;

            case STREAM_META_GROUP_NAME:
            case STREAM_META_GROUP:
                $return = chgrp($target, $value);
                break;

            case STREAM_META_ACCESS:
                $return = chmod($target, $value);
                break;
        }
        if ($return) {
            // For convenience clear the file status cache of the underlying file,
            // since metadata operations are often followed by file status checks.
            clearstatcache(true, $target);
        }
        return $return;
    }

    /**
     * @return bool
     */
    public function stream_open($uri, $mode, $options, &$opened_path)
    {
        $this->uri = $uri;
        $path = $this->getLocalPath();
        $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @
            fopen($path, $mode);

        if ((bool)$this->handle && $options & STREAM_USE_PATH) {
            $opened_path = $path;
        }

        return (bool)$this->handle;
    }

    /**
     * @return string
     */
    public function stream_read($count)
    {
        return fread($this->handle, $count);
    }

    /**
     * Seeks to specific location in a stream.
     *
     * This method is called in response to fseek().
     *
     * The read/write position of the stream should be updated according to the
     * offset and whence.
     *
     * @param int $offset
     *   The byte offset to seek to.
     * @param int $whence
     *   Possible values:
     *   - SEEK_SET: Set position equal to offset bytes.
     *   - SEEK_CUR: Set position to current location plus offset.
     *   - SEEK_END: Set position to end-of-file plus offset.
     *   Defaults to SEEK_SET.
     *
     * @return bool
     *   TRUE if the position was updated, FALSE otherwise.
     *
     * @see http://php.net/manual/streamwrapper.stream-seek.php
     */
    public function stream_seek($offset, $whence = SEEK_SET)
    {
        // fseek returns 0 on success and -1 on a failure.
        // stream_seek   1 on success and  0 on a failure.
        return !fseek($this->handle, $offset, $whence);
    }

    /**
     * Change stream options.
     *
     * This method is called to set options on the stream.
     *
     * @param int $option
     *   One of:
     *   - STREAM_OPTION_BLOCKING: The method was called in response to
     *     stream_set_blocking().
     *   - STREAM_OPTION_READ_TIMEOUT: The method was called in response to
     *     stream_set_timeout().
     *   - STREAM_OPTION_WRITE_BUFFER: The method was called in response to
     *     stream_set_write_buffer().
     * @param int $arg1
     *   If option is:
     *   - STREAM_OPTION_BLOCKING: The requested blocking mode:
     *     - 1 means blocking.
     *     - 0 means not blocking.
     *   - STREAM_OPTION_READ_TIMEOUT: The timeout in seconds.
     *   - STREAM_OPTION_WRITE_BUFFER: The buffer mode, STREAM_BUFFER_NONE or
     *     STREAM_BUFFER_FULL.
     * @param int $arg2
     *   If option is:
     *   - STREAM_OPTION_BLOCKING: This option is not set.
     *   - STREAM_OPTION_READ_TIMEOUT: The timeout in microseconds.
     *   - STREAM_OPTION_WRITE_BUFFER: The requested buffer size.
     *
     * @return bool
     *   TRUE on success, FALSE otherwise. If $option is not implemented, FALSE
     *   should be returned.
     */
    public function stream_set_option($option, $arg1, $arg2)
    {
        trigger_error('stream_set_option() not supported for local file based stream wrappers',
            E_USER_WARNING);
        return false;
    }

    /**
     * @return array
     */
    public function stream_stat()
    {
        return fstat($this->handle);
    }

    /**
     * @return int
     */
    public function stream_tell()
    {
        return ftell($this->handle);
    }
    /**
     * Truncate stream.
     *
     * Will respond to truncation; e.g., through ftruncate().
     *
     * @param int $new_size
     *   The new size.
     *
     * @return bool
     *   TRUE on success, FALSE otherwise.
     */
    public function stream_truncate($new_size)
    {
        return ftruncate($this->handle, $new_size);
    }

    /**
     * @return int
     */
    public function stream_write($data)
    {
        return fwrite($this->handle, $data);
    }

    /**
     * @return bool
     */
    public function unlink($uri)
    {
        $this->uri = $uri;
        return unlink($this->getLocalPath());
    }

    /**
     * @return array
     */
    public function url_stat($uri, $flags)
    {
        $this->uri = $uri;
        $path = $this->getLocalPath();
        // Suppress warnings if requested or if the file or directory does not
        // exist. This is consistent with PHP's plain filesystem stream wrapper.
        if ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) {
            return @stat($path);
        } else {
            return stat($path);
        }
    }


    /****************************************************以上为php流类原型定义的方法,以下为自定义方法*****************************************/
    /**
     * 设置流包装器需要操作的主目录
     */
    public static function setDirectoryPath($dir = '')
    {
        if (empty($dir)) {
            self::$dir = "./";
        }
        if (!is_string($dir)) {
            trigger_error('DirectoryPath must be string', E_USER_WARNING);
        }
        self::$dir = $dir;
    }
    /**
     * 得到流包装器需要操作的主目录
     */
    public function getDirectoryPath()
    {
        self::$dir = empty(trim(self::$dir)) ? "./" : (self::$dir);
        /*
        if (!is_dir(self::$dir)) {
        mkdir(self::$dir, 0777, true);
        }
        //为了方便在实用程序中可以这样做,为标准起见不这样,建立目录不应该是工具库的责任
        */
        return self::$dir;
    }
    /**
     * 得到本地文件路径
     */
    protected function getLocalPath($uri = null)
    {
        if (!isset($uri)) {
            $uri = $this->uri;
        }
        $path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);

        // In PHPUnit tests, the base path for local streams may be a virtual
        // filesystem stream wrapper URI, in which case this local stream acts like
        // a proxy. realpath() is not supported by vfsStream, because a virtual
        // file system does not have a real filepath.
        if (strpos($path, 'vfs://') === 0) {
            return $path;
        }
        $realpath = realpath($path);
        if (!$realpath) {
            // This file does not yet exist.
            $realpath = realpath(dirname($path)) . '/' . basename($path);
        }
        $directory = realpath($this->getDirectoryPath());
        if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) {
            return false;
        }

        return $realpath;
    }

    /**
     * 得到目标
     */
    protected function getTarget($uri = null)
    {
        if (!isset($uri)) {
            $uri = $this->uri;
        }

        list(, $target) = explode('://', $uri, 2);

        // Remove erroneous leading or trailing, forward-slashes and backsl ashes.
        return trim($target, '\/');
    }

}

stream_wrapper_register("yunke", "yunkeWrapper"); //注册yunke流包装器
if (!is_dir("yunke://a/b")) {
    mkdir("yunke://a/b", 0777, true); //创建目录是开发者用户的责任
}
if (file_put_contents("yunke://a/b/wrapperTest.txt", "it is a test by:yunke time20161014") !== false) {
    echo "ok";
} else {
    echo "err";
}

$dir = "yunkenew/subdir"; //再次设定yunke流主目录
yunkeWrapper::setDirectoryPath($dir);
if (!is_dir($dir)) {
    mkdir($dir, 0777, true); //创建yunke流的主目录是开发者用户的责任
}
if (file_put_contents("yunke://wrapperTest.txt", "it is a test by:yunke time20161015") !== false) {
    echo "ok";
} else {
    echo "err";
}

【云游天下 做客四方】 我是云客,欢迎留言,进一步讨论请进群qq群203286137

 

允许转载,请注明出处,尊重分享者的劳动成果

 

 

 

 

Drupal 版本