marcioAlmada / yay

Yay is a high level PHP preprocessor
https://github.com/marcioAlmada/yay
MIT License
572 stars 35 forks source link

Real Time Mode #11

Open marcioAlmada opened 8 years ago

marcioAlmada commented 8 years ago

Should allow to activate pre processing of every included file.

msjyoo commented 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?

marcioAlmada commented 8 years ago

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);
marcioAlmada commented 8 years ago

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 :)

msjyoo commented 8 years ago

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.

msjyoo commented 8 years ago

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".

hikari-no-yume commented 8 years ago

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.

marcioAlmada commented 8 years ago

@TazeTSchnitzel you genius :smirk:

I still want composer integration but that's indeed a really cool idea.

hikari-no-yume commented 8 years ago

:D

hikari-no-yume commented 8 years ago

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.

marcioAlmada commented 8 years ago

Yes of course, otherwise it would fail upon parsing. I'm building a sample bootstrap project to test all the possibilities.

msjyoo commented 8 years ago

@TazeTSchnitzel You sir, are a genius! Thanks for that solution.

marcioAlmada commented 8 years ago

it... works https://github.com/marcioAlmada/yay-enums/blob/master/enum.tests.php :manic_laugh:

marcioAlmada commented 8 years ago

We are going to use a small php extension for engine integration instead. See #32.

CMCDragonkai commented 7 years ago

Can the macros be executed on a function execution, I was looking for something like lisp-like macros.

sunel commented 6 years ago

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);
chris-kruining commented 6 years ago

@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

sunel commented 6 years ago

I dont think so, composer has plugin system for package event and installer events.

chris-kruining commented 6 years ago

@sunel I see a decision has been made, see #32 ;)

marcioAlmada commented 6 years ago

@sunel https://github.com/preprocess - uses YAY as its base - and If I'm not mistaken it was made by modifying composer autoload.

sunel commented 6 years ago

@marcioAlmada ya it uses YAY "preprocess" uses a different file extension "pre".

assertchris commented 5 years ago

@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.

marcioAlmada commented 5 years ago
<?php declare(enable_preprocessor=1);

This is indistinguishable from trolling, I'll let you decide :sweat_smile:

xtlsoft commented 5 years ago

There is an example on how to hook composer's autoloader: https://github.com/ircmaxell/PhpGenerics/blob/master/lib/bootstrap.php