admin管理员组文章数量:1336293
I am writing a bash script to ease installing a new site.
The laravel project needs included files which just contain functions and not classes, and currently I have manually edited composer.json
which works fine.
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/DD_laravelAp/Helpers/php_extend/php_extend.php"
]
},
However, I would like the bash script to automatically update the composer.json file to result in e.g.:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/DD_laravelAp/Helpers/php_extend/php_extend.php",
"app/DD_laravelAp/Helpers/php_extend/array_extend.php"
]
},
My question is the syntax from the command line to include a new file from a bash script for example when in the site directory
composer require "app/DD_laravelAp/Helpers/php_extend/array_extend.php"
to update the composer.json file to the second one above. I am sure I have seen this documented but cannot find where
I am writing a bash script to ease installing a new site.
The laravel project needs included files which just contain functions and not classes, and currently I have manually edited composer.json
which works fine.
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/DD_laravelAp/Helpers/php_extend/php_extend.php"
]
},
However, I would like the bash script to automatically update the composer.json file to result in e.g.:
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/DD_laravelAp/Helpers/php_extend/php_extend.php",
"app/DD_laravelAp/Helpers/php_extend/array_extend.php"
]
},
My question is the syntax from the command line to include a new file from a bash script for example when in the site directory
composer require "app/DD_laravelAp/Helpers/php_extend/array_extend.php"
to update the composer.json file to the second one above. I am sure I have seen this documented but cannot find where
Share edited Nov 20, 2024 at 15:01 yivi 47.7k18 gold badges130 silver badges154 bronze badges asked Nov 19, 2024 at 18:27 DatadimensionDatadimension 1,0551 gold badge16 silver badges37 bronze badges 1- Wouldn't it be easier to put these files into a proper package? That way, you could also update them for all your projects – Nico Haase Commented Nov 20, 2024 at 7:55
2 Answers
Reset to default 1Composer does not offer a feature to modify the autoload
section from the command-line interface.
It was requested many years ago, but it was officially rejected a couple of years after that:
Yup I don't think we really want to offer this from CLI it's gonna be a bunch of code for very limited use as typically this is done once on package creation.
If you check that issue thread, you'll see many scripts that deal with similar use-cases, some of which you can tailor to your specific need.
A very crude PHP implementation you could use within your bash script:
#!/usr/bin/env php
<?php
if (count($argv) < 2) {
exit;
}
$json = json_decode(file_get_contents('composer.json'), true);
array_shift($argv);
foreach ($argv as $arg) {
$json['autoload']['files'][] = $arg;
}
file_put_contents('composer.json', json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
Use it as:
./patch_autoload.php "app/DD_laravelAp/Helpers/php_extend/array_extend.php" "app/DD_laravelAp/Helpers/php_extend/php_extend.php"
(Assuming you call the script patch_autoload.php
and make it executable).
In the spirit of sharing I made an all in one php file. In explaination of the first 2 lines, they simply use local values to determine where the composer.json file is, substitute your method here.
<?php
include(getenv('HOME') . "/bashtools/php_bash/bash.env.php");
$jsonpath = $wwwroot . "/html/" . $www_sitefocus . "/composer.json";
$json = json_decode(file_get_contents($jsonpath), true);
$includes=[
"app/DD_laravelAp/Helpers/php_extend/php_extend.php",
"app/DD_laravelAp/Helpers/php_extend/array_extend.php",
"app/DD_laravelAp/Helpers/php_extend/string_extend.php",
];
foreach ($includes as $path) {
if(!in_array($path,$json['autoload']['files'])) {
array_push($json['autoload']['files'], $path);
}
}
$json=json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
file_put_contents($jsonpath, $json);
本文标签: phpHow to modify the quotfilesquot array of the autoload configuration with a commandStack Overflow
版权声明:本文标题:php - How to modify the "files" array of the autoload configuration with a command? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406116a2468855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论