Skip to content

Commit 5998788

Browse files
authored
Merge pull request #42 from CharlotteDunoisLabs/feature-supported-adapters
Add supported adapters static method
2 parents 7ae9728 + f4614c7 commit 5998788

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

src/AdapterInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
interface AdapterInterface
99
{
1010
const CREATION_MODE = 'rwxrw----';
11+
12+
/**
13+
* Checks whether the current installation supports the adapter.
14+
*
15+
* @return boolean
16+
*/
17+
public static function isSupported();
1118

1219
/**
1320
* Return the loop associated with this adapter.

src/ChildProcess/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ protected function setUpPool($options)
9090
});
9191
}
9292

93+
/**
94+
* @return boolean
95+
*/
96+
public static function isSupported()
97+
{
98+
return substr(strtolower(PHP_OS), 0, 3) !== 'win' && function_exists('proc_open');
99+
}
100+
93101
/**
94102
* @return LoopInterface
95103
*/

src/Eio/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ protected function applyConfiguration(array $options)
102102
$this->options = array_merge_recursive($this->options, $options);
103103
}
104104

105+
/**
106+
* @return boolean
107+
*/
108+
public static function isSupported()
109+
{
110+
return extension_loaded('eio');
111+
}
112+
105113
/**
106114
* {@inheritDoc}
107115
*/

src/Filesystem.php

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

33
namespace React\Filesystem;
44

5+
use RuntimeException;
56
use React\EventLoop\LoopInterface;
67
use React\Filesystem\Node;
78

@@ -16,14 +17,18 @@ class Filesystem implements FilesystemInterface
1617
* @param LoopInterface $loop
1718
* @param array $options
1819
* @return FilesystemInterface
20+
* @throws RuntimeException
1921
*/
2022
public static function create(LoopInterface $loop, array $options = [])
2123
{
22-
if (extension_loaded('eio')) {
23-
return static::setFilesystemOnAdapter(static::createFromAdapter(new Eio\Adapter($loop, $options)));
24+
$adapters = static::getSupportedAdapters();
25+
26+
if (!empty($adapters)) {
27+
$adapter = "\\React\\Filesystem\\".$adapters[0]."\\Adapter";
28+
return static::setFilesystemOnAdapter(static::createFromAdapter(new $adapter($loop, $options)));
2429
}
2530

26-
return static::setFilesystemOnAdapter(static::createFromAdapter(new ChildProcess\Adapter($loop, $options)));
31+
throw new RuntimeException('No supported adapter found for this installation');
2732
}
2833

2934
/**
@@ -45,6 +50,24 @@ protected static function setFilesystemOnAdapter(FilesystemInterface $filesystem
4550
return $filesystem;
4651
}
4752

53+
/**
54+
* @return string[]
55+
*/
56+
public static function getSupportedAdapters()
57+
{
58+
$adapters = [];
59+
60+
if (Eio\Adapter::isSupported()) {
61+
$adapters[] = 'Eio';
62+
}
63+
64+
if (ChildProcess\Adapter::isSupported()) {
65+
$adapters[] = 'ChildProcess';
66+
}
67+
68+
return $adapters;
69+
}
70+
4871
/**
4972
* Filesystem constructor.
5073
* @param AdapterInterface $adapter

src/FilesystemInterface.php

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

33
namespace React\Filesystem;
44

5+
use RuntimeException;
56
use React\EventLoop\LoopInterface;
67
use React\Filesystem\Node;
78

@@ -11,6 +12,7 @@ interface FilesystemInterface
1112
* @param LoopInterface $loop
1213
* @param array $options
1314
* @return FilesystemInterface
15+
* @throws RuntimeException
1416
*/
1517
public static function create(LoopInterface $loop, array $options = []);
1618

@@ -20,6 +22,11 @@ public static function create(LoopInterface $loop, array $options = []);
2022
*/
2123
public static function createFromAdapter(AdapterInterface $adapter);
2224

25+
/**
26+
* @return string[]
27+
*/
28+
public static function getSupportedAdapters();
29+
2330
/**
2431
* @return AdapterInterface
2532
*/

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function mockAdapter(LoopInterface $loop = null)
2727
'setFilesystem',
2828
'setInvoker',
2929
'callFilesystem',
30+
'isSupported',
3031
'mkdir',
3132
'rmdir',
3233
'unlink',

0 commit comments

Comments
 (0)