1313
1414namespace Ymir \Runtime ;
1515
16- use AsyncAws \Lambda \LambdaClient ;
1716use AsyncAws \Ssm \Input \GetParametersByPathRequest ;
18- use AsyncAws \Ssm \SsmClient ;
1917use AsyncAws \Ssm \ValueObject \Parameter ;
2018use Tightenco \Collect \Support \Arr ;
19+ use Ymir \Runtime \Aws \LambdaClient ;
20+ use Ymir \Runtime \Aws \SsmClient ;
2121use Ymir \Runtime \Exception \InvalidConfigurationException ;
22- use Ymir \Runtime \FastCgi \PhpFpmProcess ;
2322use Ymir \Runtime \Lambda \Handler \ConsoleCommandLambdaEventHandler ;
2423use Ymir \Runtime \Lambda \Handler \Http as HttpHandler ;
2524use Ymir \Runtime \Lambda \Handler \LambdaEventHandlerCollection ;
@@ -38,27 +37,24 @@ class Runtime
3837 public static function create (): RuntimeInterface
3938 {
4039 $ coldStart = microtime (true );
41- $ logger = new Logger (getenv ('YMIR_RUNTIME_LOG_LEVEL ' ) ?: Logger::INFO );
42- $ runtimeApiClient = new RuntimeApiClient ((string ) getenv ('AWS_LAMBDA_RUNTIME_API ' ), $ logger );
40+ $ context = RuntimeContext::createFromEnvironment ();
41+
42+ $ logger = $ context ->getLogger ();
43+ $ rootDirectory = $ context ->getRootDirectory ();
44+ $ runtimeApiClient = $ context ->getRuntimeApiClient ();
4345
4446 try {
4547 $ functionType = getenv ('YMIR_FUNCTION_TYPE ' );
46- $ region = getenv ('AWS_REGION ' );
47- $ rootDirectory = getenv ('LAMBDA_TASK_ROOT ' );
4848
4949 if (!is_string ($ functionType )) {
5050 throw new InvalidConfigurationException ('The "YMIR_FUNCTION_TYPE" environment variable is missing ' );
51- } elseif (!is_string ($ rootDirectory )) {
52- throw new InvalidConfigurationException ('The "LAMBDA_TASK_ROOT" environment variable is missing ' );
53- } elseif (!is_string ($ region )) {
54- throw new InvalidConfigurationException ('The "AWS_REGION" environment variable is missing ' );
5551 }
5652
57- self ::injectSecretEnvironmentVariables ($ logger , $ region );
53+ self ::injectSecretEnvironmentVariables ($ context );
5854
5955 $ handlers = [
6056 new PingLambdaEventHandler (),
61- new WarmUpEventHandler (new LambdaClient ([ ' region ' => $ region ], null , null , $ logger ), $ logger ),
57+ new WarmUpEventHandler (LambdaClient:: createFromContext ( $ context ), $ logger ),
6258 ];
6359
6460 switch ($ functionType ) {
@@ -76,8 +72,7 @@ public static function create(): RuntimeInterface
7672
7773 break ;
7874 case WebsiteRuntime::TYPE :
79- $ maxInvocations = ((int ) getenv ('YMIR_RUNTIME_MAX_INVOCATIONS ' )) ?: null ;
80- $ phpFpmProcess = PhpFpmProcess::createForConfig ($ logger );
75+ $ phpFpmProcess = $ context ->getPhpFpmProcess ();
8176
8277 $ runtime = new WebsiteRuntime ($ runtimeApiClient , new LambdaEventHandlerCollection ($ logger , array_merge ($ handlers , [
8378 // Application/Framework specific handlers
@@ -88,7 +83,7 @@ public static function create(): RuntimeInterface
8883
8984 // Fallback handlers
9085 new HttpHandler \PhpScriptHttpEventHandler ($ logger , $ phpFpmProcess , $ rootDirectory , getenv ('_HANDLER ' ) ?: 'index.php ' ),
91- ])), $ logger , $ phpFpmProcess , $ maxInvocations );
86+ ])), $ logger , $ phpFpmProcess , $ context -> getMaxInvocations () );
9287
9388 $ runtime ->start ();
9489
@@ -111,7 +106,7 @@ public static function create(): RuntimeInterface
111106 /**
112107 * Inject the secret environment variables into the runtime.
113108 */
114- private static function injectSecretEnvironmentVariables (Logger $ logger , string $ region ): void
109+ private static function injectSecretEnvironmentVariables (RuntimeContext $ context ): void
115110 {
116111 $ secretsPath = getenv ('YMIR_SECRETS_PATH ' );
117112
@@ -124,13 +119,13 @@ private static function injectSecretEnvironmentVariables(Logger $logger, string
124119 // results because they use a numbered index.
125120 //
126121 // @see https://stackoverflow.com/questions/70536304/why-does-iterator-to-array-give-different-results-than-foreach
127- collect (iterator_to_array (( new SsmClient ([ ' region ' => $ region ], null , null , $ logger ) )->getParametersByPath (new GetParametersByPathRequest ([
122+ collect (iterator_to_array (SsmClient:: createFromContext ( $ context )->getParametersByPath (new GetParametersByPathRequest ([
128123 'Path ' => $ secretsPath ,
129124 'WithDecryption ' => true ,
130125 ])), false ))->mapWithKeys (function (Parameter $ parameter ) {
131126 return [Arr::last (explode ('/ ' , (string ) $ parameter ->getName ())) => (string ) $ parameter ->getValue ()];
132- })->filter ()->each (function ($ value , $ name ) use ($ logger ): void {
133- $ logger ->debug (sprintf ('Injecting [%s] secret environment variable into runtime ' , $ name ));
127+ })->filter ()->each (function ($ value , $ name ) use ($ context ): void {
128+ $ context -> getLogger () ->debug (sprintf ('Injecting [%s] secret environment variable into runtime ' , $ name ));
134129 $ _ENV [$ name ] = $ value ;
135130 });
136131 }
0 commit comments