Skip to content

Instantly share code, notes, and snippets.

View kelunik's full-sized avatar

Niklas Keller kelunik

View GitHub Profile
@kelunik
kelunik / example.php
Created April 17, 2019 16:41
Short closure return value handling
<?php
class EventManager {
private $handlers;
public function onEvent(callable $callback) {
$this->handlers[] = $callback;
}
public function invoke(Event $event) {
<?php
use Amp\Loop;
use Amp\Mysql\ConnectionConfig;
use Amp\Mysql\Pool;
use Amp\Mysql\ResultSet;
use Amp\Mysql\TimeoutConnector;
use Amp\Socket\ClientTlsContext;
use Illuminate\Support\Collection;
<?php
namespace Amp\Http\Server\FormParser;
final class Field {
/** @var string */
private $name;
/** @var string */
private $value;
<?php
require __DIR__ . '/vendor/autoload.php';
use Amp\Loop;
use Amp\Artax;
$queue = new SplQueue;
$waiting = new Amp\Deferred;
$http = new Artax\DefaultClient;
____ ____ __ _
/ _/___ / __/__ _____/ /_(_)___ ____
/ // __ \/ /_/ _ \/ ___/ __/ / __ \/ __ \
_/ // / / / __/ __/ /__/ /_/ / /_/ / / / /
/___/_/ /_/_/ \___/\___/\__/_/\____/_/ /_/
Running initial test suite...
PHPUnit version: 6.5.7
<?php
use Amp\Iterator;
use Amp\Cluster\Listener;
use Psr\Log\LoggerInterface as PsrLogger;
Amp\Loop::run(function () {
if (Amp\Cluster::isWorker()) {
$socket = yield Amp\Cluster::listen("tcp://0.0.0.0:8080");
$logger = Amp\Cluster::getLogger();
<?php
require __DIR__ . "/vendor/autoload.php";
$http = new Amp\Artax\Client;
$promises = [
async $http->request("https://google.com/"),
async $http->request("https://stackoverflow.com/"),
];

You can use amphp/react-adapter + amphp/amp, which will give you an ev based implementation – if ev is installed – that is fully compatible with ReactPHP's LoopInterface.

$driver = (new Amp\Loop\DriverFactory)->create();
$loop = new Amp\ReactAdapter\ReactAdapter($driver);

// $loop is now a ReactPHP compatible driver based on ext-ev

If you don't want the automatic driver selection based on the available extensions, you can also instantiate a driver on your own using $driver = new Amp\Loop\EvDriver;.

@kelunik
kelunik / polygon.txt
Created February 20, 2018 10:12
Polygon Intersection
______
____/ \
/ \ |
| | /
\_____|____/
#!/bin/bash
set -e
set -x
rm -rf ~/.php-build
mkdir ~/.php-build
cd ~/.php-build
if [ $1 == "master" ]