[2.x] fix: resolve VersionerInterface from container; cache rev-manifest reads#4638
Merged
Conversation
RevisionCompiler, JsDirectoryCompiler, and Document declared a VersionerInterface property but instantiated FileVersioner directly in their constructors. The interface contract was unenforceable — code binding VersionerInterface to a custom implementation had no effect. Bind VersionerInterface in FrontendServiceProvider (defaulting to FileVersioner against the flarum-assets disk) and resolve it via the container in all three call sites. Local-disk installs are unchanged (same default binding); installs on remote storage can now bind a faster implementation (DB, Redis, etc.) and have it take effect everywhere. Also cache the parsed manifest in-memory on FileVersioner. With the binding now a singleton, all ~6 compilers plus Document per request share one VersionerInterface instance — previously they each read and JSON-decoded rev-manifest.json independently. On S3-backed installs this was ~12 round-trips per request just for asset URL resolution. Document's constructor signature changes: the sixth parameter FilesystemFactory $filesystem is now VersionerInterface $versioner. Subclasses calling parent::__construct() positionally need updating; named args are forward-compatible. Fixes #4637
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RevisionCompiler,JsDirectoryCompiler, andDocumentdeclared aVersionerInterfaceproperty but instantiatedFileVersionerdirectly in their constructors. The interface contract was unenforceable —$container->bind(VersionerInterface::class, MyVersioner::class)had no effect on these classes.This PR:
VersionerInterfaceinFrontendServiceProvider(defaulting toFileVersioneragainst theflarum-assetsdisk) and resolves it via the container in all three call sites. Local-disk installs are unchanged (same default binding); installs on remote storage can now bind a faster implementation and have it take effect everywhere.FileVersioner. With the binding now a singleton, all ~6 compilers +Documentper request share oneVersionerInterfaceinstance — previously each read and JSON-decodedrev-manifest.jsonindependently. On S3-backed installs this was ~12 round-trips per request just for asset URL resolution; now it's one.Fixes #4637
Changes
framework/core/src/Frontend/FrontendServiceProvider.php— bindVersionerInterfaceto aFileVersionersingleton scoped to theflarum-assetsfilesystem.framework/core/src/Frontend/Compiler/RevisionCompiler.phpandJsDirectoryCompiler.php— drop the inlinenew FileVersioner(...), acceptVersionerInterfacevia constructor (autowired). Both classes are@internaland resolved through the container byFrontend\Assets, so no caller needs updating.framework/core/src/Frontend/Document.php— replace theFilesystemFactory $filesystemconstructor parameter withVersionerInterface $versioner. Resolved via$container->makeWith()fromFrontend.php— autowiring picks up the new arg.framework/core/src/Frontend/Compiler/FileVersioner.php— per-instance manifest cache with write-through.putRevision()updates both disk and the cache;getRevision()andallRevisions()read from the cache. The CLI compile path constructs fresh containers, so per-instance lifetime is correct (no stale-manifest worries across processes).framework/core/tests/unit/Frontend/Compiler/FileVersionerTest.php— 5 tests pinning down read-once-per-instance, missing keys, missing manifest, write-through cache update, and key removal vianullrevision.Backward compatibility
Document::__constructsignature change is the only break. The sixth parameterFilesystemFactory $filesystemis nowVersionerInterface $versioner. Subclasses that callparent::__construct(...)with positional arguments will need to update; named arguments are forward-compatible. Most extensions don't subclassDocumentand aren't affected. Worth a note in the 2.0 dev upgrade guide.RevisionCompilerandJsDirectoryCompilerare@internaland only resolved through the container; the new constructor parameter is autowired from the binding.Test plan
FileVersionerTest).php flarum assets:publishandphp flarum cache:clearstill write a validrev-manifest.jsonand bundles serve correctly.Related
exists()calls in a per-request hot path) on the avatarsrcsetForaccessor; outside scope of this PR.