adachsoft / devlink-workspace
PHP adapters for accessing devlink remote workspaces over HTTP, including filesystem and command execution.
Requires
- php: ^8.3
- adachsoft/command-executor-lib: ^2.4
- adachsoft/filesystem: ^1.4
- guzzlehttp/guzzle: ^7
- psr/http-client: ^1
- psr/http-factory: ^1
Requires (Dev)
- adachsoft/php-code-style: ^0.9.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Overview
adachsoft/devlink-workspace connects PHP filesystem and command-runner contracts to a remote workspace exposed by the devlink-agent HTTP gateway. The library provides one transport implementation for devlink; a different transport, such as SSH, should be provided by a separate library implementing the same underlying contracts.
The workspace.fs extension handles remote file operations, metadata, listing, and search. The workspace.exec extension runs allowlisted commands as argument vectors without invoking a shell.
Installation
composer require adachsoft/devlink-workspace
Usage
Create one gateway HTTP client and share it between the filesystem and command-runner adapters. The complete setup and basic calls are shown in the Quick start example below.
Quick start
<?php
declare(strict_types=1);
use AdachSoft\CommandExecutorLib\CommandRequest;
use AdachSoft\DevlinkWorkspace\Exec\WorkspaceExecClient;
use AdachSoft\DevlinkWorkspace\Fs\WorkspaceFsClient;
use AdachSoft\DevlinkWorkspace\GatewayConfig;
use AdachSoft\DevlinkWorkspace\Http\GatewayHttpClientFactory;
use AdachSoft\DevlinkWorkspace\RemoteCommandRunner;
use AdachSoft\DevlinkWorkspace\RemoteFileSystem;
use AdachSoft\FileSystem\Model\Path;
$token = getenv('DEVLINK_GATEWAY_TOKEN');
if ($token === false || $token === '') {
throw new RuntimeException('Set the DEVLINK_GATEWAY_TOKEN environment variable.');
}
$config = new GatewayConfig('https://gateway.example', $token);
$httpClient = GatewayHttpClientFactory::create($config);
$workspace = 'project';
$localRootPath = '/work/project';
$fileSystem = new RemoteFileSystem(
new WorkspaceFsClient($httpClient),
$workspace,
$localRootPath,
);
$commandRunner = new RemoteCommandRunner(
new WorkspaceExecClient($httpClient),
$workspace,
$localRootPath,
);
$contents = $fileSystem->readFile(new Path('src/App.php'));
$commandResult = $commandRunner->run(new CommandRequest(['/usr/bin/php', '--version']));
RemoteCommandRunner accepts an argument list and never runs a shell command string. Configure its command map when an executable path must be translated to a gateway allowlist name.
Conflict protection, line endings, and BOM
RemoteFileSystem uses SHA-256 optimistic concurrency for writes and deletes. A write after a read uses the hash returned by that read. A write to a path that has not been read first checks its current stat and uses the current hash, or null when the path is absent. The gateway rejects a write when the remote hash no longer matches; the adapter reports this as RemoteFileConflictException and the file should be read again before retrying.
With the default preserveLineEndings: true, a previously read CRLF file keeps CRLF when new content contains only LF, and a previously read UTF-8 BOM is retained. Mixed line endings are not rewritten. Pass false to disable this preservation behavior.
Path mapping (localRootPath)
Relative Path values are sent as workspace-relative paths. An absolute path under localRootPath is translated to its workspace-relative form; an absolute path outside that root, or a relative path that escapes the workspace, raises PathTraversalException. The workspace root is represented by Path('.') or Path('').
Paths returned by listContents(), listContentsRecursively(), and findFiles() are relative to the workspace root, including the directory path passed to the listing operation. They can be passed directly to other filesystem methods.
Error handling
The low-level gateway clients can throw WorkspaceOfflineException when the gateway cannot be reached. RemoteFileSystem adapts gateway failures to the adachsoft/filesystem contract: missing files and directories become FileNotFoundException or DirectoryNotFoundException, rejected paths become PathTraversalException, write conflicts become RemoteFileConflictException, and other failures become FileSystemIOException with the original exception preserved as the previous exception. Offline filesystem errors contain the text workspace offline.
RemoteCommandRunner adapts offline and other gateway failures to CommandExecutorException.
Security notes
Enable only the required devlink-agent extensions and scope tokens to the intended operations. The gateway must enforce its workspace allowlist and configured path boundary. Command execution uses the gateway's executable allowlist and an argument vector; the library does not invoke a shell. Keep gateway tokens outside source control and use a trusted private network or VPN for remote access.
Requirements
- PHP 8.3 or newer.
- A devlink-agent release providing the
workspace.fsandworkspace.execextensions. - A devlink specification supported by that agent release.