Skip to content

Commit b94bc8e

Browse files
authored
Merge pull request #153 from WyriHaximus-labs/throwing_constructor
Throw BadMethodCallException on manual loop creating when required extension isn't installed
2 parents 534ce37 + bf2e14b commit b94bc8e

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ You should use the [`Factory`](#factory) to automatically create a new instance.
149149
Advanced! If you explicitly need a certain event loop implementation, you can
150150
manually instantiate one of the following classes.
151151
Note that you may have to install the required PHP extensions for the respective
152-
event loop implementation first or this may result in a fatal error.
152+
event loop implementation first or they will throw a `BadMethodCallException` on creation.
153153

154154
#### StreamSelectLoop
155155

src/ExtEventLoop.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\EventLoop;
44

5+
use BadMethodCallException;
56
use Event;
67
use EventBase;
78
use EventConfig as EventBaseConfig;
@@ -38,6 +39,10 @@ final class ExtEventLoop implements LoopInterface
3839

3940
public function __construct()
4041
{
42+
if (!class_exists('EventBase', false)) {
43+
throw new BadMethodCallException('Cannot create ExtEventLoop, ext-event extension missing');
44+
}
45+
4146
$config = new EventBaseConfig();
4247
$config->requireFeatures(EventBaseConfig::FEATURE_FDS);
4348

src/ExtLibevLoop.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\EventLoop;
44

5+
use BadMethodCallException;
56
use libev\EventLoop;
67
use libev\IOEvent;
78
use libev\SignalEvent;
@@ -36,6 +37,10 @@ final class ExtLibevLoop implements LoopInterface
3637

3738
public function __construct()
3839
{
40+
if (!class_exists('libev\EventLoop', false)) {
41+
throw new BadMethodCallException('Cannot create ExtLibevLoop, ext-libev extension missing');
42+
}
43+
3944
$this->loop = new EventLoop();
4045
$this->futureTickQueue = new FutureTickQueue();
4146
$this->timerEvents = new SplObjectStorage();

src/ExtLibeventLoop.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\EventLoop;
44

5+
use BadMethodCallException;
56
use Event;
67
use EventBase;
78
use React\EventLoop\Tick\FutureTickQueue;
@@ -52,6 +53,10 @@ final class ExtLibeventLoop implements LoopInterface
5253

5354
public function __construct()
5455
{
56+
if (!function_exists('event_base_new')) {
57+
throw new BadMethodCallException('Cannot create ExtLibeventLoop, ext-libevent extension missing');
58+
}
59+
5560
$this->eventBase = event_base_new();
5661
$this->futureTickQueue = new FutureTickQueue();
5762
$this->timerEvents = new SplObjectStorage();

0 commit comments

Comments
 (0)