12
12
13
13
namespace phpDocumentor \Reflection \DocBlock ;
14
14
15
+ use phpDocumentor \Reflection \DocBlock ;
16
+
15
17
/**
16
18
* Parses a tag definition for a DocBlock.
17
19
*
@@ -36,20 +38,59 @@ class Tag implements \Reflector
36
38
/** @var int Line number of the tag */
37
39
protected $ line_number = 0 ;
38
40
39
- /** @var \phpDocumentor\Reflection\DocBlock docblock class */
40
- protected $ docblock ;
41
+ /** @var DocBlock The DocBlock which this tag belongs to. */
42
+ protected $ docblock = null ;
43
+
44
+ /**
45
+ * @var array An array with a tag as a key, and an FQCN to a class that
46
+ * handles it as an array value. The class is expected to inherit this
47
+ * class.
48
+ */
49
+ private static $ tagHandlerMappings = array (
50
+ 'author '
51
+ => '\phpDocumentor\Reflection\DocBlock\Tag\AuthorTag ' ,
52
+ 'covers '
53
+ => '\phpDocumentor\Reflection\DocBlock\Tag\CoversTag ' ,
54
+ 'link '
55
+ => '\phpDocumentor\Reflection\DocBlock\Tag\LinkTag ' ,
56
+ 'method '
57
+ => '\phpDocumentor\Reflection\DocBlock\Tag\MethodTag ' ,
58
+ 'param '
59
+ => '\phpDocumentor\Reflection\DocBlock\Tag\ParamTag ' ,
60
+ 'property-read '
61
+ => '\phpDocumentor\Reflection\DocBlock\Tag\PropertyReadTag ' ,
62
+ 'property '
63
+ => '\phpDocumentor\Reflection\DocBlock\Tag\PropertyTag ' ,
64
+ 'property-write '
65
+ => '\phpDocumentor\Reflection\DocBlock\Tag\PropertyWriteTag ' ,
66
+ 'return '
67
+ => '\phpDocumentor\Reflection\DocBlock\Tag\ReturnTag ' ,
68
+ 'see '
69
+ => '\phpDocumentor\Reflection\DocBlock\Tag\SeeTag ' ,
70
+ 'throw '
71
+ => '\phpDocumentor\Reflection\DocBlock\Tag\ThrowsTag ' ,
72
+ 'throws '
73
+ => '\phpDocumentor\Reflection\DocBlock\Tag\ThrowsTag ' ,
74
+ 'uses '
75
+ => '\phpDocumentor\Reflection\DocBlock\Tag\UsesTag ' ,
76
+ 'var '
77
+ => '\phpDocumentor\Reflection\DocBlock\Tag\VarTag '
78
+ );
41
79
42
80
/**
43
81
* Factory method responsible for instantiating the correct sub type.
44
82
*
45
- * @param string $tag_line The text for this tag, including description.
83
+ * @param string $tag_line The text for this tag, including description.
84
+ * @param DocBlock $docblock The DocBlock which this tag belongs to.
46
85
*
47
86
* @throws \InvalidArgumentException if an invalid tag line was presented.
48
87
*
49
- * @return \phpDocumentor\Reflection\DocBlock\Tag
88
+ * @return static A new tag object.
50
89
*/
51
- public static function createInstance ($ tag_line )
52
- {
90
+ final public static function createInstance (
91
+ $ tag_line ,
92
+ DocBlock $ docblock = null
93
+ ) {
53
94
if (!preg_match (
54
95
'/^@([\w\-\_ \\\\]+)(?:\s*([^\s].*)|$)?/us ' ,
55
96
$ tag_line ,
@@ -60,31 +101,66 @@ public static function createInstance($tag_line)
60
101
);
61
102
}
62
103
63
- // support hypphen separated tag names
64
- $ tag_name = str_replace (
65
- ' ' ,
66
- '' ,
67
- ucwords (str_replace ('- ' , ' ' , $ matches [1 ]))
68
- ).'Tag ' ;
69
- $ class_name = 'phpDocumentor \\Reflection \\DocBlock \\Tag \\' . $ tag_name ;
70
-
71
- return ($ matches [1 ] === strtolower ($ matches [1 ])
72
- && @class_exists ($ class_name ))
73
- ? new $ class_name ($ matches [1 ], isset ($ matches [2 ]) ? $ matches [2 ] : '' )
74
- : new self ($ matches [1 ], isset ($ matches [2 ]) ? $ matches [2 ] : '' );
104
+ if (isset (self ::$ tagHandlerMappings [$ matches [1 ]])) {
105
+ $ handler = self ::$ tagHandlerMappings [$ matches [1 ]];
106
+ return new $ handler (
107
+ $ matches [1 ],
108
+ isset ($ matches [2 ]) ? $ matches [2 ] : '' ,
109
+ $ docblock
110
+ );
111
+ }
112
+ return new self (
113
+ $ matches [1 ],
114
+ isset ($ matches [2 ]) ? $ matches [2 ] : '' ,
115
+ $ docblock
116
+ );
117
+ }
118
+
119
+ /**
120
+ * Registers a handler for tags.
121
+ *
122
+ * Registers a handler for tags. The class specified is autoloaded if it's
123
+ * not available. It must inherit from this class.
124
+ *
125
+ * @param string $tag Name of tag to regiser a handler for.
126
+ * @param string|null $handler FQCN of handler. Specifing NULL removes the
127
+ * handler for the specified tag, if any.
128
+ *
129
+ * @return bool TRUE on success, FALSE on failure.
130
+ */
131
+ final public static function registerTagHandler ($ tag , $ handler )
132
+ {
133
+ $ tag = trim ((string )$ tag );
134
+
135
+ if (null === $ handler ) {
136
+ unset(self ::$ tagHandlerMappings [$ tag ]);
137
+ return true ;
138
+ }
139
+
140
+ if ('' !== $ tag
141
+ && class_exists ($ handler , true )
142
+ && is_subclass_of ($ handler , __CLASS__ )
143
+ ) {
144
+ self ::$ tagHandlerMappings [$ tag ] = $ handler ;
145
+ return true ;
146
+ }
147
+
148
+ return false ;
75
149
}
76
150
77
151
/**
78
152
* Parses a tag and populates the member variables.
79
153
*
80
- * @param string $type Name of the tag.
81
- * @param string $content The contents of the given tag.
154
+ * @param string $type Name of the tag.
155
+ * @param string $content The contents of the given tag.
156
+ * @param DocBlock $docblock The DocBlock which this tag belongs to.
82
157
*/
83
- public function __construct ($ type , $ content )
158
+ public function __construct ($ type , $ content, DocBlock $ docblock = null )
84
159
{
85
160
$ this ->tag = $ type ;
86
161
$ this ->content = $ content ;
87
- $ this ->description = $ content ;
162
+ $ this ->description = trim ($ content );
163
+ $ this ->docblock = $ docblock ;
88
164
}
89
165
90
166
/**
@@ -126,7 +202,7 @@ public function getDescription()
126
202
public function getParsedDescription ()
127
203
{
128
204
if (null === $ this ->parsedDescription ) {
129
- $ description = new LongDescription ($ this ->description );
205
+ $ description = new Description ($ this ->description , $ this -> docblock );
130
206
$ this ->parsedDescription = $ description ->getParsedContents ();
131
207
}
132
208
return $ this ->parsedDescription ;
@@ -154,20 +230,6 @@ public function getLineNumber()
154
230
return $ this ->line_number ;
155
231
}
156
232
157
- /**
158
- * Inject the docblock class
159
- *
160
- * This exposes some common functionality contained in the docblock abstract.
161
- *
162
- * @param object $docblock Object containing the DocBlock.
163
- *
164
- * @return void
165
- */
166
- public function setDocBlock ($ docblock )
167
- {
168
- $ this ->docblock = $ docblock ;
169
- }
170
-
171
233
/**
172
234
* Builds a string representation of this object.
173
235
*
0 commit comments