Refactor service initialization and request lifecycle - #956
Merged
giuscris merged 5 commits intoSep 26, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
It introduces model accessor regressions, duplicate controller construction, and broken panel compatibility and redirect behavior.
Review effort: Balanced
Findings: 1
Open (6)
Preserve assigned scheme and invalidate cached fields · New Preserve Getter metadata on the scheme override · New Avoid parsing the route callback twice · New Preserve fallback handling for deprecated login limits · New Return after applying the remote-registration redirect · New Retain Getter metadata on the scheme override · New
What changed in this PR
Refactors service initialization for lazier dependency resolution and centralizes application URI generation.
Changes:
- Adds lazy service loaders and route-action lifecycle events.
- Introduces
Cms\UriGeneratorand migrates URI generation. - Lazily initializes model schemes and fields.
| File | Description |
|---|---|
panel/config/routes/routes.php |
Uses named routes and removes authentication filters. |
formwork/src/Users/User.php |
Lazily resolves user schemes. |
formwork/src/Services/Loaders/PanelServiceLoader.php |
Moves panel access checks into route events. |
formwork/src/Services/Loaders/LanguagesServiceLoader.php |
Adds independent language initialization. |
formwork/src/Services/Loaders/AssetsServiceLoader.php |
Decouples assets from Site. |
formwork/src/Schemes/Scheme.php |
Clarifies field collection behavior. |
formwork/src/Router/Router.php |
Dispatches resolved-action events. |
formwork/src/Router/Events/RouteActionResolvedEvent.php |
Adds mutable route-action event. |
formwork/src/Plugins/Plugin.php |
Lazily retrieves view and URI services. |
formwork/src/Panel/Controllers/UsersController.php |
Injects upload service per action. |
formwork/src/Panel/Controllers/PagesController.php |
Uses centralized named-route generation. |
formwork/src/Panel/Controllers/FilesController.php |
Lazily injects uploader and generates routes. |
formwork/src/Panel/Controllers/ErrorsController.php |
Uses named dashboard routes. |
formwork/src/Panel/Controllers/BackupController.php |
Uses named backup routes. |
formwork/src/Panel/Controllers/AbstractController.php |
Delegates view creation to the base controller. |
formwork/src/Pages/Page.php |
Defers page field initialization. |
formwork/src/Model/Model.php |
Lazily creates and caches model fields. |
formwork/src/Files/FileUriGenerator.php |
Uses the centralized URI generator. |
formwork/src/Files/FileFactory.php |
Removes eager scheme initialization. |
formwork/src/Files/File.php |
Resolves file schemes lazily. |
formwork/src/Controllers/AbstractController.php |
Lazily resolves views, forms, and upload dependencies. |
formwork/src/Cms/UriGenerator.php |
Adds application path and route generation. |
formwork/src/Cms/Site.php |
Removes unrelated eager dependencies. |
formwork/src/Cms/App.php |
Registers lifecycle-aware services. |
formwork/config/views/methods.php |
Delegates view URI generation. |
formwork/config/routes/routes.php |
Resolves languages independently of Site. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
giuscris
marked this pull request as ready for review
September 26, 2026 07:50
giuscris
deleted the
refactor/service-initializaition-and-request-lifecycle
branch
September 26, 2026 07:50
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
This pull request refactors Formwork's service initialization lifecycle to reduce unnecessary transitive dependencies and make lazy service resolution follow the actual application lifecycle.
It also introduces a new
Cms\UriGeneratorservice to centralize application URI and route generation without requiring higher-level services such asSite.Changes
Service lifecycle
Container,AppandConfigeagerly initialized.Dependency decoupling
Siteto its actual page-related dependencies instead of using it as an entry point for unrelated CMS services.Site, allowing the language filter to initializeLanguagesandTranslationsindependently.FileFactoryfromSchemes.Schemes,Templates,Users,Files,Cacheand other services independently resolvable.URI generation
Formwork\Cms\UriGeneratorfor generating application paths and named routes.App::uri().UriGeneratorfor controller redirects, asset URI generation and file URI generation.UriandPathutilities for actual URI/path manipulation rather than using them directly to implement application-level URI generation.This allows services that need an application URI to depend directly on
UriGeneratorinstead of depending onSiteor duplicating URI construction logic.Result
The refactoring makes the service dependency graph substantially flatter and better aligned with the request lifecycle.
Previously, resolving
Sitecould implicitly initialize a large portion of the CMS, including languages, translations, schemes, templates, users and files.The resulting lifecycle is now closer to:
As a result,
Siteis no longer a central dependency for unrelated services and the cost of its initialization is greatly reduced.In profiling, the initial request was around 37.5 ms, with
Siteaccounting for roughly 17 ms of initialization. After the refactoring,Siteis around 0.3 ms, while total request times are generally in the ~25–32 ms range depending on filesystem/cache state.The remaining cost is now concentrated in the actual page loading and rendering work rather than service bootstrap and transitive dependency resolution.
Dependency Injection and Service Loading:
UriGeneratorandLanguagesservices (formwork/src/Cms/App.php). [1] [2] [3]FileUploader,ViewFactory, andLanguagesFactory(formwork/src/Controllers/AbstractController.php,formwork/src/Cms/Site.php,formwork/src/Files/FileFactory.php). [1] [2] [3]Code Cleanup:
formwork/config/views/methods.php,formwork/src/Files/FileUriGenerator.php,formwork/src/Controllers/AbstractController.php). [1] [2] [3]Languages Service Refactoring:
Languagesservice is injected and accessed, moving from direct instantiation inSiteto service container retrieval, and updating route and service definitions accordingly (formwork/config/routes/routes.php,formwork/src/Cms/Site.php,formwork/src/Cms/App.php). [1] [2] [3]Site and File Model Simplification:
SiteandFileobjects retrieve their associated services and schemes, using lazy loading via the application container and removing explicit scheme assignment (formwork/src/Cms/Site.php,formwork/src/Files/File.php,formwork/src/Files/FileFactory.php). [1] [2] [3]Routing and URI Generation Improvements:
UriGeneratorclass to encapsulate URI and route generation logic, and registered it as a service in the application container (formwork/src/Cms/UriGenerator.php,formwork/src/Cms/App.php). [1] [2] [3]UriandPathcalls to use the newUriGeneratorservice in views, controllers, and route definitions (formwork/config/views/methods.php,formwork/src/Controllers/AbstractController.php,formwork/config/routes/routes.php). [1] [2] [3]