[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10 namespace Zend\Code\Reflection; 11 12 use Zend\Code\Scanner\CachingFileScanner; 13 14 class FileReflection implements ReflectionInterface 15 { 16 /** 17 * @var string 18 */ 19 protected $filePath = null; 20 21 /** 22 * @var string 23 */ 24 protected $docComment = null; 25 26 /** 27 * @var int 28 */ 29 protected $startLine = 1; 30 31 /** 32 * @var int 33 */ 34 protected $endLine = null; 35 36 /** 37 * @var string[] 38 */ 39 protected $namespaces = array(); 40 41 /** 42 * @var string[] 43 */ 44 protected $uses = array(); 45 46 /** 47 * @var string[] 48 */ 49 protected $requiredFiles = array(); 50 51 /** 52 * @var ClassReflection[] 53 */ 54 protected $classes = array(); 55 56 /** 57 * @var FunctionReflection[] 58 */ 59 protected $functions = array(); 60 61 /** 62 * @var string 63 */ 64 protected $contents = null; 65 66 /** 67 * @param string $filename 68 * @param bool $includeIfNotAlreadyIncluded 69 * @throws Exception\InvalidArgumentException If file does not exists 70 * @throws Exception\RuntimeException If file exists but is not included or required 71 */ 72 public function __construct($filename, $includeIfNotAlreadyIncluded = false) 73 { 74 if (($fileRealPath = realpath($filename)) === false) { 75 $fileRealPath = stream_resolve_include_path($filename); 76 } 77 78 if (!$fileRealPath) { 79 throw new Exception\InvalidArgumentException(sprintf( 80 'No file for %s was found.', 81 $filename 82 )); 83 } 84 85 if (!in_array($fileRealPath, get_included_files())) { 86 if (!$includeIfNotAlreadyIncluded) { 87 throw new Exception\RuntimeException(sprintf( 88 'File %s must be required before it can be reflected', 89 $filename 90 )); 91 } 92 93 include $fileRealPath; 94 } 95 96 $this->filePath = $fileRealPath; 97 $this->reflect(); 98 } 99 100 /** 101 * Required by the Reflector interface. 102 * 103 * @todo What should this do? 104 * @return null 105 */ 106 public static function export() 107 { 108 return; 109 } 110 111 /** 112 * Return the file name of the reflected file 113 * 114 * @return string 115 */ 116 public function getFileName() 117 { 118 return basename($this->filePath); 119 } 120 121 /** 122 * Get the start line - Always 1, staying consistent with the Reflection API 123 * 124 * @return int 125 */ 126 public function getStartLine() 127 { 128 return $this->startLine; 129 } 130 131 /** 132 * Get the end line / number of lines 133 * 134 * @return int 135 */ 136 public function getEndLine() 137 { 138 return $this->endLine; 139 } 140 141 /** 142 * @return string 143 */ 144 public function getDocComment() 145 { 146 return $this->docComment; 147 } 148 149 /** 150 * @return DocBlockReflection 151 */ 152 public function getDocBlock() 153 { 154 if (!($docComment = $this->getDocComment())) { 155 return false; 156 } 157 158 $instance = new DocBlockReflection($docComment); 159 160 return $instance; 161 } 162 163 /** 164 * @return string[] 165 */ 166 public function getNamespaces() 167 { 168 return $this->namespaces; 169 } 170 171 /** 172 * @return string 173 */ 174 public function getNamespace() 175 { 176 if (count($this->namespaces) == 0) { 177 return; 178 } 179 180 return $this->namespaces[0]; 181 } 182 183 /** 184 * @return array 185 */ 186 public function getUses() 187 { 188 return $this->uses; 189 } 190 191 /** 192 * Return the reflection classes of the classes found inside this file 193 * 194 * @return ClassReflection[] 195 */ 196 public function getClasses() 197 { 198 $classes = array(); 199 foreach ($this->classes as $class) { 200 $classes[] = new ClassReflection($class); 201 } 202 203 return $classes; 204 } 205 206 /** 207 * Return the reflection functions of the functions found inside this file 208 * 209 * @return FunctionReflection[] 210 */ 211 public function getFunctions() 212 { 213 $functions = array(); 214 foreach ($this->functions as $function) { 215 $functions[] = new FunctionReflection($function); 216 } 217 218 return $functions; 219 } 220 221 /** 222 * Retrieve the reflection class of a given class found in this file 223 * 224 * @param null|string $name 225 * @return ClassReflection 226 * @throws Exception\InvalidArgumentException for invalid class name or invalid reflection class 227 */ 228 public function getClass($name = null) 229 { 230 if (null === $name) { 231 reset($this->classes); 232 $selected = current($this->classes); 233 234 return new ClassReflection($selected); 235 } 236 237 if (in_array($name, $this->classes)) { 238 return new ClassReflection($name); 239 } 240 241 throw new Exception\InvalidArgumentException(sprintf( 242 'Class by name %s not found.', 243 $name 244 )); 245 } 246 247 /** 248 * Return the full contents of file 249 * 250 * @return string 251 */ 252 public function getContents() 253 { 254 return file_get_contents($this->filePath); 255 } 256 257 public function toString() 258 { 259 return ''; // @todo 260 } 261 262 /** 263 * Serialize to string 264 * 265 * Required by the Reflector interface 266 * 267 * @todo What should this serialization look like? 268 * @return string 269 */ 270 public function __toString() 271 { 272 return ''; 273 } 274 275 /** 276 * This method does the work of "reflecting" the file 277 * 278 * Uses Zend\Code\Scanner\FileScanner to gather file information 279 * 280 * @return void 281 */ 282 protected function reflect() 283 { 284 $scanner = new CachingFileScanner($this->filePath); 285 $this->docComment = $scanner->getDocComment(); 286 $this->requiredFiles = $scanner->getIncludes(); 287 $this->classes = $scanner->getClassNames(); 288 $this->namespaces = $scanner->getNamespaces(); 289 $this->uses = $scanner->getUses(); 290 } 291 292 /** 293 * Validate / check a file level DocBlock 294 * 295 * @param array $tokens Array of tokenizer tokens 296 * @return void 297 */ 298 protected function checkFileDocBlock($tokens) 299 { 300 foreach ($tokens as $token) { 301 $type = $token[0]; 302 $value = $token[1]; 303 $lineNum = $token[2]; 304 if (($type == T_OPEN_TAG) || ($type == T_WHITESPACE)) { 305 continue; 306 } elseif ($type == T_DOC_COMMENT) { 307 $this->docComment = $value; 308 $this->startLine = $lineNum + substr_count($value, "\n") + 1; 309 310 return; 311 } else { 312 // Only whitespace is allowed before file DocBlocks 313 return; 314 } 315 } 316 } 317 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |