Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
interface AdapterInterface
{
const CREATION_MODE = 'rwxrw----';

/**
* Checks whether the current installation supports the adapter.
*
* @return boolean
*/
public static function isSupported();

/**
* Return the loop associated with this adapter.
Expand Down
8 changes: 8 additions & 0 deletions src/ChildProcess/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ protected function setUpPool($options)
});
}

/**
* @return boolean
*/
public static function isSupported()
{
return substr(strtolower(PHP_OS), 0, 3) !== 'win' && function_exists('proc_open');
}

/**
* @return LoopInterface
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Eio/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ protected function applyConfiguration(array $options)
$this->options = array_merge_recursive($this->options, $options);
}

/**
* @return boolean
*/
public static function isSupported()
{
return extension_loaded('eio');
}

/**
* {@inheritDoc}
*/
Expand Down
29 changes: 26 additions & 3 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Filesystem;

use RuntimeException;
use React\EventLoop\LoopInterface;
use React\Filesystem\Node;

Expand All @@ -16,14 +17,18 @@ class Filesystem implements FilesystemInterface
* @param LoopInterface $loop
* @param array $options
* @return FilesystemInterface
* @throws RuntimeException
*/
public static function create(LoopInterface $loop, array $options = [])
{
if (extension_loaded('eio')) {
return static::setFilesystemOnAdapter(static::createFromAdapter(new Eio\Adapter($loop, $options)));
$adapters = static::getSupportedAdapters();

if (!empty($adapters)) {
$adapter = "\\React\\Filesystem\\".$adapters[0]."\\Adapter";
return static::setFilesystemOnAdapter(static::createFromAdapter(new $adapter($loop, $options)));
}

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

/**
Expand All @@ -45,6 +50,24 @@ protected static function setFilesystemOnAdapter(FilesystemInterface $filesystem
return $filesystem;
}

/**
* @return string[]
*/
public static function getSupportedAdapters()
{
$adapters = [];

if (Eio\Adapter::isSupported()) {
$adapters[] = 'Eio';
}

if (ChildProcess\Adapter::isSupported()) {
$adapters[] = 'ChildProcess';
}

return $adapters;
}

/**
* Filesystem constructor.
* @param AdapterInterface $adapter
Expand Down
7 changes: 7 additions & 0 deletions src/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Filesystem;

use RuntimeException;
use React\EventLoop\LoopInterface;
use React\Filesystem\Node;

Expand All @@ -11,6 +12,7 @@ interface FilesystemInterface
* @param LoopInterface $loop
* @param array $options
* @return FilesystemInterface
* @throws RuntimeException
*/
public static function create(LoopInterface $loop, array $options = []);

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

/**
* @return string[]
*/
public static function getSupportedAdapters();

/**
* @return AdapterInterface
*/
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function mockAdapter(LoopInterface $loop = null)
'setFilesystem',
'setInvoker',
'callFilesystem',
'isSupported',
'mkdir',
'rmdir',
'unlink',
Expand Down