Open marcioAlmada opened 8 years ago
Hello, I've made an attempt at writing a composer plugin for Real-Time mode integration with Yay, and it seems that all that needs to be done is a plugin that patches ClassLoader.php's includeFile function to simply prepend the yay:// wrapper before the filename?
Yes, that's one of the possibilities. But patching ClassLoader.php is a little too ad hoc to my taste and probably won't last long.
The other idea is to hijack the composer autoloader instance and force everything to be included through yay://
, but I havent checked if it's possible yet (no time):
$autoloader = include 'vendor/autoload.php';
// do some sorcery with $autoloader instance
And there is also the more formal approach to ask composer project for a configuration or an API change on autoloader that allows registering a wrapper for inclusion.
$autoloader->addWrapper(\Yay\StreamWrapper::PROTOCOL);
Also, for the real time mode to work decently we would need the stream-is-cacheable RFC to pass. So, any support to pass this RFC (author François Laupretre) is welcome :)
Hello
I've looked at the composer autoloader and composer plugins only work the cli level, not at the autoloader level so the only way to overload the autoloader is to subscribe to the post-autoloader-dump hook and modify the autoloader after dumping is finished. So I think patching the ClassLoader is the only way since ClassLoader is where the include statement is at and ClassLoader is copy-pasted, not generated so no modification of Composer instance would change anything in ClassLoader unless we edit the file directly.
It's also possible to simply have a cache directory, where the code is dumped postprocessed, and the Composer autoloader can be re-wired to include from that directory as a prefix but then it would not be "real-time".
You could make the files themselves have an explicit call to yay at the top of the file, avoiding any magic:
return \Yay\compile(__FILE__);
This statement would either get yay to compile this file, or look it up in the cache, and in both cases then execute it.
Yay would strip that line out when the file is compiled, obviously.
@TazeTSchnitzel you genius :smirk:
I still want composer integration but that's indeed a really cool idea.
:D
I just realised that this wouldn't work if what followed wasn't valid PHP syntax.
But you could use __halt_compiler
to mitigate that.
Yes of course, otherwise it would fail upon parsing. I'm building a sample bootstrap project to test all the possibilities.
@TazeTSchnitzel You sir, are a genius! Thanks for that solution.
it... works https://github.com/marcioAlmada/yay-enums/blob/master/enum.tests.php :manic_laugh:
We are going to use a small php extension for engine integration instead. See #32.
Can the macros be executed on a function execution, I was looking for something like lisp-like macros.
Why cant we do this in the entry file?
$autoload = require __DIR__.'/vendor/autoload.php';
spl_autoload_unregister(array($autoload, 'loadClass'));
function includeFile($file)
{
include $file;
echo "CALLED: {$file} \n";
}
spl_autoload_register(function($class) use ($autoload) {
if ($file = $autoload->findFile($class)) {
includeFile($file);
return true;
}
}, true, true);
@sunel I wouldn't personally under any circumstance bypass/overload the composer autloading. I do believe composer has some 'event'-based system for autloading plugins, I think it is better to utilize that feature, imho
I dont think so, composer has plugin system for package event and installer events.
@sunel I see a decision has been made, see #32 ;)
@sunel https://github.com/preprocess - uses YAY as its base - and If I'm not mistaken it was made by modifying composer autoload.
@marcioAlmada ya it uses YAY "preprocess" uses a different file extension "pre".
@sunel this is so that the preprocessing is opt-in per file. Could look at configuration to find some other way to opt-in and some other scheme for the file creation, but I think it will always be opt-in per file.
<?php declare(enable_preprocessor=1);
This is indistinguishable from trolling, I'll let you decide :sweat_smile:
There is an example on how to hook composer's autoloader: https://github.com/ircmaxell/PhpGenerics/blob/master/lib/bootstrap.php
Should allow to activate pre processing of every included file.
[x] A stream wrapper should be available for single file inclusion: