/usr/share/php/ApiGen/Backend.php is in php-apigen 2.8.0+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | <?php
/**
* ApiGen 2.8.0 - API documentation generator for PHP 5.3+
*
* Copyright (c) 2010-2011 David Grudl (http://davidgrudl.com)
* Copyright (c) 2011-2012 Jaroslav Hanslík (https://github.com/kukulich)
* Copyright (c) 2011-2012 Ondřej Nešpor (https://github.com/Andrewsville)
*
* For the full copyright and license information, please view
* the file LICENSE.md that was distributed with this source code.
*/
namespace ApiGen;
use TokenReflection, TokenReflection\IReflectionConstant, TokenReflection\IReflectionFunction, TokenReflection\Broker, TokenReflection\Resolver;
use InvalidArgumentException, RuntimeException;
/**
* Customized TokenReflection broker backend.
*
* Adds internal classes from @param, @var, @return, @throws annotations as well
* as parent classes to the overall class list.
*/
class Backend extends Broker\Backend\Memory
{
/**
* Generator instance.
*
* @var \ApiGen\Generator
*/
private $generator;
/**
* Cache of processed token streams.
*
* @var array
*/
private $fileCache = array();
/**
* Determines if token streams should be cached in filesystem.
*
* @var boolean
*/
private $cacheTokenStreams = false;
/**
* Constructor.
*
* @param \ApiGen\Generator $generator Generator instance
* @param boolean $cacheTokenStreams If token stream should be cached
*/
public function __construct(Generator $generator, $cacheTokenStreams = false)
{
$this->generator = $generator;
$this->cacheTokenStreams = $cacheTokenStreams;
}
/**
* Destructor.
*
* Deletes all cached token streams.
*/
public function __destruct()
{
foreach ($this->fileCache as $file) {
unlink($file);
}
}
/**
* Adds a file to the backend storage.
*
* @param \TokenReflection\Stream\StreamBase $tokenStream Token stream
* @param \TokenReflection\ReflectionFile $file File reflection object
* @return \TokenReflection\Broker\Backend\Memory
*/
public function addFile(TokenReflection\Stream\StreamBase $tokenStream, TokenReflection\ReflectionFile $file)
{
if ($this->cacheTokenStreams) {
$this->fileCache[$file->getName()] = $cacheFile = tempnam(sys_get_temp_dir(), 'trc');
file_put_contents($cacheFile, serialize($tokenStream));
}
parent::addFile($tokenStream, $file);
return $this;
}
/**
* Returns an array of tokens for a particular file.
*
* @param string $fileName File name
* @return \TokenReflection\Stream
* @throws \RuntimeException If the token stream could not be returned.
*/
public function getFileTokens($fileName)
{
try {
if (!$this->isFileProcessed($fileName)) {
throw new InvalidArgumentException('File was not processed');
}
$realName = Broker::getRealPath($fileName);
if (!isset($this->fileCache[$realName])) {
throw new InvalidArgumentException('File is not in the cache');
}
$data = @file_get_contents($this->fileCache[$realName]);
if (false === $data) {
throw new RuntimeException('Cached file is not readable');
}
$file = @unserialize($data);
if (false === $file) {
throw new RuntimeException('Stream could not be loaded from cache');
}
return $file;
} catch (\Exception $e) {
throw new RuntimeException(sprintf('Could not return token stream for file %s', $fileName), 0, $e);
}
}
/**
* Prepares and returns used class lists.
*
* @return array
*/
protected function parseClassLists()
{
$allClasses = array(
self::TOKENIZED_CLASSES => array(),
self::INTERNAL_CLASSES => array(),
self::NONEXISTENT_CLASSES => array()
);
$declared = array_flip(array_merge(get_declared_classes(), get_declared_interfaces()));
foreach ($this->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $name => $trClass) {
$class = new ReflectionClass($trClass, $this->generator);
$allClasses[self::TOKENIZED_CLASSES][$name] = $class;
if (!$class->isDocumented()) {
continue;
}
foreach (array_merge($trClass->getParentClasses(), $trClass->getInterfaces()) as $parentName => $parent) {
if ($parent->isInternal()) {
if (!isset($allClasses[self::INTERNAL_CLASSES][$parentName])) {
$allClasses[self::INTERNAL_CLASSES][$parentName] = $parent;
}
} elseif (!$parent->isTokenized()) {
if (!isset($allClasses[self::NONEXISTENT_CLASSES][$parentName])) {
$allClasses[self::NONEXISTENT_CLASSES][$parentName] = $parent;
}
}
}
$this->generator->checkMemory();
}
}
foreach ($allClasses[self::TOKENIZED_CLASSES] as $class) {
if (!$class->isDocumented()) {
continue;
}
foreach ($class->getOwnMethods() as $method) {
$allClasses = $this->processFunction($declared, $allClasses, $method);
}
foreach ($class->getOwnProperties() as $property) {
$annotations = $property->getAnnotations();
if (!isset($annotations['var'])) {
continue;
}
foreach ($annotations['var'] as $doc) {
foreach (explode('|', preg_replace('~\\s.*~', '', $doc)) as $name) {
if ($name = rtrim($name, '[]')) {
$name = Resolver::resolveClassFQN($name, $class->getNamespaceAliases(), $class->getNamespaceName());
$allClasses = $this->addClass($declared, $allClasses, $name);
}
}
}
}
$this->generator->checkMemory();
}
foreach ($this->getFunctions() as $function) {
$allClasses = $this->processFunction($declared, $allClasses, $function);
}
array_walk_recursive($allClasses, function(&$reflection, $name, Generator $generator) {
if (!$reflection instanceof ReflectionClass) {
$reflection = new ReflectionClass($reflection, $generator);
}
}, $this->generator);
return $allClasses;
}
/**
* Processes a function/method and adds classes from annotations to the overall class array.
*
* @param array $declared Array of declared classes
* @param array $allClasses Array with all classes parsed so far
* @param \ApiGen\ReflectionFunction|\TokenReflection\IReflectionFunctionBase $function Function/method reflection
* @return array
*/
private function processFunction(array $declared, array $allClasses, $function)
{
static $parsedAnnotations = array('param', 'return', 'throws');
$annotations = $function->getAnnotations();
foreach ($parsedAnnotations as $annotation) {
if (!isset($annotations[$annotation])) {
continue;
}
foreach ($annotations[$annotation] as $doc) {
foreach (explode('|', preg_replace('~\\s.*~', '', $doc)) as $name) {
if ($name) {
$name = Resolver::resolveClassFQN(rtrim($name, '[]'), $function->getNamespaceAliases(), $function->getNamespaceName());
$allClasses = $this->addClass($declared, $allClasses, $name);
}
}
}
}
foreach ($function->getParameters() as $param) {
if ($hint = $param->getClassName()) {
$allClasses = $this->addClass($declared, $allClasses, $hint);
}
}
return $allClasses;
}
/**
* Adds a class to list of classes.
*
* @param array $declared Array of declared classes
* @param array $allClasses Array with all classes parsed so far
* @param string $name Class name
* @return array
*/
private function addClass(array $declared, array $allClasses, $name)
{
$name = ltrim($name, '\\');
if (!isset($declared[$name]) || isset($allClasses[self::TOKENIZED_CLASSES][$name])
|| isset($allClasses[self::INTERNAL_CLASSES][$name]) || isset($allClasses[self::NONEXISTENT_CLASSES][$name])
) {
return $allClasses;
}
$parameterClass = $this->getBroker()->getClass($name);
if ($parameterClass->isInternal()) {
$allClasses[self::INTERNAL_CLASSES][$name] = $parameterClass;
foreach (array_merge($parameterClass->getInterfaces(), $parameterClass->getParentClasses()) as $parentClass) {
if (!isset($allClasses[self::INTERNAL_CLASSES][$parentName = $parentClass->getName()])) {
$allClasses[self::INTERNAL_CLASSES][$parentName] = $parentClass;
}
}
} elseif (!$parameterClass->isTokenized() && !isset($allClasses[self::NONEXISTENT_CLASSES][$name])) {
$allClasses[self::NONEXISTENT_CLASSES][$name] = $parameterClass;
}
return $allClasses;
}
/**
* Returns all constants from all namespaces.
*
* @return array
*/
public function getConstants()
{
$generator = $this->generator;
return array_map(function(IReflectionConstant $constant) use ($generator) {
return new ReflectionConstant($constant, $generator);
}, parent::getConstants());
}
/**
* Returns all functions from all namespaces.
*
* @return array
*/
public function getFunctions()
{
$generator = $this->generator;
return array_map(function(IReflectionFunction $function) use ($generator) {
return new ReflectionFunction($function, $generator);
}, parent::getFunctions());
}
}
|