View
531
Download
0
Category
Preview:
Citation preview
James Titcumbwww.jamestitcumb.comwww.roave.comwww.phphants.co.ukwww.phpsouthcoast.co.uk@asgrim
Who is this guy?
GET_REFLECTION_OBJECT_PTR(ce);
lc_name = zend_str_tolower_dup(name, name_len);
if ((ce == zend_ce_closure && (name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
&& memcmp(lc_name, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0)
|| zend_hash_str_exists(&ce->function_table, lc_name, name_len)) {
efree(lc_name);
RETURN_TRUE;
} else {
efree(lc_name);
RETURN_FALSE;
}
use PhpParser\ParserFactory;
$parser = (new ParserFactory)
->create(ParserFactory::PREFER_PHP7);
print_r($parser->parse(
file_get_contents('ast-demo-src.php')
));
AST
`-- Echo statement
`-- Concat
|-- Left
| `-- String, value "Hello "
`-- Right
`-- String, value "world"
$reflection = new ReflectionClass(
'BetterReflectionTest\Fixture\ExampleClass'
);
$this->assertSame(
'ExampleClass',
$reflection->getShortName()
);
$reflection = ReflectionClass::createFromName(
'BetterReflectionTest\Fixture\ExampleClass'
);
$this->assertSame(
'ExampleClass',
$reflection->getShortName()
);
// In ReflectionClass :
public static function createFromName($className)
{
return ClassReflector::buildDefaultReflector()->reflect($className);
}
// In ClassReflector :
public static function buildDefaultReflector()
{
return new self(new AggregateSourceLocator([
new PhpInternalSourceLocator(),
new EvaledCodeSourceLocator(),
new AutoloadSourceLocator(),
]));
}
AutoloadSourceLocator
ReflectionClass::createFromName(new MyClass)
replace stream wrapper
disable error handling
call “class_exists”
restore stream wrapper
restore error handling
store attempted filename load
DO NOT LOAD FILE!
return stored filename
Read file and parse AST!
// Create the reflection first
// ***BEFORE*** class is loaded
$classInfo = ReflectionClass::createFromName('MyClass');
// Override the body...!
$methodInfo = $classInfo->getMethod('foo');
$methodInfo->setBody(function () {
return 4;
});
// Save the class and require it
$classCode = (new CodePrinter())->prettyPrint([$classInfo->getAst()]);
$tmpFile = tempnam(sys_get_temp_dir(), 'br-monkey-patching');
file_put_contents($tmpFile, '<?php ' . $classCode);
require_once($tmpFile);
unlink($tmpFile);
// Now create an instance, and call the function...
$c = new MyClass();
var_dump($c->foo()); // will be 4!!!
● DONE Currently: rewriting the internals○ SourceLocators “might” return some code ?!
● DONE Returning AST/Code for method/fn
Ideas/plans - DONE
● WIP More compatibility core reflection● WIP Reflect core fns with default params
○ This is not currently possible with core reflection● WIP Modification & exporting● WIP Reflecting on closures
Ideas/plans - WIP
● Adding a getColumn() function● PHP 7 compatibility● @inheritDoc traversal (to inherit type hints)● Make it faster
Ideas/plans
Any questions? :)
https://joind.in/talk/9a5d8James Titcumb @asgrim
Recommended