1 | <?php |
||
23 | class StreamMetaData |
||
24 | { |
||
25 | /** |
||
26 | * Mapping between array keys and properties |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $propertyMap = [ |
||
31 | 'stream_type' => 'streamType', |
||
32 | 'wrapper_type' => 'wrapperType', |
||
33 | 'wrapper_data' => 'wrapperData', |
||
34 | 'filters' => 'filterList', |
||
35 | 'uri' => 'uri', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * A label describing the underlying implementation of the stream. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $streamType; |
||
44 | |||
45 | /** |
||
46 | * A label describing the protocol wrapper implementation layered over the stream. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | public $wrapperType; |
||
51 | |||
52 | /** |
||
53 | * Wrapper-specific data attached to this stream. |
||
54 | * |
||
55 | * @var mixed |
||
56 | */ |
||
57 | public $wrapperData; |
||
58 | |||
59 | /** |
||
60 | * Array containing the names of any filters that have been stacked onto this stream. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | public $filterList; |
||
65 | |||
66 | /** |
||
67 | * The URI/filename associated with this stream. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public $uri; |
||
72 | |||
73 | /** |
||
74 | * Information about syntax tree |
||
75 | * |
||
76 | * @var Node[] |
||
77 | */ |
||
78 | public $syntaxTree; |
||
79 | |||
80 | /** |
||
81 | * List of source tokens |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | public $tokenStream = []; |
||
86 | |||
87 | /** |
||
88 | * Creates metadata object from stream |
||
89 | * |
||
90 | * @param resource $stream Instance of stream |
||
91 | * @param string $source Source code or null |
||
92 | * @throws \InvalidArgumentException for invalid stream |
||
93 | */ |
||
94 | 31 | public function __construct($stream, $source = null) |
|
95 | { |
||
96 | 31 | if (!is_resource($stream)) { |
|
97 | throw new InvalidArgumentException('Stream should be valid resource'); |
||
98 | } |
||
99 | 31 | $metadata = stream_get_meta_data($stream); |
|
100 | 31 | if (preg_match('/resource=(.+)$/', $metadata['uri'], $matches)) { |
|
101 | 6 | $metadata['uri'] = PathResolver::realpath($matches[1]); |
|
102 | } |
||
103 | 31 | foreach ($metadata as $key => $value) { |
|
104 | 31 | if (!isset(self::$propertyMap[$key])) { |
|
105 | 31 | continue; |
|
106 | } |
||
107 | 31 | $mappedKey = self::$propertyMap[$key]; |
|
108 | 31 | $this->$mappedKey = $value; |
|
109 | } |
||
110 | 31 | $this->syntaxTree = ReflectionEngine::parseFile($this->uri, $source); |
|
111 | 31 | $this->setSource($source); |
|
112 | 31 | } |
|
113 | |||
114 | /** |
||
115 | * @inheritDoc |
||
116 | */ |
||
117 | 31 | public function __get($name) |
|
125 | |||
126 | /** |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | public function __set($name, $value) |
||
130 | { |
||
131 | if ($name === 'source') { |
||
132 | trigger_error('Setting StreamMetaData->source is deprecated, use tokenStream instead', E_USER_DEPRECATED); |
||
133 | $this->setSource($value); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns source code directly from tokens |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 31 | private function getSource() |
|
143 | { |
||
144 | 31 | $transformedSource = ''; |
|
145 | 31 | foreach ($this->tokenStream as $token) { |
|
146 | 31 | $transformedSource .= isset($token[1]) ? $token[1] : $token; |
|
147 | } |
||
148 | |||
149 | 31 | return $transformedSource; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Sets the new source for this file |
||
154 | * |
||
155 | * @TODO: Unfortunately, AST won't be changed, so please be accurate during transformation |
||
156 | * |
||
157 | * @param string $newSource |
||
158 | */ |
||
159 | 31 | private function setSource($newSource) |
|
166 | } |
||
167 |