From a83147ebc1ceedec3d64d94fa314219db630bb8d Mon Sep 17 00:00:00 2001 From: Charlotte Dunois Date: Thu, 21 Jun 2018 18:53:47 +0200 Subject: [PATCH] Add supported adapters static method --- src/AdapterInterface.php | 7 +++++++ src/ChildProcess/Adapter.php | 8 ++++++++ src/Eio/Adapter.php | 8 ++++++++ src/Filesystem.php | 29 ++++++++++++++++++++++++++--- src/FilesystemInterface.php | 7 +++++++ tests/TestCase.php | 1 + 6 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/AdapterInterface.php b/src/AdapterInterface.php index d919e34b..7faa711c 100644 --- a/src/AdapterInterface.php +++ b/src/AdapterInterface.php @@ -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. diff --git a/src/ChildProcess/Adapter.php b/src/ChildProcess/Adapter.php index 45fdd484..bf69186d 100644 --- a/src/ChildProcess/Adapter.php +++ b/src/ChildProcess/Adapter.php @@ -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 */ diff --git a/src/Eio/Adapter.php b/src/Eio/Adapter.php index 31f120ff..aee022a7 100644 --- a/src/Eio/Adapter.php +++ b/src/Eio/Adapter.php @@ -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} */ diff --git a/src/Filesystem.php b/src/Filesystem.php index b36dedf7..db596906 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -2,6 +2,7 @@ namespace React\Filesystem; +use RuntimeException; use React\EventLoop\LoopInterface; use React\Filesystem\Node; @@ -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'); } /** @@ -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 diff --git a/src/FilesystemInterface.php b/src/FilesystemInterface.php index 7685b341..a2c4d1be 100644 --- a/src/FilesystemInterface.php +++ b/src/FilesystemInterface.php @@ -2,6 +2,7 @@ namespace React\Filesystem; +use RuntimeException; use React\EventLoop\LoopInterface; use React\Filesystem\Node; @@ -11,6 +12,7 @@ interface FilesystemInterface * @param LoopInterface $loop * @param array $options * @return FilesystemInterface + * @throws RuntimeException */ public static function create(LoopInterface $loop, array $options = []); @@ -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 */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 5ab2ec28..7c928304 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -27,6 +27,7 @@ protected function mockAdapter(LoopInterface $loop = null) 'setFilesystem', 'setInvoker', 'callFilesystem', + 'isSupported', 'mkdir', 'rmdir', 'unlink',