wiki.techinc.nl/includes/shell/ShellboxClientFactory.php
Tim Starling 99392f3b16 Use Shellbox for Shell::command() etc.
Make Command extend Shellbox's UnboxedCommand. Only a few MediaWiki-
specific features remain in the subclass.

Also add BoxedCommand abstraction and Shellbox client.

The Result alias didn't actually work, I just had to change the return
type hint.

Bug: T260330
Change-Id: Iff7428e4c5fe3959a5cda8e113f223caa0976fc1
2021-02-05 21:51:58 +11:00

70 lines
1.9 KiB
PHP

<?php
namespace MediaWiki\Shell;
use GuzzleHttp\Psr7\Uri;
use MediaWiki\Http\HttpRequestFactory;
use Shellbox\Client;
/**
* This is a service which provides a configured client to access a remote
* Shellbox installation.
*
* @since 1.36
*/
class ShellboxClientFactory {
/** @var HttpRequestFactory */
private $requestFactory;
/** @var string|null */
private $url;
/** @var string|null */
private $key;
/** The default request timeout, in seconds */
public const DEFAULT_TIMEOUT = 10;
/**
* @internal Use MediaWikiServices::getShellboxClientFactory()
* @param HttpRequestFactory $requestFactory The factory which will be used
* to make HTTP clients.
* @param string|null $url The Shellbox base URL
* @param string|null $key The shared secret key used for HMAC authentication
*/
public function __construct( HttpRequestFactory $requestFactory, $url, $key ) {
$this->requestFactory = $requestFactory;
$this->url = $url;
$this->key = $key;
}
/**
* Test whether remote Shellbox is enabled by configuration.
*
* @return bool
*/
public function isEnabled() {
return $this->url !== null && strlen( $this->key );
}
/**
* Get a Shellbox client with the specified options. If remote Shellbox is
* not configured (isEnabled() returns false), an exception will be thrown.
*
* @param array $options Associative array of options:
* - timeout: The request timeout in seconds
* @return Client
* @throws \RuntimeException
*/
public function getClient( array $options = [] ) {
if ( !$this->isEnabled() ) {
throw new \RuntimeException( 'To use a remote shellbox to run shell commands, ' .
'$wgShellboxUrl and $wgShellboxSecretKey must be configured.' );
}
return new Client(
new ShellboxHttpClient( $this->requestFactory,
$options['timeout'] ?? self::DEFAULT_TIMEOUT ),
new Uri( $this->url ),
$this->key
);
}
}