aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-rpc/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-04 02:25:28 +0900
committernsfisis <nsfisis@gmail.com>2026-08-04 05:43:18 +0900
commita02fc7d728a9973a3275a0f47604081c4439b424 (patch)
treeb1a73d2eb6b17bb6c204318b58d75ae6cf46cc7a /crates/shirabe-php-rpc/src
parent20f7a7826ae048d249e0d837ca6393a5f09c9ba6 (diff)
downloadphp-shirabe-a02fc7d728a9973a3275a0f47604081c4439b424.tar.gz
php-shirabe-a02fc7d728a9973a3275a0f47604081c4439b424.tar.zst
php-shirabe-a02fc7d728a9973a3275a0f47604081c4439b424.zip
feat(plugin): activate plugins through the PHP RPC worker
Implement the remainder of PluginManager::registerPackage: the plugin autoload map is built by the ported createLoader/parseAutoloads and served to the worker over the existing reverse-RPC autoloader, files entries go through a composerRequire-equivalent glue call, and already-defined classes take the upstream _composer_tmp rename/eval path. Instantiation uses the new NewObject/CallPhpMethod lanes backed by a P table in the worker; PhpPluginProxy adapts the resulting handle to PluginInterface, with $composer/$io exposed to plugin callbacks via an R table (unsupported methods stay explicit errors). Hand-written proxy stubs cover Composer, PartialComposer and the IO hierarchy, and the stub autoloader is re-prepended after loading the Composer PHP runtime so its vendor autoloader cannot shadow proxied FQCNs. FilesystemRepository::write now mirrors InstalledVersions::reload into a running worker (class_exists-guarded, so an unloaded class keeps its upstream lazy-load behavior), removing the previously undefined observation window. The installer pipeline passes the installed repository as a shared handle instead of a long-lived `&mut dyn`: plugin registration runs inside InstallationManager::execute and re-enters the same local repository through the RepositoryManager, which would panic on the RefCell re-borrow under the old shape. PluginInterface lifecycle methods now take an owned ComposerHandle (plugins retain $composer past the call) and return anyhow::Result (PHP plugin code may throw); the plugin list uses shared ownership so the identity comparison of removePlugin survives the dual storage in registeredPlugins, matching PHP reference semantics. Ports the activate/upgrade/uninstall tests of PluginInstallerTest, serialized across the shared worker process whose persistent class table is exactly what exercises the rename path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-php-rpc/src')
-rw-r--r--crates/shirabe-php-rpc/src/lib.rs86
1 files changed, 82 insertions, 4 deletions
diff --git a/crates/shirabe-php-rpc/src/lib.rs b/crates/shirabe-php-rpc/src/lib.rs
index 491af072..cfee5e4c 100644
--- a/crates/shirabe-php-rpc/src/lib.rs
+++ b/crates/shirabe-php-rpc/src/lib.rs
@@ -11,7 +11,7 @@ use indexmap::IndexMap;
use shirabe_external_packages::symfony::process::PhpExecutableFinder;
use shirabe_php_shim::PhpMixed;
use std::os::unix::net::{UnixListener, UnixStream};
-use std::sync::atomic::{AtomicU64, Ordering};
+use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{LazyLock, Mutex, OnceLock};
use std::time::{Duration, Instant};
@@ -396,6 +396,57 @@ pub fn call_static_method(
)
}
+/// Instantiates `new $class(...$ctor_args)` in the worker (autoloading the class if needed).
+/// On success the returned value is a `PluginValue::PhpHandle` registered in the worker's P
+/// table; the entity stays alive there until `release_php_handle`.
+pub fn new_object(
+ class: &str,
+ ctor_args: Vec<PluginValue>,
+ dispatcher: Option<&mut dyn RustMethodDispatcher>,
+) -> anyhow::Result<Result<PluginValue, PhpThrow>> {
+ rpc_call(
+ |corr_id| Frame::NewObject {
+ corr_id,
+ pclass: class.to_string(),
+ ctor_args,
+ },
+ dispatcher,
+ )
+}
+
+/// Calls `$obj->$method(...$args)` on a P-table entity in the worker.
+pub fn call_php_method(
+ phandle: u64,
+ method: &str,
+ args: Vec<PluginValue>,
+ dispatcher: Option<&mut dyn RustMethodDispatcher>,
+) -> anyhow::Result<Result<PluginValue, PhpThrow>> {
+ rpc_call(
+ |corr_id| Frame::CallPhpMethod {
+ corr_id,
+ phandle,
+ method_name: method.to_string(),
+ args,
+ out_param_positions: Vec::new(),
+ },
+ dispatcher,
+ )
+}
+
+/// One-way notification dropping a P-table entity in the worker. Callers releasing from a
+/// destructor should ignore the error: a dead worker has nothing left to release.
+pub fn release_php_handle(phandle: u64) -> anyhow::Result<()> {
+ let _session = session::SessionGuard::enter();
+ send_frame(&Frame::ReleasePhpHandle { phandle })
+}
+
+/// Whether the PHP worker process has been spawned by this process. Callers that only need to
+/// mirror state into an already-running worker (e.g. `InstalledVersions::reload` pushes) use
+/// this to avoid spawning a worker that would have nothing to observe.
+pub fn worker_is_running() -> bool {
+ WORKER_SPAWNED.load(Ordering::SeqCst)
+}
+
fn rpc_call(
request: impl FnOnce(u64) -> Frame,
mut dispatcher: Option<&mut dyn RustMethodDispatcher>,
@@ -499,6 +550,30 @@ const STUB_FILES: &[(&str, &str)] = &[
"Composer/Script/Event.php",
include_str!("../php/stubs/Composer/Script/Event.php"),
),
+ (
+ "Composer/PartialComposer.php",
+ include_str!("../php/stubs/Composer/PartialComposer.php"),
+ ),
+ (
+ "Composer/Composer.php",
+ include_str!("../php/stubs/Composer/Composer.php"),
+ ),
+ (
+ "Composer/IO/BaseIO.php",
+ include_str!("../php/stubs/Composer/IO/BaseIO.php"),
+ ),
+ (
+ "Composer/IO/ConsoleIO.php",
+ include_str!("../php/stubs/Composer/IO/ConsoleIO.php"),
+ ),
+ (
+ "Composer/IO/BufferIO.php",
+ include_str!("../php/stubs/Composer/IO/BufferIO.php"),
+ ),
+ (
+ "Composer/IO/NullIO.php",
+ include_str!("../php/stubs/Composer/IO/NullIO.php"),
+ ),
];
struct Worker {
@@ -530,11 +605,14 @@ impl Worker {
// TODO(phase-c): a failed spawn panics rather than propagating a `Result`; this is an interim
// step until PHP RPC gets proper error handling (see docs/dev/php-rpc.md).
static WORKER: LazyLock<Mutex<Worker>> = LazyLock::new(|| {
- Mutex::new(
- spawn_worker().unwrap_or_else(|e| panic!("PHP RPC: failed to spawn PHP worker: {e:#}")),
- )
+ let worker =
+ spawn_worker().unwrap_or_else(|e| panic!("PHP RPC: failed to spawn PHP worker: {e:#}"));
+ WORKER_SPAWNED.store(true, Ordering::SeqCst);
+ Mutex::new(worker)
});
+static WORKER_SPAWNED: AtomicBool = AtomicBool::new(false);
+
/// Writes one frame while holding the worker mutex only for the duration of the write, so the
/// session owner (see `session`) can interleave sends and blocking reads without keeping the
/// worker locked across a whole call.