2017 © Pedro Peláez
 

library node

An asynchronous PHP framework with and event loop (node.js-like)

image

iankuca/node

An asynchronous PHP framework with and event loop (node.js-like)

  • Thursday, November 1, 2012
  • by iankuca
  • Repository
  • 1 Watchers
  • 7 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

node.php, (*1)

An asynchronous PHP framework with an event loop (node.js-like), (*2)

Installation

The preferred way is to use composer., (*3)

{
  "require": { "iankuca/node": "*" }
}

Or just use this repository directly., (*4)

Usage

$ php -f main.php yourfile.php

Examples

Timeouts

<?php
echo "A";
setTimeout(function () {
  echo "C";
}, 2000);
echo "B";

This prints AB and adds C after 2 seconds. Then it exits., (*5)

HTTP Server

<?php
$server = \Node\HTTP::createServer(function ($req, $res) {
  $res->writeHead(200, array(
    'content-type' => 'text/plain; charset=UTF-8'
  ));
  $res->write('Hello world!');
  $res->end();
});

$port = 8080;
$server->listen($port, 'localhost');
console_log('The HTTP server is listening on port %d.', $port);

This creates an HTTP server listening on the port 8080. It writes Hello world! to each response. It does not exit by itself., (*6)

HTTP Request

<?php
$options = array(
  'host' => 'ifconfig.me',
  'path' => '/ip'
);

\Node\HTTP::request($options, function ($res) {
  console_log('status: %d', $res->status);
  print_r($res->headers);

  $res->on('data', function ($chunk) {
    echo $chunk;
  });
});

This makes an HTTP GET request to http://ifoconfig.me/ip and asynchronously calls the provided callback function when a response object is ready. If waits for response body. Then it exits., (*7)

The standard output would be…, (*8)

status: 200
Array
(...)
XX.XX.XX.XX

Directory Listing

<?php
\Node\FS::readdir('/tmp', function ($err, $files) {
  if ($err) throw $err;
  print_r($files);
});

Spawns a child ls -a /tmp process and asynchronously calls the provided callback function when done. Then it exits., (*9)

Logging

Since the standard output of the process does not go to the browser, eventual exceptions and warnings are visible in the terminal window. The native exception stringifier is pretty horrible which is why node.php includes its own error formatter. Exceptions are now a lot nicer:, (*10)

Exception Example, (*11)

Another addition is the console_log() function which is basically sprintf that outputs to the standard output with an added end-of-line (\n) character., (*12)

```php console_log('%d + %d = %s', 1, 2, 'awesome'); // stdout: 1 + 2 = awesome, (*13)

The Versions

01/11 2012

dev-master

9999999-dev https://github.com/jankuca/node.php

An asynchronous PHP framework with and event loop (node.js-like)

  Sources   Download

Do What The Fuck You Want To Public License

The Requires

  • php >=5.3.0

 

framework events asynchronous async node

01/11 2012

v0.0.4

0.0.4.0 https://github.com/jankuca/node.php

An asynchronous PHP framework with and event loop (node.js-like)

  Sources   Download

Do What The Fuck You Want To Public License

The Requires

  • php >=5.3.0

 

framework events asynchronous async node

01/11 2012

v0.0.3

0.0.3.0 https://github.com/jankuca/node.php

An asynchronous PHP framework with and event loop (node.js-like)

  Sources   Download

Do What The Fuck You Want To Public License

The Requires

  • php >=5.3.0

 

framework events asynchronous async node

31/10 2012

v0.0.2

0.0.2.0 https://github.com/jankuca/node.php

An asynchronous PHP framework with and event loop (node.js-like)

  Sources   Download

Do What The Fuck You Want To Public License

The Requires

  • php >=5.3.0

 

framework events asynchronous async node

31/10 2012

v0.0.1

0.0.1.0 https://github.com/jankuca/node.php

An asynchronous PHP framework with and event loop (node.js-like)

  Sources   Download

Do What The Fuck You Want To Public License

The Requires

  • php >=5.3.0

 

framework events asynchronous async node