diff --git a/AGENTS.md b/AGENTS.md index 79d960c..47d76a4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -41,6 +41,11 @@ ci/ .github/ builds a full CMS with the plugin inside (Docker), first, and only then asks whether the path exists. - **A stranger's personal mount is "not found", never "forbidden"** (and not even an administrator opens it); `Mounts::find()` enforces it, controllers must always go through it. +- **Storage drivers are optional packages**: `require` holds only `league/flysystem` and + `league/flysystem-local`; every other driver's package is in `suggest`, with its minimum version + in `conflict` (SFTP `<3.36` would break host-key pinning). Builds (tests, demo, zip, images) + add `PLUGIN_EXTRAS` from `ci/plugin.env`, so tests may use the S3/SFTP/WebDAV classes; code may + not assume them outside their driver (`Driver::available()`). `PackagesTest` keeps this in sync. - **Where a mount connects** (DESIGN rule 10): `Driver::filesystem()` is fail-closed - without `_trusted` (set by `Mount::filesystem()` from `Mount::trusted()`) local/zip refuse and every `destinations()` entry must resolve public. A driver gaining a host/URL field must list it in diff --git a/README.md b/README.md index 0565a8b..dca9328 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,22 @@ php artisan package:installrequire elcreator/amount "*" php artisan migrate ``` -The module appears in the manager menu as **aMount**. Run `php artisan amount:drivers` to see which drivers are -installed; install a missing one with the command it prints. `php artisan amount:demo --run` seeds a demo on a local +The module appears in the manager menu as **aMount**. Only the local folder driver comes with it; every storage +driver is an optional package, so a site carries just the ones it uses (the AWS SDK alone is about 77 MB). Run +`php artisan amount:drivers` to see which drivers are installed; install a missing one with the command it prints, +e.g. for S3, SFTP and WebDAV (the trailing `0` records a package without running Composer, so it runs once): + +```sh +php artisan package:installrequire league/flysystem-aws-s3-v3 "*" 0 +php artisan package:installrequire league/flysystem-sftp-v3 "*" 0 +php artisan package:installrequire league/flysystem-webdav "*" +``` + +> **Upgrading from a version that required S3, SFTP and WebDAV:** `composer update` removes those packages if nothing +> else needs them, and mounts using them then report the install command instead of connecting. Install the drivers +> you use as above before or after updating. + +The ready-made zip and Docker images include the S3, SFTP and WebDAV drivers. `php artisan amount:demo --run` seeds a demo on a local disk (no account needed). ## Permissions diff --git a/ci/compose-evo.sh b/ci/compose-evo.sh index 9d379b6..d6a4a30 100755 --- a/ci/compose-evo.sh +++ b/ci/compose-evo.sh @@ -179,7 +179,8 @@ step "core/custom/composer.json" # CMS plus this one plugin; a package that happened to be installed in the # core this was built from has no business travelling into a published image. custom="$target/core/custom/composer.json" -PLUGIN_PACKAGE="$PLUGIN_PACKAGE" PLUGIN_SLUG="$PLUGIN_SLUG" php "$here/lib/write-custom-composer.php" "$custom" +PLUGIN_PACKAGE="$PLUGIN_PACKAGE" PLUGIN_SLUG="$PLUGIN_SLUG" PLUGIN_EXTRAS="${PLUGIN_EXTRAS:-}" \ + php "$here/lib/write-custom-composer.php" "$custom" cat "$custom" step "composer update $PLUGIN_PACKAGE (partial)" @@ -200,7 +201,10 @@ step "composer update $PLUGIN_PACKAGE (partial)" # being maintained and wrong for a build that has to be reproducible. lock_before=$(mktemp) cp "$target/core/composer.lock" "$lock_before" -( cd "$target/core" && composer update "${composer_args[@]}" --with-dependencies "$PLUGIN_PACKAGE" ) +# The optional packages from plugin.env (PLUGIN_EXTRAS) are named too, so they +# may be installed; word splitting is intended. +# shellcheck disable=SC2086 +( cd "$target/core" && composer update "${composer_args[@]}" --with-dependencies "$PLUGIN_PACKAGE" ${PLUGIN_EXTRAS:-} ) step "What the plugin changed in core/composer.lock" php "$here/lib/lock-delta.php" "$lock_before" "$target/core/composer.lock" diff --git a/ci/lib/write-custom-composer.php b/ci/lib/write-custom-composer.php index 8a07c05..152dc4a 100644 --- a/ci/lib/write-custom-composer.php +++ b/ci/lib/write-custom-composer.php @@ -12,12 +12,16 @@ * else they had installed there is exactly what must not travel into a * published zip or image. * - * Usage: PLUGIN_PACKAGE=... PLUGIN_SLUG=... php write-custom-composer.php + * PLUGIN_EXTRAS (optional, space-separated package names) are required at + * any version: the plugin's own "conflict" section sets their minimums. + * + * Usage: PLUGIN_PACKAGE=... PLUGIN_SLUG=... [PLUGIN_EXTRAS=...] php write-custom-composer.php */ $custom = $argv[1] ?? ''; $package = getenv('PLUGIN_PACKAGE') ?: ''; $slug = getenv('PLUGIN_SLUG') ?: ''; +$extras = preg_split('/\s+/', trim((string) getenv('PLUGIN_EXTRAS')), -1, PREG_SPLIT_NO_EMPTY) ?: []; if ($custom === '' || $package === '' || $slug === '') { fwrite(STDERR, "usage: PLUGIN_PACKAGE=... PLUGIN_SLUG=... php write-custom-composer.php \n"); @@ -44,7 +48,7 @@ 'options' => ['symlink' => false], ], ], - 'require' => [$package => '@dev'], + 'require' => [$package => '@dev'] + array_fill_keys($extras, '*'), 'autoload' => ['psr-4' => ['EvolutionCMS\\Custom\\' => 'src/']], ]; diff --git a/ci/plugin.env b/ci/plugin.env index 0de9b1b..5a0a76d 100644 --- a/ci/plugin.env +++ b/ci/plugin.env @@ -14,6 +14,13 @@ PLUGIN_PROVIDER='Elcreator\aMount\aMountServiceProvider' # Directory name under core/custom/packages/, and the local docker image name. PLUGIN_SLUG='amount' +# Optional (composer "suggest") packages the builds install next to the plugin, +# space-separated, at any version the plugin's "conflict" allows. The unit +# suite, the demo and the published zip/images then carry these drivers, while +# a site installing the plugin from packagist adds only the ones it uses. +# PLUGIN_EXTRAS= (set but empty) builds the bare plugin. +PLUGIN_EXTRAS="${PLUGIN_EXTRAS-league/flysystem-aws-s3-v3 league/flysystem-sftp-v3 league/flysystem-webdav}" + # The CMS this plugin is built into. EVO_REPO="${EVO_REPO:-https://github.com/evolution-cms/evolution}" EVO_REF="${EVO_REF:-3.5.x}" diff --git a/ci/smoke.sh b/ci/smoke.sh index 5e037fa..74ef4e4 100755 --- a/ci/smoke.sh +++ b/ci/smoke.sh @@ -26,6 +26,14 @@ fail() { echo "smoke.sh: $*" >&2; exit 1; } step "Package is installed" [ -d "$evo_dir/core/vendor/$PLUGIN_PACKAGE" ] || fail "$PLUGIN_PACKAGE missing from core/vendor" +if [ -n "${PLUGIN_EXTRAS:-}" ]; then + step "Optional packages are installed" + for extra in $PLUGIN_EXTRAS; do + [ -d "$evo_dir/core/vendor/$extra" ] || fail "$extra (PLUGIN_EXTRAS) missing from core/vendor" + echo "ok $extra" + done +fi + step "Service provider is discovered" # `artisan package:discover` runs on every composer dump-autoload and drops one # file per discovered provider into core/custom/config/app/providers/. That diff --git a/composer.json b/composer.json index 47f7186..9614061 100644 --- a/composer.json +++ b/composer.json @@ -8,14 +8,15 @@ "php": "^8.3", "evolution-cms/evolution": "^3.5.2", "league/flysystem": "^3.10", - "league/flysystem-aws-s3-v3": "^3.10", - "league/flysystem-sftp-v3": "^3.36", - "league/flysystem-webdav": "^3.6" + "league/flysystem-local": "^3.10" }, "require-dev": { "pestphp/pest": "^4.0" }, "suggest": { + "league/flysystem-aws-s3-v3": "Amazon S3 and S3-compatible driver (MinIO, Cloudflare R2, Backblaze B2, Wasabi, DigitalOcean Spaces ...)", + "league/flysystem-sftp-v3": "SFTP / SSH driver", + "league/flysystem-webdav": "WebDAV driver (Nextcloud, ownCloud, Synology ...)", "league/flysystem-ftp": "FTP/FTPS driver (needs ext-ftp, which many PHP builds omit, so it is not a hard requirement)", "league/flysystem-async-aws-s3": "AsyncAws S3 driver (lighter alternative to the AWS SDK)", "league/flysystem-google-cloud-storage": "Google Cloud Storage driver", @@ -31,6 +32,11 @@ "overtrue/flysystem-qiniu": "Qiniu Kodo driver", "seiger/stask": "Alternative queue manager: aMount runs its uploads as an sTask worker when it is installed" }, + "conflict": { + "league/flysystem-aws-s3-v3": "<3.10", + "league/flysystem-sftp-v3": "<3.36", + "league/flysystem-webdav": "<3.6" + }, "autoload": { "psr-4": { "Elcreator\\aMount\\": "src/" diff --git a/src/Console/DemoCommand.php b/src/Console/DemoCommand.php index f362992..ac74f0e 100644 --- a/src/Console/DemoCommand.php +++ b/src/Console/DemoCommand.php @@ -2,6 +2,7 @@ namespace Elcreator\aMount\Console; +use Elcreator\aMount\Drivers\Drivers; use Elcreator\aMount\Models\FileEntry; use Elcreator\aMount\Models\Mount; use Elcreator\aMount\Models\Rule; @@ -84,6 +85,15 @@ private function s3(Mounts $mounts, Rules $rules): array return []; } + // The S3 package is optional (composer "suggest"). + $driver = Drivers::get('s3'); + + if (!$driver->available()) { + $this->warn('Skipping --s3: the S3 driver is not installed. Install it with: ' . $driver->installCommand()); + + return []; + } + $mount = $this->mount($mounts, 'Demo S3 (MinIO)', 's3', [ 'bucket' => $env['BUCKET'], 'region' => $env['REGION'] ?: 'us-east-1', 'endpoint' => $env['ENDPOINT'], 'path_style' => true, 'key' => $env['KEY'], 'secret' => $env['SECRET'], diff --git a/tests/Unit/PackagesTest.php b/tests/Unit/PackagesTest.php new file mode 100644 index 0000000..63d7b50 --- /dev/null +++ b/tests/Unit/PackagesTest.php @@ -0,0 +1,69 @@ + amountReset()); + +function amountComposer(): array +{ + return json_decode((string) file_get_contents(dirname(__DIR__, 2) . '/composer.json'), true, 512, JSON_THROW_ON_ERROR); +} + +/** @return string[] the PLUGIN_EXTRAS of ci/plugin.env */ +function amountPluginExtras(): array +{ + $env = (string) file_get_contents(dirname(__DIR__, 2) . '/ci/plugin.env'); + + return preg_match('/^PLUGIN_EXTRAS="\$\{PLUGIN_EXTRAS-([^}]*)\}"/m', $env, $m) ? preg_split('/\s+/', trim($m[1]), -1, PREG_SPLIT_NO_EMPTY) : []; +} + +it('requires only what every installation uses', function () { + expect(array_keys(amountComposer()['require'])) + ->toBe(['php', 'evolution-cms/evolution', 'league/flysystem', 'league/flysystem-local']); +}); + +it('offers every storage driver package as a suggestion, apart from the local one', function () { + $composer = amountComposer(); + // What the required CMS brings itself (guzzle for the computer driver). + $core = json_decode((string) file_get_contents((getenv('EVO_CORE_PATH_TEST') ?: 'C:/projects/Opensource/evo/evolution/core') . '/composer.json'), true); + + foreach (Drivers::all() as $driver) { + foreach ($driver->packages() as $package) { + $declared = array_key_exists($package, $composer['require']) || array_key_exists($package, $composer['suggest']) + || array_key_exists($package, (array) ($core['require'] ?? [])); + + expect($declared)->toBeTrue($driver->id() . ' needs ' . $package . ', which neither composer.json nor the CMS core declares'); + } + } + + expect($composer['suggest'])->toHaveKeys(['league/flysystem-aws-s3-v3', 'league/flysystem-sftp-v3', 'league/flysystem-webdav']); +}); + +it('keeps the minimum versions of the optional drivers it depends on', function () { + // SFTP below 3.36 takes neither several pinned keys nor the OpenSSH SHA256 form. + expect(amountComposer()['conflict'])->toBe([ + 'league/flysystem-aws-s3-v3' => '<3.10', + 'league/flysystem-sftp-v3' => '<3.36', + 'league/flysystem-webdav' => '<3.6', + ]); +}); + +it('builds the published CMS with the S3, SFTP and WebDAV drivers', function () { + $suggest = amountComposer()['suggest']; + $extras = amountPluginExtras(); + + expect($extras)->toBe(['league/flysystem-aws-s3-v3', 'league/flysystem-sftp-v3', 'league/flysystem-webdav']); + + foreach ($extras as $package) { + expect($suggest)->toHaveKey($package); + } +}); + +it('tells how to install a missing driver with one installrequire call per package', function () { + foreach (Drivers::all() as $driver) { + // package:installrequire {key} {value} takes one package per call. + expect(substr_count($driver->installCommand(), 'package:installrequire'))->toBe(1) + ->and(count($driver->packages()))->toBe(1, $driver->id() . ' lists several packages; installCommand() would need one call each'); + } +});