Composer Startup of multiple files in a folder

I use composer in my last project and map my function as follows

"require": { ... }, "require-dev": { ... }, "autoload": { "psr-4": { ... }, "files": [ "src/function/test-function.php" ] } 

I assume that there will be many files in the folder, for example: real-function-1.php, real-function-2.php, etc. So, can a composer call all the files in a folder function? I'm lazy to use

 "files": [ "src/function/real-function-1.php", "src/function/real-function-2.php", .., "src/function/real-function-100.php", ] 

Is there a lazy person like me ...

+6
source share
1 answer

If you cannot use the namespace for your functions (because it breaks a bunch of code or because you cannot use PSR-4), and you do not want to create static classes that contain your functions (which could then autoload), you can make your own global include file and then tell the composer to include it.

composer.json

 { "autoload": { "files": [ "src/function/include.php" ] } } 

include.php

 $files = glob(__DIR__ . '/real-function-*.php'); if ($files === false) { throw new RuntimeException("Failed to glob for function files"); } foreach ($files as $file) { require_once $file; } unset($file); unset($files); 

This is not ideal, since it downloads every file for every request, regardless of whether functions will be used in it, but it will work.

+12
source

Source: https://habr.com/ru/post/976189/


All Articles