Maslosoft Addendum API
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
<?php
/**
* This software package is licensed under AGPL, Commercial license.
*
* @package maslosoft/addendum
* @licence AGPL, Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com> (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
* @link https://maslosoft.com/addendum/ - maslosoft addendum
* @link https://code.google.com/p/addendum/ - original addendum project
*/
namespace Maslosoft\Addendum\Builder;
use Exception;
use Maslosoft\Addendum\Addendum;
use Maslosoft\Addendum\Annotations\TargetAnnotation;
use Maslosoft\Addendum\Cache\BuildOneCache;
use Maslosoft\Addendum\Collections\AnnotationsCollection;
use Maslosoft\Addendum\Collections\MatcherConfig;
use Maslosoft\Addendum\Helpers\CoarseChecker;
use Maslosoft\Addendum\Interfaces\AnnotationInterface;
use Maslosoft\Addendum\Matcher\AnnotationsMatcher;
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedClass;
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedMethod;
use Maslosoft\Addendum\Reflection\ReflectionAnnotatedProperty;
use Maslosoft\Addendum\Utilities\Blacklister;
use Maslosoft\Addendum\Utilities\ClassChecker;
use Maslosoft\Addendum\Utilities\NameNormalizer;
use Maslosoft\Addendum\Utilities\ReflectionName;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Reflector;
/**
* @Label("Annotations builder")
*/
class Builder
{
/**
* Cached values of parsing
* @var string[][][]
*/
private static $cache = [];
/**
* Addendum instance
* @var Addendum
*/
private $addendum = null;
/**
* One entity build cache
* @var BuildOneCache
*/
private $buildCache = null;
public function __construct(Addendum $addendum = null)
{
$this->addendum = $addendum ?: new Addendum();
$this->buildCache = new BuildOneCache(static::class, null, $this->addendum);
}
/**
* Build annotations collection
* @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $targetReflection
* @return AnnotationsCollection
*/
public function build($targetReflection)
{
$annotations = [];
$data = $this->buildOne($targetReflection);
// Get annotations from current entity
foreach ($data as $class => $parameters)
{
foreach ($parameters as $params)
{
$annotation = $this->instantiateAnnotation($class, $params, $targetReflection);
if ($annotation !== false)
{
$annotations[$class][] = $annotation;
}
}
}
return new AnnotationsCollection($annotations);
}
private function buildOne($targetReflection)
{
if (empty($targetReflection))
{
return [];
}
// Decide where from take traits and base classes.
// Either from class if it's being processed reflection class
// or from declaring class if it's being processed for properties and
// methods
if ($targetReflection instanceof ReflectionClass)
{
$targetClass = $targetReflection;
}
else
{
$targetClass = $targetReflection->getDeclaringClass();
}
// Check if currently reflected component is cached
$this->buildCache->setComponent(ReflectionName::createName($targetReflection));
$cached = $this->buildCache->get();
// Check if currently reflected component *class* is cached
$this->buildCache->setComponent(ReflectionName::createName($targetClass));
$cachedClass = $this->buildCache->get();
// Cache is valid only if class cache is valid,
// this is required for Conflicts and Target checks
if (false !== $cached && false !== $cachedClass)
{
return $cached;
}
// Parials annotation data
$partialsData = [];
// Extract annotations from all partials
foreach ($this->getPartials($targetClass) as $partial)
{
// Need to recurse here, as:
//
// * Interface might extend from other interfaces
// * Parent class, might have traits or interfaces
// * Trait might have traits
$partData = $this->buildOne($this->getRelated($targetReflection, $partial));
// Check if data is present and proper
if (false === $partData || empty($partData) || !is_array($partData))
{
continue;
}
$partialsData = array_merge_recursive($partialsData, $partData);
}
// Merge data from traits etc.
// with data from class
$data = array_merge_recursive($partialsData, $this->parse($targetReflection));
$this->buildCache->setComponent(ReflectionName::createName($targetReflection));
$this->buildCache->set($data);
return $data;
}
/**
* Get reflection for related target reflection.
* Will return class, property or method reflection from related reflection,
* based on type of target reflection.
*
* Will return false target reflection id for method or property, and
* method or property does not exists on related reflection.
*
* @param Reflector $targetReflection
* @param ReflectionClass $relatedReflection
* @return ReflectionClass|ReflectionProperty|ReflectionMethod|bool
*/
private function getRelated(Reflector $targetReflection, ReflectionClass $relatedReflection)
{
switch (true)
{
case $targetReflection instanceof ReflectionClass:
return $relatedReflection;
case $targetReflection instanceof ReflectionProperty && $relatedReflection->hasProperty($targetReflection->name):
return $relatedReflection->getProperty($targetReflection->name);
case $targetReflection instanceof ReflectionMethod && $relatedReflection->hasMethod($targetReflection->name):
return $relatedReflection->getMethod($targetReflection->name);
}
return false;
}
/**
* Get partials of class, this includes:
*
* * Parent class
* * Interfaces
* * Traits
*
* @param ReflectionClass $targetClass
* @return ReflectionAnnotatedClass[]
*/
private function getPartials(ReflectionClass $targetClass)
{
// Partial reflections
/* @var ReflectionAnnotatedClass[] */
$partials = [];
// Collect current class interfaces
foreach ($targetClass->getInterfaces() as $targetInterface)
{
/* @var $targetInterface ReflectionAnnotatedClass */
$partials[] = $targetInterface;
}
// Collect current class parent class
$targetParent = $targetClass->getParentClass();
if (!empty($targetParent))
{
$partials[] = $targetParent;
}
// Collect current class traits
foreach ($targetClass->getTraits() as $trait)
{
/* @var $trait ReflectionAnnotatedClass */
$partials[] = $trait;
}
return $partials;
}
/**
* Create new instance of annotation
* @param string $class
* @param mixed[] $parameters
* @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|bool $targetReflection
* @return boolean|object
*/
public function instantiateAnnotation($class, $parameters, $targetReflection = false)
{
$class = ucfirst($class) . "Annotation";
// If namespaces are empty assume global namespace
$fqn = $this->normalizeFqn('\\', $class);
$mandatoryNs = TargetAnnotation::Ns;
$namespaces = $this->addendum->namespaces;
if (!in_array($mandatoryNs, $namespaces))
{
$namespaces[] = $mandatoryNs;
}
foreach ($namespaces as $ns)
{
$fqn = $this->normalizeFqn($ns, $class);
if (Blacklister::ignores($fqn))
{
continue;
}
try
{
if (!ClassChecker::exists($fqn))
{
$this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]);
Blacklister::ignore($fqn);
}
else
{
// Class exists, exit loop
break;
}
}
catch (Exception $e)
{
// Ignore class autoloading errors
}
}
if (Blacklister::ignores($fqn))
{
return false;
}
try
{
// NOTE: was class_exists here, however should be safe to use ClassChecker::exists here
if (!ClassChecker::exists($fqn))
{
$this->addendum->getLogger()->debug('Annotation class `{fqn}` not found, ignoring', ['fqn' => $fqn]);
Blacklister::ignore($fqn);
return false;
}
}
catch (Exception $e)
{
// Ignore autoload errors and return false
Blacklister::ignore($fqn);
return false;
}
$resolvedClass = Addendum::resolveClassName($fqn);
if ((new ReflectionClass($resolvedClass))->implementsInterface(AnnotationInterface::class) || $resolvedClass == AnnotationInterface::class)
{
return new $resolvedClass($parameters, $targetReflection);
}
return false;
}
/**
* Normalize class name and namespace to proper fully qualified name
* @param string $ns
* @param string $class
* @return string
*/
private function normalizeFqn($ns, $class)
{
$fqn = "\\$ns\\$class";
NameNormalizer::normalize($fqn);
return $fqn;
}
/**
* Get doc comment
* @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection
* @return mixed[]
*/
private function parse($reflection)
{
$key = sprintf('%s@%s', $this->addendum->getInstanceId(), ReflectionName::createName($reflection));
if (!isset(self::$cache[$key]))
{
//
if (!CoarseChecker::mightHaveAnnotations($reflection))
{
self::$cache[$key] = [];
return self::$cache[$key];
}
$parser = new AnnotationsMatcher;
$data = [];
$parser->setPlugins(new MatcherConfig([
'addendum' => $this->addendum,
'reflection' => $reflection
]));
$parser->matches($this->getDocComment($reflection), $data);
self::$cache[$key] = $data;
}
return self::$cache[$key];
}
/**
* Get doc comment
* @param ReflectionAnnotatedClass|ReflectionAnnotatedMethod|ReflectionAnnotatedProperty $reflection
* @return mixed[]
*/
protected function getDocComment($reflection)
{
return Addendum::getDocComment($reflection);
}
/**
* Clear local parsing cache
*/
public static function clearCache()
{
self::$cache = [];
}
}
API documentation generated by ApiGen