/usr/share/php/ApiGen/Parser/Broker/Backend.php is in php-apigen 4.1.2-1ubuntu2.
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 | <?php
/**
* This file is part of the ApiGen (http://apigen.org)
*
* For the full copyright and license information, please view
* the file license.md that was distributed with this source code.
*/
namespace ApiGen\Parser\Broker;
use ApiGen\Reflection\ReflectionClass;
use ApiGen\Reflection\ReflectionConstant;
use ApiGen\Reflection\ReflectionFunction;
use ApiGen\Reflection\ReflectionMethod;
use ApiGen\Reflection\TokenReflection\ReflectionFactory;
use TokenReflection;
use TokenReflection\Broker;
use TokenReflection\IReflectionConstant;
use TokenReflection\IReflectionFunction;
use TokenReflection\Resolver;
/**
* Customized TokenReflection broker backend.
* Adds internal classes from @param, @var, @return, @throws annotations as well
* as parent classes to the overall class list.
*
* @method TokenReflection\ReflectionNamespace[] getNamespaces()
*/
class Backend extends Broker\Backend\Memory
{
/**
* @var array[]
*/
private $allClasses = [
self::TOKENIZED_CLASSES => [],
self::INTERNAL_CLASSES => [],
self::NONEXISTENT_CLASSES => []
];
/**
* @var array
*/
private $declared = [];
/**
* @var ReflectionFactory
*/
private $reflectionFactory;
public function __construct(ReflectionFactory $reflectionFactory)
{
$this->reflectionFactory = $reflectionFactory;
}
/**
* @return ReflectionConstant[]
*/
public function getConstants()
{
return array_map(function (IReflectionConstant $constant) {
return $this->reflectionFactory->createFromReflection($constant);
}, parent::getConstants());
}
/**
* @return ReflectionFunction[]
*/
public function getFunctions()
{
return array_map(function (IReflectionFunction $function) {
return $this->reflectionFactory->createFromReflection($function);
}, parent::getFunctions());
}
/**
* Prepares and returns used class lists.
*
* @return ReflectionClass[]
*/
protected function parseClassLists()
{
$this->declared = array_flip(array_merge(get_declared_classes(), get_declared_interfaces()));
foreach ($this->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $name => $ref) {
$class = $this->reflectionFactory->createFromReflection($ref);
$this->allClasses[self::TOKENIZED_CLASSES][$name] = $class;
if ( ! $class->isDocumented()) {
continue;
}
$this->loadParentClassesAndInterfacesFromClassReflection($ref);
}
}
/** @var ReflectionClass $class */
foreach ($this->allClasses[self::TOKENIZED_CLASSES] as $class) {
if ( ! $class->isDocumented()) {
continue;
}
foreach ($class->getOwnMethods() as $method) {
$this->processFunction($method);
}
foreach ($class->getOwnProperties() as $property) {
$this->loadAnnotationFromReflection($class, $property->getAnnotations(), 'var');
}
}
foreach ($this->getFunctions() as $function) {
$this->processFunction($function);
}
array_walk_recursive($this->allClasses, function (&$reflection) {
if ( ! $reflection instanceof ReflectionClass) {
$reflection = $this->reflectionFactory->createFromReflection($reflection);
}
});
return $this->allClasses;
}
/**
* Processes a function/method and adds classes from annotations to the overall class array.
*
* @param ReflectionMethod|ReflectionFunction $reflection
*/
private function processFunction($reflection)
{
$annotations = $reflection->getAnnotations();
foreach (['param', 'return', 'throws'] as $annotation) {
$this->loadAnnotationFromReflection($reflection, $annotations, $annotation);
}
foreach ($reflection->getParameters() as $parameter) {
if ($hint = $parameter->getClassName()) {
$this->addClass($hint);
}
}
}
/**
* @param string $name
*/
private function addClass($name)
{
$name = ltrim($name, '\\');
if ( ! isset($this->declared[$name]) || $this->isClassLoaded($name)) {
return FALSE;
}
$parameterClass = $this->getBroker()->getClass($name);
if ($parameterClass->isInternal()) {
$this->allClasses[self::INTERNAL_CLASSES][$name] = $parameterClass;
foreach (array_merge($parameterClass->getInterfaces(), $parameterClass->getParentClasses()) as $parentClass) {
if ( ! isset($this->allClasses[self::INTERNAL_CLASSES][$parentName = $parentClass->getName()])) {
$this->allClasses[self::INTERNAL_CLASSES][$parentName] = $parentClass;
}
}
} elseif ( ! $parameterClass->isTokenized()) {
if ( ! isset($this->allClasses[self::NONEXISTENT_CLASSES][$name])) {
$this->allClasses[self::NONEXISTENT_CLASSES][$name] = $parameterClass;
}
}
}
/**
* @param TokenReflection\ReflectionClass|TokenReflection\Invalid\ReflectionClass $ref
*/
private function loadParentClassesAndInterfacesFromClassReflection($ref)
{
foreach (array_merge($ref->getParentClasses(), $ref->getInterfaces()) as $parentName => $parentReflection) {
/** @var TokenReflection\ReflectionClass $parentReflection */
if ($parentReflection->isInternal()) {
if ( ! isset($this->allClasses[self::INTERNAL_CLASSES][$parentName])) {
$this->allClasses[self::INTERNAL_CLASSES][$parentName] = $parentReflection;
}
} elseif ( ! $parentReflection->isTokenized()) {
if ( ! isset($this->allClasses[self::NONEXISTENT_CLASSES][$parentName])) {
$this->allClasses[self::NONEXISTENT_CLASSES][$parentName] = $parentReflection;
}
}
}
}
/**
* @param string $name
* @return bool
*/
private function isClassLoaded($name)
{
return isset($this->allClasses[self::TOKENIZED_CLASSES][$name])
|| isset($this->allClasses[self::INTERNAL_CLASSES][$name])
|| isset($this->allClasses[self::NONEXISTENT_CLASSES][$name]);
}
/**
* @param ReflectionClass|ReflectionMethod $reflection
* @param array $annotations
* @param string $name
*/
private function loadAnnotationFromReflection($reflection, array $annotations, $name)
{
if ( ! isset($annotations[$name])) {
return;
}
foreach ($annotations[$name] as $doc) {
foreach (explode('|', preg_replace('~\\s.*~', '', $doc)) as $name) {
if ($name = rtrim($name, '[]')) {
$name = $this->getClassFqn($name, $reflection);
$this->addClass($name);
}
}
}
}
/**
* @param string $name
* @param ReflectionClass|ReflectionMethod $reflection
* @return string
*/
private function getClassFqn($name, $reflection)
{
return Resolver::resolveClassFqn($name, $reflection->getNamespaceAliases(), $reflection->getNamespaceName());
}
}
|