1 <?php
2 /**
3 * @author wonli <wonli@live.com>
4 * StringToPHPStream.php
5 */
6
7 namespace Cross\Lib;
8
9 /**
10 * 字符串php代码通过wrapper转换为可以执行的php代码
11 * <pre>
12 * 使用方式 stream_register_wrapper("自定义名字", "stringToPHPStream")
13 * $var = include ("自定义名字://字符串代码")
14 * </pre>
15 *
16 * @author wonli <wonli@live.com>
17 * Class StringToPHPStream
18 * @package Cross\Lib\Other
19 */
20 class StringToPHPStream
21 {
22
23 /**
24 * 代码内容
25 *
26 * @var array
27 */
28 static $content;
29
30 /**
31 * 在$content中的标示
32 *
33 * @var string
34 */
35 protected $key;
36
37 /**
38 * @var int
39 */
40 protected $pos;
41
42 /**
43 * @param $path
44 * @param $mode
45 * @param $options
46 * @param $opened_path
47 * @return bool
48 */
49 public function stream_open($path, $mode, $options, $opened_path)
50 {
51 $this->key = md5($path);
52 if (!isset(self::$content[$this->key])) {
53 self::$content[$this->key] = sprintf('<?php return %s;', substr($path, 11));
54 }
55
56 $this->pos = 0;
57 return true;
58 }
59
60 /**
61 * @param $count
62 * @return string
63 */
64 public function stream_read($count)
65 {
66 $content = self::$content[$this->key];
67 $ret = substr($content, $this->pos, $count);
68 $this->pos += strlen($ret);
69 return $ret;
70 }
71
72 /**
73 *
74 */
75 public function stream_stat()
76 {
77
78 }
79
80 /**
81 *
82 */
83 public function stream_eof()
84 {
85
86 }
87 }
88
89
90