GithubHelp home page GithubHelp logo

justblackbird / parser-reflection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from goaop/parser-reflection

0.0 2.0 0.0 247 KB

Parser Reflection API - Provides source code analysis without loading classes into the PHP memory

License: MIT License

Shell 0.08% PHP 99.92%

parser-reflection's Introduction

Parser Reflection API Library

Parser Reflection API library provides a set of classes that extend original internal Reflection classes, but powered by PHP-Parser library thus allowing to create a reflection instance without loading classes into the memory.

This library can be used for analysing the source code for PHP versions 5.5, 5.6, 7.0; for automatic proxy creation and much more.

Build Status Code Coverage Total Downloads Daily Downloads Scrutinizer Code Quality SensioLabs InsightMinimum PHP Version License

Installation

Library can be installed with Composer. Installation is quite easy:

$ composer require goaop/parser-reflection

Composer will install the library to your project's vendor/goaop/parser-reflection directory.

Usage

Initialization

Prior to the first use library can be optionally initialized. If you use Composer for installing packages and loading classes, then you shouldn't worry about initialization, library will be initialized automatically.

If project uses a custom autoloader then you should follow the next steps:

  1. Create a new class that implements \Go\ParserReflection\LocatorInterface
  2. Create an instance of that class and pass it to the ReflectionEngine::init() method for initial configuration

Reflecting concrete classes/methods/properties without loading them

Just use Go\ParserReflection package reflection classes like traditional ones:

$parsedClass = new \Go\ParserReflection\ReflectionClass(SomeClass::class);
var_dump($parsedClass->getMethods());

$parsedMethod = new \Go\ParserReflection\ReflectionMethod(SomeClass::class, 'someMethod');
echo (string)$parsedMethod;

Or you can use an additional classes ReflectionFile and ReflectionFileNamespace to analyse a raw PHP files:

$parsedFile     = new \Go\ParserReflection\ReflectionFile('SomeClass.php');
$fileNameSpaces = $parsedFile->getFileNamespaces();
// We can iterate over namespaces in the file
foreach ($fileNameSpaces as $namespace) {
    $classes = $namespace->getClasses();
    // Iterate over the classes in the namespace
    foreach ($classes as $class) {
        echo "Found class: ", $class->getName(), PHP_EOL;
        // Now let's show all methods in the class
        foreach ($class->getMethods() as $method) {
            echo "Found class method: ", $class->getName(), '::', $method->getName(), PHP_EOL;
        }
        
        // ...all properties in the class
        foreach ($class->getProperties() as $property) {
            echo "Found class property: ", $class->getName(), '->', $property->getName(), PHP_EOL;
        }
    }
}

How it works?

To understand how library works let's look at what happens during the call to the new \Go\ParserReflection\ReflectionClass(SomeClass::class)

  • \Go\ParserReflection\ReflectionClass asks reflection engine to give an AST node for the given class name
  • Reflection engine asks a locator to locate a filename for the given class
  • ComposerLocator instance asks the Composer to find a filename for the given class and returns this result back to the reflection engine
  • Reflection engine loads the content of file and passes it to the PHP-Parser for tokenization and processing
  • PHP-Parser returns an AST (Abstract Syntax Tree)
  • Reflection engine then analyse this AST to extract specific nodes an wrap them into corresponding reflection classes.

Compatibility

All parser reflection classes extend PHP internal reflection classes, this means that you can use \Go\ParserReflection\ReflectionClass instance in any place that asks for \ReflectionClass instance. All reflection methods should be compatible with original ones, providing an except methods that requires object manipulation, such as invoke(), invokeArgs(), setAccessible(), etc. These methods will trigger the autoloading of class and switching to the internal reflection.

parser-reflection's People

Contributors

fedott avatar lisachenko avatar loren-osborn avatar maximiliankresse avatar pdelre avatar siwinski avatar volch avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.