feat: add Facade, improve performance and ergonomics#69
feat: add Facade, improve performance and ergonomics#69
Conversation
This change introduces Laravel-specific patterns for interacting with WorkOS: * A WorkOS facade for static access (e.g. WorkOS::userManagement() ->listUsers()) * A global workos() helper function for fluent access everywhere (e.g. workos()->userManagement()->listUsers()) * WorkOSService singleton with cached service instances * Dependency injection support via service container binding Additionally, while working on this I noticed a potential performance issue. Previously the service container was setting the API Key and Client ID on every request. The service container now configures the SDK only when requested, and its configuration is cached on subsequent requests.
|
Oof, apparently I need to figure out some dependency issues, Let me do that real quick. |
0ad0eae to
b0896e4
Compare
|
Oooook, some dependency weirdness and incompatibility existed in the composer. But after comparing everything on packagist to our stated requirements (and verifying with Claude) things should be good now 🎉 |
| $this->assertInstanceOf(\WorkOS\UserManagement::class, WorkOS::userManagement()); | ||
| } | ||
|
|
||
| public function test_facade_provides_access_to_all_services() |
There was a problem hiding this comment.
this says "all services" but there are a few missing: Vault, Webhook, Widgets. Is there a way to dynamically check these?
There was a problem hiding this comment.
There is (reflection) and I was actually already using it in a different test, so this is done now. 👍🏽 Registering them is still manual essentially because of how we expose the types (unless I'm missing some PHP trickery), but testing for drift etc can be automated.
Fix @return/@see references to use the full WorkOS\Laravel\Services\WorkOSService namespace, and spread arguments with ... in __call() so constructor args are unpacked correctly.
Replace hardcoded service assertions with reflection-based iteration over WorkOSService::$serviceMap so new services are automatically covered.
CookieSession requires constructor arguments (UserManagement, sealedSession, cookiePassword) so it cannot be zero-arg instantiated like other services. RBAC is a normal service added in workos-php 4.32.0.
This change introduces Laravel-specific patterns for interacting with WorkOS:
WorkOS::userManagement()->listUsers())workos()helper function for fluent access everywhere (e.g.workos()->userManagement()->listUsers())WorkOSServicesingleton with cached service instancesAdditionally, while working on this I noticed a potential performance issue. Previously the service container was setting the API Key and Client ID on every request. The service container now configures the SDK only when requested, and its configuration is cached on subsequent requests.
this does not break or change any existing client code.
Intriguing, tell me more
Throughout Laravel, there are multiple common patterns used to get access to the various services/components of your application. For example, if you're issuing a response, you could either import and use the
ResponseFacade directly:Alternatively, you could use a global helper as a one-off:
Or, finally, you could inject the dependency into a function as a typed argument, and it will be automatically resolved by the Service Container:
This PR updates the code so that we enable similar ergonomics using their usual patterns. For example:
These are automatically registered immediately upon installing the
workos-php-laravelpackage, and make it much, much easier to productively integrate the client into your project.