Flow-based programming for PHP
Join the DZone community and get the full member experience.
Join For FreeYou may have seen my earlier post about NoFlo, the flow-based programming tool I've written for Node.js. It allows you to do quite cool stuff, like a visually controlled web server:
Yesterday Igor Wiedler published Evenement, a PHP port of the EventEmitter class from Node.js. As NoFlo builds quite heavily on EventEmitter, I decided to see how far the PHP port could be taken.
As result, there is now PhpFlo, a flow-based programming environment for PHP.
Example of how to define and run a flow (you can also use a JSON format for this):
// Add nodes to the graph $graph = new PhpFlo\Graph("linecount"); $graph->addNode("Read File", "ReadFile"); $graph->addNode("Split by Lines", "SplitStr"); $graph->addNode("Count Lines", "Counter"); $graph->addNode("Display", "Output"); // Add connections between nodes $graph->addEdge("Read File", "out", "Split by Lines", "in"); $graph->addEdge("Read File", "error", "Display", "in"); $graph->addEdge("Split by Lines", "out", "Count Lines", "in"); $graph->addEdge("Count Lines", "count", "Display", "in"); // Kick-start the process by sending filename to Read File $graph->addInitial($fileName, "Read File", "source"); // Make the graph "live" $network = PhpFlo\Network::create($graph);
The flow consists of processes, or instances simple "black box" components that have their own defined input and output ports. Program logic is defined by making connections between them. Here is a simple component that reads the contents of a file:
namespace PhpFlo\Component; use PhpFlo\Component; use PhpFlo\Port; class ReadFile extends Component { public function __construct() { $this->inPorts['source'] = new Port(); $this->outPorts['out'] = new Port(); $this->outPorts['error'] = new Port(); $this->inPorts['source']->on('data', array($this, 'readFile')); } public function readFile($data) { if (!file_exists($data)) { $this->outPorts['error']->send("File {$data} doesn't exist"); return; } $this->outPorts['out']->send(file_get_contents($data)); $this->outPorts['out']->disconnect(); } }
I hope people find this system useful. If you're interested in FBP, then J. Paul Morrison's book is a good place to start.
And if you're in FrOSCon, feel free to come and chat with me :-)
Source: http://bergie.iki.fi/blog/flow-based_programming_for_php/
Opinions expressed by DZone contributors are their own.
Trending
-
How To Check IP Addresses for Known Threats and Tor Exit Node Servers in Java
-
How To Use an Automatic Sequence Diagram Generator
-
21 Hidden Careers in the AI Revolution: Driving Change in the Tech Industry
-
Build a Simple Chat Server With gRPC in .Net Core
Comments