Files
feuerwerk/api/lib/PluginStore.php
T
adminandClaude Opus 5 5ba796a2b6
game-maker / gate (push) Successful in 3m32s
game-maker / suite (push) Failing after 8m21s
Plugins belong to the project, and they load before it: the project file gained the field plugins: ["demo-plugin.js"] (default empty, no formatVersion bump), the Plugins tab's switch writes into it as an ordinary undo step (SetPluginActiveCommand) instead of into a server-wide plugins.json, and the start order is turned around - startPlugins only fetches the list of files, then editor.boot() runs and ProjectStore.load(id, prepare) hands the raw JSON through before it is deserialized, where pluginsOf(raw) reads the list and PluginManager.syncTo() activates exactly those; that is the point of the issue, because what a plugin registers has to stand there before normalizeGraph would throw it away as unknown (#126, #127), a project switch first stops what the new project does not name and then starts what it does, a file the server does not have is passed over silently, and after signing out the manager is dead so a late event cannot start anything in a torn-down session; on the server plugins.json, PluginStore::setActive, isActive, the route PUT ?r=plugins/{file} and the 403 for inactive modules are gone and GET ?r=plugins only answers {file,bytes,updatedAt}, which makes every plugin file fetchable - the consequence accepted in #117, written down in the wiki for #115 and #116; publishPlugins(root) only copies files now, the seeder switches the demo plugin on in the fixture project, and the Plugins tab carries a puzzle piece as its icon while only a plugin's own tab still goes without one (the knob and the socket are cubic curves, not arcs, which Pixi's parser draws as a spike after an L); 3 new e2e checks in module 38 and 5 new unit tests cover the project as the owner of the switch, its undo, the order, pluginsOf against crude input and an unknown file name (#119)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 14:24:27 +02:00

60 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/**
* The editor plugins in `data/plugins/`: one ES module per `.js` file. Whether one runs is written
* in the project that is open, not here (#119) – the server hands out every plugin it has and knows
* nothing about switches. The folder is still not served statically: a module is only ever handed
* out by its bare file name, so no request can reach past it.
*/
final class PluginStore
{
public function __construct(private readonly string $dir)
{
}
/** A file name as it may be asked for: no path, no leading dot, `.js` at the end. */
public static function isValidName(string $name): bool
{
return (bool) preg_match('/^[A-Za-z0-9][A-Za-z0-9._-]*\.js$/', $name);
}
/**
* Sorted by file name, which is also the order the editor activates them in.
*
* @return list<array{file: string, bytes: int, updatedAt: int}>
*/
public function list(): array
{
$files = is_dir($this->dir) ? scandir($this->dir) : [];
$out = [];
foreach ($files === false ? [] : $files as $file) {
if ($this->exists($file)) {
$path = "{$this->dir}/{$file}";
$out[] = [
'file' => $file,
'bytes' => (int) filesize($path),
'updatedAt' => (int) filemtime($path),
];
}
}
usort($out, static fn (array $a, array $b): int => strcmp($a['file'], $b['file']));
return $out;
}
/** The module's source, or null when there is no such plugin. */
public function read(string $file): ?string
{
if (!$this->exists($file)) {
return null;
}
$source = file_get_contents("{$this->dir}/{$file}");
return $source === false ? null : $source;
}
private function exists(string $file): bool
{
return self::isValidName($file) && is_file("{$this->dir}/{$file}");
}
}