In order to automatically reflect using the exact same API as the core Reflection* classes, we need to be able to essentially ignore the whole SourceLocator concept, despite it's flexibility.
So, I think it's possible to implement a new AutoloadingSourceLocator that takes no constructor dependencies, which, funnily enough, would probably need to rely on the internal ReflectionClass API, e.g.:
// if it's a class we're loading...
$reflectionClass = new \ReflectionClass($identifier->getName());
$filename = $reflectionClass->getFileName();
$source = file_get_contents($filename);
// use these to make the LocatedSource...
Additionally, we'd need to create a "static" reflector, which would be something like:
class StaticReflector
{
public static function reflect(...)
{
return (new Reflector(new AutoloadingSourceLocator()))->reflect($identifier);
}
}
Then we can use this reflector in the constructors or something to reflect (still a bit hazy on how to actually implement this). The downside of this is it means we do have to LOAD the classes. So I also propose making a LoadSafeSourceLocator interface, which doesn't have any methods, but purely indicates that it "by design" does NOT load up classes into memory. Then in the ..\Reflector\Generic class, we can check if $reflection implements LoadSafeSourceLocator, and ensure that the class is not loaded beforehand, and is not loaded after reflection, to ensure the contract is obeyed.
TL;DR
- Create new
AutoloadingSourceLocator that uses core refleciton API to locate source code filename
- Cleanup constructors so they are empty
- Create
StaticReflector that uses AutoloadingSourceLocator
- Update
Reflection* classes to use AutoloadingSourceLocator
- Create
LoadSafeSourceLocator interface, any source locator that implements this interface MUST ensure the class does not get loaded ever. These would be the Composer, SingleFile and String source locators, as these do not load file. AutoloadingSourceLocator would load the file, so MUST NOT implement the LoadSafeSourceLocator interface.
- This allows us to do the following (i.e. replicate almost exactly the core reflection API instantiation technique):
$reflectionClass = new \BetterReflection\Reflection\ReflectionClass('My\Foo\Bar');
In order to automatically reflect using the exact same API as the core
Reflection*classes, we need to be able to essentially ignore the wholeSourceLocatorconcept, despite it's flexibility.So, I think it's possible to implement a new
AutoloadingSourceLocatorthat takes no constructor dependencies, which, funnily enough, would probably need to rely on the internalReflectionClassAPI, e.g.:Additionally, we'd need to create a "static" reflector, which would be something like:
Then we can use this reflector in the constructors or something to reflect (still a bit hazy on how to actually implement this). The downside of this is it means we do have to LOAD the classes. So I also propose making a
LoadSafeSourceLocatorinterface, which doesn't have any methods, but purely indicates that it "by design" does NOT load up classes into memory. Then in the..\Reflector\Genericclass, we can check if$reflection implements LoadSafeSourceLocator, and ensure that the class is not loaded beforehand, and is not loaded after reflection, to ensure the contract is obeyed.TL;DR
AutoloadingSourceLocatorthat uses core refleciton API to locate source code filenameStaticReflectorthat usesAutoloadingSourceLocatorReflection*classes to useAutoloadingSourceLocatorLoadSafeSourceLocatorinterface, any source locator that implements this interface MUST ensure the class does not get loaded ever. These would be theComposer,SingleFileandStringsource locators, as these do not load file.AutoloadingSourceLocatorwould load the file, so MUST NOT implement theLoadSafeSourceLocatorinterface.