Hi, Recoil developer here. Recoil can definitely be used for your purposes, but it's really syntactic sugar designed to take things one step further than promises - no callbacks is a feature, not a drawback ;)
Internally, Recoil uses ReactPHP which is
one of a few implementations of an asynchronous event-loop for PHP. The other most notable implementation is, in my opinion, Amp which several people have already mentioned.
If you want to perform concurrent database requests in PHP, one of these event-loop implementations is likely your best bet, especially if you'd like to work with promises. Both React and Amp offer promise implementations and MySQL clients; though I've not personally used MySQL asynchronously with PHP.
One thing to keep in mind is that the event-loop implementation you choose dictates what type of IO requests you can make. Any concurrent IO you want to perform must be handled by the same event-loop. This means that synchronous libraries (such as PDO) can not be used.
ReactPHP has a list of users, which is basically an index of projects that a written to use its event loop. Amp has a similar list of official packages.
The world of asynchronous PHP is still in its infancy, but there is a growing number of developers who are using these approaches successfully in production systems. At the very least it's great to have these options available when "just rewrite it in Node/Go/whatever" isn't a realistic approach for your project!
For what little it's worth, I also wrote this before learning about Box. Same idea again, but with the goal of deriving as much information as possible from the composer.json file (eg, the CLI entry point).
Edit: Apologies for entirely absent documentation :(
I put tests in the same namespace as (but different directory to) the class being tested, suffixed with "Test". That way PHPUnit can automatically determine which classes your tests cover, obviating the need for @covers annotations at the class level.
You just need enable the mapTestClassNameToCoveredClassName in your PHPUnit configuration file. Here's an example.
Thanks :) I think you may be right regarding the naming. I'm not sure if I'd want to rename at this point, but at any rate "fiber" does seem to be correct. Come to think of it, I have no idea where I got the name "strand" from at all, maybe I was just trying to be clever.
This is a tough question to answer well because many of the things you mention might very well be used along with Recoil, but are not directly related. I'll do my best :)
The idea of persistent resource allocation, while not the goal of Recoil, should indeed be possible. You could associate a strand with each user/session and resume it on each request, though this is probably not a good use of resources. It's also worth stating that such an application would be possible with any asynchronous framework that allowed you to keep a process running persistently.
It's also not directly related to websockets, though again PHP can serve websocket requests asynchronously with or without Recoil (see Ratchet, an awesome websocket server implementation also based on React).
The cold truth is, Recoil doesn't allow you to do anything you can't already do. What it does do is change the way you write asynchronous code such that it more closely resembles the synchronous code we're all used to - you call a function and a value is returned (or an exception is thrown), simple as that.
There is some example code in the repository that shows how a problem might be solved with "vanilla" React vs with Recoil:
These aren't the simplest examples to follow if you're not already familiar with asynchronous programming in PHP using promises. What should be noticeable is that, other than the presence of the yield keyword, the Recoil example looks very much like regular PHP code, whereas the first example looks a little more like what we're used to in JavaScript - lots of closures and callback passing - though the use of promises goes a long way to improving readability. In my opinion co-routine based code such as Recoil allows makes for a more easily maintainable application.
This isn't really an ELI5, I'm sorry, but hopefully I've gone some way towards a helpful explanation.
On the topic of adoption, one of the major drawbacks of such a system is that to see widespread use there really needs to be a good set of asynchronous libraries for all the things we already do so frequently in the PHP world (pdo, redis, etc), and furthermore those libraries need to be built on the same event loop in order to be interoperable.
I'm hoping that by basing the kernel on React and trying to make the use of React's promise-based code feel very native inside Recoil that some of the issues are are mitigated to some degree. There is a growing group of people doing some cool stuff with React.
I'm not sure what kind of code you're interested in generating, but I wrote a library for generating PHP code a while ago. It's circling the drain as I haven't used it in a long time but it might offer some ideas. You build a syntax tree programatically and the library converts the tree back into code. You can see an example of it in use here.
Personally, I don't consider the directory structure to be part of the "public API" and hence have only been bumping the patch version for releases that switch PSR-0 to PSR-4. I can get away with this because the only "supported" installation method for my packages is composer, and hence nobody using it normally should be affected... if anybody is even using the stuff!
The error was displayed in a pop-up on the packagist site after adding a new package that was using a psr-4 autoloader. Perhaps it's relevant that it was a new package.
Local composer install has been updated and everything appeared fine on that end, I could update my deps and generate a new autoloader using psr-4 without any issues.
I've got another package to add, I'll see if I can reproduce it.
Edit: turns out I did get an email as well:
[Composer\Repository\InvalidRepositoryException]: Some branches contained invalid data and were discarded, it is advised to review the log and fix any issues present in branches
Reading composer.json of recoil/recoil (develop)
Importing branch develop (dev-develop)
Skipped branch develop, Invalid package information:
autoload : invalid value (psr-4), must be one of psr-0, classmap, files
Edit again: I tried updating the same package to PSR-4 again now that it's already in the registry - same error. 'composer validate' passes.
Once more: I asked about this on #composer IRC channel, apparently this is to be expected atm. Seems fair enough given the recommendation to wait a while before updating our packages. https://github.com/composer/packagist/issues/373
This is probably the most important PHP RFC I've seen to date. What are the official channels, if any, for everyday members of the PHP community to offer support for this?
If you're going the OO route, I suggest teaching about the concept of dependency injection.
When I think back to a beginner programming cause I took, we were taught about classes, interfaces, polymorphism, etc, and this is great because they offered us a way to map real-world ideas to code in a way that is somewhat familiar to non-developers.
Then invariably somebody will mumble something about 'separation of concerns' and decoupling, but it's far less common that people are shown how object composition using dependency injection helps to deliver these things.
A number of developers that I have introduced to the concept have stated things like "oh, now OOP really makes sense!", so I just don't think it can be understated :)
I'm not convinced PHAR is a bad idea for the web - as someone else mentioned the op-code caches support it, which would be my primary area of concern. I'm sure there will be other issues, but I just haven't got around to trying it yet :)
I have two projects that work towards some of the same goals:
"near" builds a .phar directly from a composer package by looking at the composer.json
"near-assets" is a libary to assist in embedding/extracting non-PHP assets, but it's very immature, in fact I can't remember when I last got a chance to work on it.
We use Near pretty regularly for packing up projects that have CLI tools, but not yet for web-based projects. Still, maybe these can be of some help :)
Edit: In regards to the build steps, we always build everything into the phar on our CI server, including composer dependencies. This means that during deploy on the production servers there is nothing else that has to happen to activate a release (ie, if packagist/github are down we are unaffected, which has saved us once or twice)
Are there specific things you're referring to not working well enough?
We're trialing a PHP websocket server at work - I'm still trying to work out if I should stick with PHP (what my team is primarily familiar with, and what the rest of the project is using) or use node.js for this particular case.
(Though I still think cycle collection was worthy of mention in that article! :)
No mention of the 5.4 GC which can collect unreachable circular references?
I'd hardly advocate for PHP as the best choice for a daemon, but I'm not 100% convinced those particular arguments stack up for modern PHP. Most of them apply equally to any other language that uses reference counting.
Hey, so this is my repo.. how surreal. The README is just a default template at the moment, so if it's not obvious "woodhouse" is actually a command line utility we use inside another project (IcecaveStudios/testing) which actually runs the tests and knows what content to publish.
The "testing" package is a dependency we include in all our projects to ensure consistent configuration of Travis and PHPUnit, etc. I'm currently working a new version of "testing" that should make it a lot more useful to the rest of the world, as at the moment it makes a lot of assumptions about the way your repository is organised.
I probably shouldn't have used the word "factory", all I really meant was alternative construction logic, as one would achieve with overloaded constructors in C++, et al.
Hi, Recoil developer here. Recoil can definitely be used for your purposes, but it's really syntactic sugar designed to take things one step further than promises - no callbacks is a feature, not a drawback ;)
Internally, Recoil uses ReactPHP which is one of a few implementations of an asynchronous event-loop for PHP. The other most notable implementation is, in my opinion, Amp which several people have already mentioned.
If you want to perform concurrent database requests in PHP, one of these event-loop implementations is likely your best bet, especially if you'd like to work with promises. Both React and Amp offer promise implementations and MySQL clients; though I've not personally used MySQL asynchronously with PHP.
One thing to keep in mind is that the event-loop implementation you choose dictates what type of IO requests you can make. Any concurrent IO you want to perform must be handled by the same event-loop. This means that synchronous libraries (such as PDO) can not be used.
ReactPHP has a list of users, which is basically an index of projects that a written to use its event loop. Amp has a similar list of official packages.
The world of asynchronous PHP is still in its infancy, but there is a growing number of developers who are using these approaches successfully in production systems. At the very least it's great to have these options available when "just rewrite it in Node/Go/whatever" isn't a realistic approach for your project!
I hope this is helpful :)
Shameless plug in case such a thing is useful to anybody: https://github.com/IcecaveStudios/duct
For what little it's worth, I also wrote this before learning about Box. Same idea again, but with the goal of deriving as much information as possible from the composer.json file (eg, the CLI entry point).
Edit: Apologies for entirely absent documentation :(
I put tests in the same namespace as (but different directory to) the class being tested, suffixed with "Test". That way PHPUnit can automatically determine which classes your tests cover, obviating the need for
@coversannotations at the class level.You just need enable the
mapTestClassNameToCoveredClassNamein your PHPUnit configuration file. Here's an example.I'm not sure if you're aware but the SSL cert for this domain is incorrect (wildcard cert for *.piwik.org) which might be turning some people off.
Thanks :) I think you may be right regarding the naming. I'm not sure if I'd want to rename at this point, but at any rate "fiber" does seem to be correct. Come to think of it, I have no idea where I got the name "strand" from at all, maybe I was just trying to be clever.
This is a tough question to answer well because many of the things you mention might very well be used along with Recoil, but are not directly related. I'll do my best :)
The idea of persistent resource allocation, while not the goal of Recoil, should indeed be possible. You could associate a strand with each user/session and resume it on each request, though this is probably not a good use of resources. It's also worth stating that such an application would be possible with any asynchronous framework that allowed you to keep a process running persistently.
It's also not directly related to websockets, though again PHP can serve websocket requests asynchronously with or without Recoil (see Ratchet, an awesome websocket server implementation also based on React).
The cold truth is, Recoil doesn't allow you to do anything you can't already do. What it does do is change the way you write asynchronous code such that it more closely resembles the synchronous code we're all used to - you call a function and a value is returned (or an exception is thrown), simple as that.
There is some example code in the repository that shows how a problem might be solved with "vanilla" React vs with Recoil:
Concurrent DNS resolution:
These aren't the simplest examples to follow if you're not already familiar with asynchronous programming in PHP using promises. What should be noticeable is that, other than the presence of the
yieldkeyword, the Recoil example looks very much like regular PHP code, whereas the first example looks a little more like what we're used to in JavaScript - lots of closures and callback passing - though the use of promises goes a long way to improving readability. In my opinion co-routine based code such as Recoil allows makes for a more easily maintainable application.This isn't really an ELI5, I'm sorry, but hopefully I've gone some way towards a helpful explanation.
I'm sure I lifted some stuff from your library too ;) /u/igorw pointed me there during a discussion at some point.
On the topic of adoption, one of the major drawbacks of such a system is that to see widespread use there really needs to be a good set of asynchronous libraries for all the things we already do so frequently in the PHP world (pdo, redis, etc), and furthermore those libraries need to be built on the same event loop in order to be interoperable.
I'm hoping that by basing the kernel on React and trying to make the use of React's promise-based code feel very native inside Recoil that some of the issues are are mitigated to some degree. There is a growing group of people doing some cool stuff with React.
Thanks for your interest :)
I agree with the other replies. In general you should not be doing this, for the reasons they have specified... but...
If you absolutely, positively, have to do so, you can use liberator, which is a library that a colleague wrote for exactly this purpose.
<shamlessplug>You might get some use out of this library I wrote - https://github.com/IcecaveStudios/isolator</shamlessplug>
I'm not sure what kind of code you're interested in generating, but I wrote a library for generating PHP code a while ago. It's circling the drain as I haven't used it in a long time but it might offer some ideas. You build a syntax tree programatically and the library converts the tree back into code. You can see an example of it in use here.
Personally, I don't consider the directory structure to be part of the "public API" and hence have only been bumping the patch version for releases that switch PSR-0 to PSR-4. I can get away with this because the only "supported" installation method for my packages is composer, and hence nobody using it normally should be affected... if anybody is even using the stuff!
+1 again. Personally, I would love to see an standalone 'evented' stream PSR just as much as an HTTP one.
The error was displayed in a pop-up on the packagist site after adding a new package that was using a psr-4 autoloader. Perhaps it's relevant that it was a new package.
Local composer install has been updated and everything appeared fine on that end, I could update my deps and generate a new autoloader using psr-4 without any issues.
I've got another package to add, I'll see if I can reproduce it.
Edit: turns out I did get an email as well:
Edit again: I tried updating the same package to PSR-4 again now that it's already in the registry - same error. 'composer validate' passes.
Once more: I asked about this on #composer IRC channel, apparently this is to be expected atm. Seems fair enough given the recommendation to wait a while before updating our packages. https://github.com/composer/packagist/issues/373
I don't remember the exact wording but it was saying that composer.json was invalid because "psr-4" was not a valid autoloader type.
I tried this out on one of my alpha packages, it seems like Packagist itself doesn't yet support PSR-4, I'm sure it's not far away though :)
This is probably the most important PHP RFC I've seen to date. What are the official channels, if any, for everyday members of the PHP community to offer support for this?
If you're going the OO route, I suggest teaching about the concept of dependency injection.
When I think back to a beginner programming cause I took, we were taught about classes, interfaces, polymorphism, etc, and this is great because they offered us a way to map real-world ideas to code in a way that is somewhat familiar to non-developers.
Then invariably somebody will mumble something about 'separation of concerns' and decoupling, but it's far less common that people are shown how object composition using dependency injection helps to deliver these things.
A number of developers that I have introduced to the concept have stated things like "oh, now OOP really makes sense!", so I just don't think it can be understated :)
Edit: clarity
I'm not convinced PHAR is a bad idea for the web - as someone else mentioned the op-code caches support it, which would be my primary area of concern. I'm sure there will be other issues, but I just haven't got around to trying it yet :)
I have two projects that work towards some of the same goals:
"near" builds a .phar directly from a composer package by looking at the composer.json
"near-assets" is a libary to assist in embedding/extracting non-PHP assets, but it's very immature, in fact I can't remember when I last got a chance to work on it.
We use Near pretty regularly for packing up projects that have CLI tools, but not yet for web-based projects. Still, maybe these can be of some help :)
Edit: In regards to the build steps, we always build everything into the phar on our CI server, including composer dependencies. This means that during deploy on the production servers there is nothing else that has to happen to activate a release (ie, if packagist/github are down we are unaffected, which has saved us once or twice)
Ahh right you are, it was 5.3.
Are there specific things you're referring to not working well enough? We're trialing a PHP websocket server at work - I'm still trying to work out if I should stick with PHP (what my team is primarily familiar with, and what the rest of the project is using) or use node.js for this particular case.
(Though I still think cycle collection was worthy of mention in that article! :)
No mention of the 5.4 GC which can collect unreachable circular references?
I'd hardly advocate for PHP as the best choice for a daemon, but I'm not 100% convinced those particular arguments stack up for modern PHP. Most of them apply equally to any other language that uses reference counting.
Hey, so this is my repo.. how surreal. The README is just a default template at the moment, so if it's not obvious "woodhouse" is actually a command line utility we use inside another project (IcecaveStudios/testing) which actually runs the tests and knows what content to publish.
The "testing" package is a dependency we include in all our projects to ensure consistent configuration of Travis and PHPUnit, etc. I'm currently working a new version of "testing" that should make it a lot more useful to the rest of the world, as at the moment it makes a lot of assumptions about the way your repository is organised.
I probably shouldn't have used the word "factory", all I really meant was alternative construction logic, as one would achieve with overloaded constructors in C++, et al.