1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace TokenReflection;
17:
18: use TokenReflection\Stream\StreamBase as Stream, TokenReflection\Exception;
19:
20: 21: 22:
23: class ReflectionFile extends ReflectionBase
24: {
25: 26: 27: 28: 29:
30: private $namespaces = array();
31:
32: 33: 34: 35: 36:
37: public function getNamespaces()
38: {
39: return $this->namespaces;
40: }
41:
42: 43: 44: 45: 46:
47: public function __toString()
48: {
49: throw new Exception\RuntimeException('Casting to string is not supported.', Exception\RuntimeException::UNSUPPORTED, $this);
50: }
51:
52: 53: 54: 55: 56: 57: 58: 59:
60: public static function export(Broker $broker, $argument, $return = false)
61: {
62: throw new Exception\RuntimeException('Export is not supported.', Exception\RuntimeException::UNSUPPORTED);
63: }
64:
65: 66: 67: 68: 69:
70: public function getSource()
71: {
72: return (string) $this->broker->getFileTokens($this->getName());
73: }
74:
75: 76: 77: 78: 79: 80: 81:
82: protected function parseStream(Stream $tokenStream, IReflection $parent = null)
83: {
84: $this->name = $tokenStream->getFileName();
85:
86: if (1 >= $tokenStream->count()) {
87:
88: $this->docComment = new ReflectionAnnotation($this, null);
89: return $this;
90: }
91:
92: $docCommentPosition = null;
93:
94: if (!$tokenStream->is(T_OPEN_TAG)) {
95: $this->namespaces[] = new ReflectionFileNamespace($tokenStream, $this->broker, $this);
96: } else {
97: $tokenStream->skipWhitespaces();
98:
99: while (null !== ($type = $tokenStream->getType())) {
100: switch ($type) {
101: case T_DOC_COMMENT:
102: if (null === $docCommentPosition) {
103: $docCommentPosition = $tokenStream->key();
104: }
105: case T_WHITESPACE:
106: case T_COMMENT:
107: break;
108: case T_DECLARE:
109:
110: $tokenStream
111: ->skipWhitespaces()
112: ->findMatchingBracket()
113: ->skipWhitespaces()
114: ->skipWhitespaces();
115: break;
116: case T_NAMESPACE:
117: $docCommentPosition = $docCommentPosition ?: -1;
118: break 2;
119: default:
120: $docCommentPosition = $docCommentPosition ?: -1;
121: $this->namespaces[] = new ReflectionFileNamespace($tokenStream, $this->broker, $this);
122: break 2;
123: }
124:
125: $tokenStream->skipWhitespaces();
126: }
127:
128: while (null !== ($type = $tokenStream->getType())) {
129: if (T_NAMESPACE === $type) {
130: $this->namespaces[] = new ReflectionFileNamespace($tokenStream, $this->broker, $this);
131: } else {
132: $tokenStream->skipWhitespaces();
133: }
134: }
135: }
136:
137: if (null !== $docCommentPosition && !empty($this->namespaces) && $docCommentPosition === $this->namespaces[0]->getStartPosition()) {
138: $docCommentPosition = null;
139: }
140: $this->docComment = new ReflectionAnnotation($this, null !== $docCommentPosition ? $tokenStream->getTokenValue($docCommentPosition) : null);
141:
142: return $this;
143: }
144: }
145: