@@ -406,11 +406,17 @@ public function setResource(string $name, callable $callback, array $injections
406406 }
407407
408408 /**
409- * Set a request-scoped resource on the current request's container.
409+ * Set a request-scoped value on the current request's context container.
410+ *
411+ * The framework convention is: `setResource()` registers singletons on
412+ * the global container; `setContext()` registers per-request values on
413+ * the adapter's request container, which is coroutine-local under the
414+ * Swoole adapters. `getResource()` reads from the request container
415+ * with parent-fallback to the global one, so either kind resolves.
410416 *
411417 * @param list<string> $injections
412418 */
413- protected function setRequestResource (string $ name , callable $ callback , array $ injections = []): void
419+ protected function setContext (string $ name , callable $ callback , array $ injections = []): void
414420 {
415421 $ this ->server ->getContainer ()->set ($ name , $ callback , $ injections );
416422 }
@@ -674,7 +680,7 @@ public function execute(Route $route, Request $request, Response $response): sta
674680 }
675681 }
676682 } catch (\Throwable $ e ) {
677- $ this ->setRequestResource ('error ' , fn () => $ e , []);
683+ $ this ->setContext ('error ' , fn () => $ e , []);
678684
679685 foreach ($ groups as $ group ) {
680686 foreach (self ::$ errors as $ error ) { // Group error hooks
@@ -793,16 +799,16 @@ private function runInternal(Request $request, Response $response): static
793799 $ response ->setCompressionSupported ($ this ->compressionSupported );
794800 }
795801
796- $ this ->setRequestResource ('request ' , fn () => $ request );
797- $ this ->setRequestResource ('response ' , fn () => $ response );
802+ $ this ->setContext ('request ' , fn () => $ request );
803+ $ this ->setContext ('response ' , fn () => $ response );
798804
799805 try {
800806 foreach (self ::$ requestHooks as $ hook ) {
801807 $ arguments = $ this ->getArguments ($ hook , [], []);
802808 \call_user_func_array ($ hook ->getAction (), $ arguments );
803809 }
804810 } catch (\Exception $ e ) {
805- $ this ->setRequestResource ('error ' , fn () => $ e , []);
811+ $ this ->setContext ('error ' , fn () => $ e , []);
806812
807813 foreach (self ::$ errors as $ error ) { // Global error hooks
808814 if (\in_array ('* ' , $ error ->getGroups ())) {
@@ -832,7 +838,7 @@ private function runInternal(Request $request, Response $response): static
832838 $ route = $ this ->match ($ request );
833839 $ groups = ($ route instanceof Route) ? $ route ->getGroups () : [];
834840
835- $ this ->setRequestResource ('route ' , fn () => $ route , []);
841+ $ this ->setContext ('route ' , fn () => $ route , []);
836842
837843 if (self ::REQUEST_METHOD_HEAD === $ method ) {
838844 $ method = self ::REQUEST_METHOD_GET ;
@@ -860,7 +866,7 @@ private function runInternal(Request $request, Response $response): static
860866 foreach (self ::$ errors as $ error ) { // Global error hooks
861867 /** @var Hook $error */
862868 if (\in_array ('* ' , $ error ->getGroups ())) {
863- $ this ->setRequestResource ('error ' , fn () => $ e , []);
869+ $ this ->setContext ('error ' , fn () => $ e , []);
864870 \call_user_func_array ($ error ->getAction (), $ this ->getArguments ($ error , [], $ request ->getParams ()));
865871 }
866872 }
@@ -878,7 +884,7 @@ private function runInternal(Request $request, Response $response): static
878884 $ route ->path ($ path );
879885
880886 $ this ->setRoute ($ route );
881- $ this ->setRequestResource ('route ' , fn () => $ route , []);
887+ $ this ->setContext ('route ' , fn () => $ route , []);
882888 }
883889 if (null !== $ route ) {
884890 return $ this ->execute ($ route , $ request , $ response );
@@ -902,15 +908,15 @@ private function runInternal(Request $request, Response $response): static
902908 } catch (\Throwable $ e ) {
903909 foreach (self ::$ errors as $ error ) { // Global error hooks
904910 if (\in_array ('* ' , $ error ->getGroups ())) {
905- $ this ->setRequestResource ('error ' , fn () => $ e , []);
911+ $ this ->setContext ('error ' , fn () => $ e , []);
906912 \call_user_func_array ($ error ->getAction (), $ this ->getArguments ($ error , [], $ request ->getParams ()));
907913 }
908914 }
909915 }
910916 } else {
911917 foreach (self ::$ errors as $ error ) { // Global error hooks
912918 if (\in_array ('* ' , $ error ->getGroups ())) {
913- $ this ->setRequestResource ('error ' , fn () => new Exception ('Not Found ' , 404 ), []);
919+ $ this ->setContext ('error ' , fn () => new Exception ('Not Found ' , 404 ), []);
914920 \call_user_func_array ($ error ->getAction (), $ this ->getArguments ($ error , [], $ request ->getParams ()));
915921 }
916922 }
0 commit comments