@@ -173,62 +173,83 @@ class ServerlessPlugin {
173173 * Process the `php-xx` runtimes to turn them into `provided.al2` runtimes + Bref layers.
174174 */
175175 processPhpRuntimes ( ) {
176- const includeBrefLayers = ( runtime , existingLayers , isArm ) => {
177- let layerName = runtime ;
176+ const includeBrefLayers = ( existingLayers , phpVersion , isArm ) => {
177+ let layerName = 'php-' + phpVersion ;
178178 // Automatically use ARM layers if the function is deployed to an ARM architecture
179179 if ( isArm ) {
180180 layerName = 'arm-' + layerName ;
181181 }
182- if ( layerName . endsWith ( '-console' ) ) {
183- layerName = layerName . substring ( 0 , layerName . length - '-console' . length ) ;
184- existingLayers . unshift ( this . getLayerArn ( 'console' , this . provider . getRegion ( ) ) ) ;
185- existingLayers . unshift ( this . getLayerArn ( layerName , this . provider . getRegion ( ) ) ) ;
186- } else {
187- existingLayers . unshift ( this . getLayerArn ( layerName , this . provider . getRegion ( ) ) ) ;
188- }
182+ existingLayers . unshift ( this . getLayerArn ( layerName , this . provider . getRegion ( ) ) ) ;
189183 return existingLayers ;
190184 }
185+ /**
186+ * @param {string } runtime
187+ * @return {string|undefined }
188+ */
189+ const runtimeStringToRuntimeClass = ( runtime ) => {
190+ if ( ! runtime . startsWith ( 'php-' ) ) {
191+ return undefined ;
192+ }
193+ if ( runtime . endsWith ( '-console' ) ) {
194+ return 'Bref\\ConsoleRuntime\\Main' ;
195+ }
196+ if ( runtime . endsWith ( '-fpm' ) ) {
197+ return 'Bref\\FpmRuntime\\Main' ;
198+ }
199+ return 'Bref\\FunctionRuntime\\Main' ;
200+ } ;
201+ const configureFunctionRuntime = ( f ) => {
202+ // `php-\d\d(-fpm|console)?`
203+ const fullRuntimeString = f . runtime || config . provider . runtime ;
204+ if ( ! fullRuntimeString || ! fullRuntimeString . startsWith ( 'php-' ) ) {
205+ return ;
206+ }
207+ const phpVersion = fullRuntimeString . substring ( 'php-' . length ) . split ( '-' ) [ 0 ] ;
208+ const runtimeClass = runtimeStringToRuntimeClass ( fullRuntimeString ) ;
209+ if ( ! runtimeClass ) return ;
210+
211+ // The logic here is a bit custom:
212+ // If there are layers on the function, we preserve them
213+ let existingLayers = f . layers || [ ] ; // make sure it's an array
214+ // Else, we merge with the layers defined at the root.
215+ // Indeed, SF overrides the layers defined at the root with the ones defined on the function.
216+ if ( existingLayers . length === 0 ) {
217+ // for some reason it's not always an array
218+ existingLayers = Array . from ( config . provider . layers || [ ] ) ;
219+ }
220+
221+ f . layers = includeBrefLayers (
222+ existingLayers ,
223+ phpVersion ,
224+ f . architecture === 'arm64' || ( isArmGlobally && ! f . architecture ) ,
225+ ) ;
226+ f . runtime = 'provided.al2' ;
227+ // Add the `BREF_RUNTIME` environment variable
228+ // to let the function know which runtime it is using
229+ // (this is used by the Bref runtime)
230+ if ( ! f . environment ) {
231+ f . environment = { } ;
232+ }
233+ if ( ! f . environment . BREF_RUNTIME ) {
234+ f . environment . BREF_RUNTIME = runtimeClass ;
235+ }
236+ }
191237
192238 const config = this . serverless . service ;
193239 const isArmGlobally = config . provider . architecture === 'arm64' ;
194240 const isBrefRuntimeGlobally = this . runtimes . includes ( config . provider . runtime || '' ) ;
195241
196242 // Check functions config
197243 for ( const f of Object . values ( config . functions || { } ) ) {
198- if (
199- ( f . runtime && this . runtimes . includes ( f . runtime ) ) ||
200- ( ! f . runtime && isBrefRuntimeGlobally )
201- ) {
202- // The logic here is a bit custom:
203- // If there are layers on the function, we preserve them
204- let existingLayers = f . layers || [ ] ; // make sure it's an array
205- // Else, we merge with the layers defined at the root.
206- // Indeed, SF overrides the layers defined at the root with the ones defined on the function.
207- if ( existingLayers . length === 0 ) {
208- // for some reason it's not always an array
209- existingLayers = Array . from ( config . provider . layers || [ ] ) ;
210- }
211-
212- f . layers = includeBrefLayers (
213- f . runtime || config . provider . runtime ,
214- existingLayers ,
215- f . architecture === 'arm64' || ( isArmGlobally && ! f . architecture ) ,
216- ) ;
217- f . runtime = 'provided.al2' ;
218- }
244+ configureFunctionRuntime ( f ) ;
219245 }
220246
221247 // Check Lift constructs config
222248 for ( const construct of Object . values ( this . serverless . configurationInput . constructs || { } ) ) {
223249 if ( construct . type !== 'queue' && construct . type !== 'webhook' ) continue ;
224250 const f = construct . type === 'queue' ? construct . worker : construct . authorizer ;
225- if ( f && ( f . runtime && this . runtimes . includes ( f . runtime ) || ! f . runtime && isBrefRuntimeGlobally ) ) {
226- f . layers = includeBrefLayers (
227- f . runtime || config . provider . runtime ,
228- f . layers || [ ] , // make sure it's an array
229- f . architecture === 'arm64' || ( isArmGlobally && ! f . architecture ) ,
230- ) ;
231- f . runtime = 'provided.al2' ;
251+ if ( f ) {
252+ configureFunctionRuntime ( f ) ;
232253 }
233254 }
234255 }
0 commit comments