Skip to content

[RFC] Consider adding functional API #330

@clue

Description

@clue

Our OOP API has a pretty good, SOLID design – but let's face it, this often makes it hard to work with. For example, take a look at the following code required to connect to a remote host:

$loop = React\EventLoop\Factory::create();
$resolverFactory = new React\Dns\Resolver\Factory();
$resolver = $resolverFactory->create('8.8.8.8', $loop);
$connector = new Connector($loop, $resolver);
$secure = new SecureConnector($connector, $loop);

$secure->create('www.google.com', 443);

Our current OOP API suffers from the fact that we do not require any global/shared state. This means we can not assume any defaults for common setups and requires passing quite a few instances. Instances that are effectively only constructed once.

After discussing this with @cboden, I figured it's worth opening this ticket as a basis for further discussion / RFC:

This ticket suggests we add a much simpler API on top of the existing class hierarchy (and does not suggest replacing it!). So we're merely talking about some API sugar.

Note however, this comes at the cost of some global state.

At the lowest level, we need a global accessor for the current loop (this has originally been suggested by @igorw (reactphp/event-loop#10)):

$loop = \React\EventLoop\loop();

Stream component (requires global loop):

\React\Stream\fopen('demo.txt', 'r')->then(function (Stream $stream) {

});

DNS component (requires global loop and DNS Resolver):

\React\Dns\lookup('www.google.com')->then(function ($ip) {
    echo $ip;
});

SocketClient component (requires global loop and DNS Resolver):

\React\SocketClient\connect('google.com:80')->then(function (Connection $connection) {

});

Socket component (requires global loop):

\React\Socket\listen('localhost:1234')->then(function (Socket $socket) {
    $socket->on('connection', function () { });
});

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy