A minimalistic implementation of Promises for PHP

Overview

libPromise

A minimalistic implementation of Promises for PHP.

Installation via DEVirion

Install the DEVirion plugin and start your server. This will create a virions folder in your server's root directory.

server_root
| -> plugins
|    --> DEVirion.phar
| -> virions
  • Download pre-compiled .phar files can be downloaded from poggit.
  • Place the pre-compiled .phar in the virions directory

Running from source code

Clone the repository via git git clone [email protected]:cooldogedev/libPromise.git. This will create a libPromise folder in your directory.

your_plugin
| -> src
|    --> cooldogedev
|       --> libPromise
  • Place the cooldogedev\libPromise folder in your src directory.

Promise Examples

Hello world

$promise = new Promise(
    function (): string {
        return "Hello world";
    }
);

$promise
    ->then(fn(string $response) => var_dump($response))
    ->settle();

JSON decoder

$promise = new Promise(
    function (): string {
        $person = [
            "name" => "john",
            "lastName" => "smith",
            "age" => 40
        ];

        return json_encode($person);
    }
);

$promise
    ->then(fn(string $response): array => json_decode($response, true))
    ->then(fn(array $response) => var_dump($response))
    ->settle();

ThreadedPromise Examples

ThreadedPromise that returns a promise

$pool = new PromisePool($this);

$promise = new ThreadedPromise(
    function (): ThreadedPromise {
        return new ThreadedPromise(fn(string $response) => var_dump("a nested promise"));
    },
    function (ThreadedPromise $promise): void {
        /**
         * You can convert the ThreadedPromise to a regular Promise via @link ThreadedPromise::asPromise()
         */
        $promise->settle();
    }
);

$pool->addPromise($promise);

Coinbase API endpoint fetcher

$pool = new PromisePool($this);

$promise = new ThreadedPromise(
    function (): string {
        $request = Internet::getURL("https://api.coinbase.com/v2/currencies");
        return $request->getBody();
    }
);

$promise
    ->then(fn(string $response): array => json_decode($response, true))
    ->then(fn(array $response) => var_dump($response))
    ->catch(fn(PromiseError $error) => var_dump($error->getMessage()));

$pool->addPromise($promise);

Tell online players a random joke

$pool = new PromisePool($this);

$promise = new ThreadedPromise(
    function (): string {
        $request = Internet::getURL("https://v2.jokeapi.dev/joke/Any");
        return $request->getBody();
    },
    function (array $response): void {
        foreach ($this->getServer()->getOnlinePlayers() as $onlinePlayer) {
            $onlinePlayer->sendMessage($response["setup"]);
            $onlinePlayer->sendMessage($response["delivery"]);
        }
    }
);

$promise
    ->then(fn(string $response): array => json_decode($response, true))
    ->catch(fn(PromiseError $error) => var_dump($error->getMessage()));

$pool->addPromise($promise);

Projects using libPromise

You might also like...
PHP's Promse implementation depends on the Swoole module.

php-promise-swoole PHP's Promse implementation depends on the Swoole module. Promise::allsettled([ /** Timer 调用 */ /** Timer call */

A circular buffer implementation in PHP

Circular Buffer Installation 💡 This is a great place for showing how to install the package, see below: Run $ composer require lctrs/circular-buffer

This package contains a PHP implementation to solve 3D bin packing problems.

3D Bin Packager This package contains a PHP implementation to solve 3d bin packing problems based on gedex implementation on Go and enzoruiz implement

PHP implementation for reading and writing Apache Parquet files/streams

php-parquet This is the first parquet file format reader/writer implementation in PHP, based on the Thrift sources provided by the Apache Foundation.

PHP implementation of PSON

PSON-PHP Information This library is an php implementation of PSON. This software is licensed under the MIT License. Installation You can install this

An open-source Minecraft: Java Edition server implementation, written in PHP.
An open-source Minecraft: Java Edition server implementation, written in PHP.

PHPCraft An open-source Minecraft: Java Edition server implementation, written in PHP. What is PHPCraft? PHPCraft is an open-source Minecraft: Java Ed

Php-file-iterator - FilterIterator implementation that filters files based on a list of suffixes, prefixes, and other exclusion criteria.

php-file-iterator Installation You can add this library as a local, per-project dependency to your project using Composer: composer require phpunit/ph

phly-mustache is a Mustache implementation written for PHP

phly-mustache is a Mustache implementation written for PHP. It conforms to the principles of mustache, and allows for extension of the format via pragmas.

This is a JSONPath implementation for PHP based on Stefan Goessner's JSONPath script.
This is a JSONPath implementation for PHP based on Stefan Goessner's JSONPath script.

JSONPath for PHP This is a JSONPath implementation for PHP based on Stefan Goessner's JSONPath script. JSONPath is an XPath-like expression language f

Comments
  • Bug

    Bug

    Fatal error: Uncaught Error: Class "cooldogedev\libPromise\constant\PromiseState" not found in D:\PocketMine-MP\virions\libPromise\src\cooldogedev\libPromise\thread\ThreadedPromise.php:87
    Stack trace:
    #0 D:\PocketMine-MP\virions\libPromise\src\cooldogedev\libPromise\traits\SharedPromisePartsTrait.php(73): cooldogedev\libPromise\thread\ThreadedPromise->handleRejection()
    #1 D:\PocketMine-MP\virions\libPromise\src\cooldogedev\libPromise\thread\PromiseSettlerThread.php(73): cooldogedev\libPromise\thread\ThreadedPromise->settle()
    #2 phar://D:/PocketMine-MP/PocketMine-MP.phar/src/thread/CommonThreadPartsTrait.php(93): cooldogedev\libPromise\thread\PromiseSettlerThread->onRun()
    #3 [internal function]: pocketmine\thread\Thread->run()
    #4 {main}
      thrown in D:\PocketMine-MP\virions\libPromise\src\cooldogedev\libPromise\thread\ThreadedPromise.php on line 87
    

    there is no other thread running and i haven't changed anything

    Status: Unconfirmed 
    opened by Mcbeany 5
  • libPromise makes server take too long to stop

    libPromise makes server take too long to stop

    I can't explain more specifically, but after loading this plugin, every time I want to stop my server, it has to save a long time so it's forced to stop. without your plugin my server stops normally image

    bug 
    opened by Mcbeany 2
Releases(v0.0.6)
Owner
full stack developer
null
You have just downloaded "Messenger-app" [A lightweight, minimalistic real-time chat application]

MESSENGER-APP You have just downloaded "Messenger-app" [A lightweight, minimalistic real-time chat application] Setup To get it working, follow these

Chr1st0ph3r SAB 1 Oct 29, 2021
Minimalistic Light colour scheme for PhpStorm

A minimalistic light colour scheme for PhpStorm If you need convincing of a light colour scheme, I'd like to direct you to a post on the topic. Colour

Brent Roose 269 Dec 24, 2022
A pure PHP implementation of the MessagePack serialization format / msgpack.org[PHP]

msgpack.php A pure PHP implementation of the MessagePack serialization format. Features Fully compliant with the latest MessagePack specification, inc

Eugene Leonovich 368 Dec 19, 2022
Php-rpc-server - JSON RPC server implementation for PHP.

JSON RPC Server implementation for PHP. The json-rpc is a very simple protocol. You can see this by reading the protocol specification. This library i

null 4 Sep 28, 2022
A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

Felix Becker 1.1k Jan 4, 2023
PHP implementation of circuit breaker pattern.

What is php-circuit-breaker A component helping you gracefully handle outages and timeouts of external services (usually remote, 3rd party services).

ArturEjsmont 169 Jul 28, 2022
Implementation of the Token Bucket algorithm in PHP.

Token Bucket This is a threadsafe implementation of the Token Bucket algorithm in PHP. You can use a token bucket to limit an usage rate for a resourc

null 477 Jan 7, 2023
A PHP implementation of the Unleash protocol aka Feature Flags in GitLab.

A PHP implementation of the Unleash protocol aka Feature Flags in GitLab. This implementation conforms to the official Unleash standards and implement

Dominik Chrástecký 2 Aug 18, 2021
An implementation of the Minecraft: Bedrock Edition protocol in PHP

BedrockProtocol An implementation of the Minecraft: Bedrock Edition protocol in PHP This library implements all of the packets in the Minecraft: Bedro

PMMP 94 Jan 6, 2023
PHP Implementation of PASERK

PASERK (PHP) Platform Agnostic SERialized Keys. Requires PHP 7.1 or newer. PASERK Specification The PASERK Specification can be found in this reposito

Paragon Initiative Enterprises 9 Nov 22, 2022