2017 © Pedro Peláez
 

library hasty-reflection-parser

image

donquixote/hasty-reflection-parser

  • Monday, January 4, 2016
  • by donquixote
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

hasty-reflection-parser

A library that parses PHP code to generate reflection objects., (*2)

It is called "hasty", because it skips and omits and ignores a lot of stuff, and only focuses on what one usually wants for e.g. annotation discovery., (*3)

Currently it ignores variable declarations, regular statements, function calls, object variables, function declarations, and more., (*4)

Use cases:
It is meant for stuff like Annotation discovery like in Doctrine or Drupal 8., (*5)

Partial libraries (as dependencies):

hasty-php-ast:
Abstract syntax tree. This is an incomplete representation of PHP !, (*6)

hasty-php-parser:
The parser. Turns a PHP string into an AST graph. Based on token_get_all()., (*7)

hasty-reflection-common:
Classes and interfaces similar to the core reflection API. Contains a working "native" implementation that uses core reflection instead of parsing., (*8)

hasty-reflection-parser:
Uses the parser to generate the reflection objects., (*9)

How to use

First, choose one of the various implementations of ClassIndexInterface. E.g., (*10)

// Use native reflection and native class loader. This means, all files will be *really* included in PHP.
$classIndex = new ClassIndex_Native();

// Files will be parsed AND included.
$classIndex = ClassIndex_Ast::createSemiNative();

// Class files will be parsed, and NOT included.
// Using the currently active Composer class loader/finder. Obviously you need to make sure to get the path right.
$composerClassLoader = include dirname(dirname(__DIR__)) . '/vendor/autoload.php';
$classIndex = ClassIndex_Ast::createWithClassLoader(new ClassLoader_Composer($composerClassLoader));

Now you can get class reflection objects as you want., (*11)

$classReflection = $classIndex->classGetReflection(C::class);
print $classReflection->getDocComment();

The ClassIndex makes sure that only one reflection object exists for each class/interface/trait/method., (*12)

assert($classIndex->classGetReflection(C::class) === $classIndex->classGetReflection(C::class));
print $classReflection->getDocComment();

Performance

This thing should be quite fast, because the parser skips a lot of stuff. For long method or function bodies, it will simply fast-forward through the tokens, and only count opening and closing brackets!, (*13)

It does currently NOT do the trick of cutting out the class body PHP. This is because it was made for a use case where we are really interested in the class body., (*14)

However, the class body will initially be read in fast mode, to be picked up later, if/when requested. (This is the "lazy" parameter for some classes)., (*15)

The token array from token_get_all() is never sliced or manipulated, which means PHP only needs to pass around the array pointer, not create different versions the array itself., (*16)

The length of the token array is not remembered either, instead it is terminated by a unique symbol., (*17)

Status of this library

This is very fresh. Mabye some names will change in the near future!, (*18)

The Versions

04/01 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

by Andreas Hennings

04/01 2016

0.0.0.alpha.4

0.0.0.0-alpha4

  Sources   Download

MIT

The Requires

 

by Andreas Hennings

04/01 2016

0.0.0.alpha.3

0.0.0.0-alpha3

  Sources   Download

MIT

The Requires

 

by Andreas Hennings

03/01 2016

0.0.0.alpha.2

0.0.0.0-alpha2

  Sources   Download

MIT

The Requires

 

by Andreas Hennings

03/01 2016

0.0.0.alpha.1

0.0.0.0-alpha1

  Sources   Download

MIT

The Requires

 

by Andreas Hennings

03/01 2016

0.0.0.alpha

0.0.0.0-alpha

  Sources   Download

MIT

The Requires

 

by Andreas Hennings