在 composer.json 中运行命令行命令

Posted

技术标签:

【中文标题】在 composer.json 中运行命令行命令【英文标题】:Run command line command in composer.json 【发布时间】:2018-07-18 20:12:09 【问题描述】:

我正在尝试编写一个 composer.json 文件,该文件将连续运行多个命令行命令,因此就像这样的示例:

"scripts": 
   "test": [
        "@createDir"
   ],
   "createDir": "mkdir testing"


当我使用composer.phar update 在终端中运行作曲家文件时,该目录并未创建。谁能指出正确的方向如何做到这一点或我做错了什么?

【问题讨论】:

可能想看看Events section (pre-update-cmd or post-update-cmd) 【参考方案1】:

默认情况下,Composer 不会在 installupdate 的末尾运行所有脚本。为此,您的脚本需要位于 Command Event 键之一下,例如post-update-cmd.

您仍然可以在这些块中引用其他脚本,例如

"scripts": 
    "post-install-cmd": [
        "@test"
    ],
    "test": [
        "touch foo"
    ]

要运行单个脚本,请使用run-script 命令:

composer run-script test

【讨论】:

也可以使用:composer test【参考方案2】:

可能结构不对,我举个例子:


"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": 
    "php": ">=5.6.4",
    "laravel/framework": "5.3.*",
    "acacha/admin-lte-template-laravel": "dev-master",
    "yajra/laravel-datatables-oracle": "^6.21",
    "barryvdh/laravel-dompdf": "^0.7.0",
    "spatie/laravel-backup": "^3.0.0"
,
"require-dev": 
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~5.0",
    "symfony/css-selector": "3.1.*",
    "symfony/dom-crawler": "3.1.*"
,
"autoload": 
    "classmap": [
        "database",
        "App/Helpers/MyCustomHelper"
    ],
    "psr-4": 
        "App\\": "app/"
    
,
"autoload-dev": 
    "classmap": [
        "tests/TestCase.php"
    ]
,
"scripts": 
    "post-root-package-install": [
        "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
    ]
,
"config": 
    "preferred-install": "dist"


编辑:也许你可以试试composer dump-autoload

【讨论】:

【参考方案3】:

如果您想运行一个应该永远运行的命令,这将对您有所帮助

在 composer.json 中

"scripts": 
   
    "start-lumen" : [
        "php -S localhost:8000 -t public"
    ]


添加 --timeout=0 以永久运行

composer run-script  start-lumen --timeout=0

【讨论】:

以上是关于在 composer.json 中运行命令行命令的主要内容,如果未能解决你的问题,请参考以下文章

composer 命令清单

通过composer安装阿里大于接口扩展

Composer & PHPUnit 安装记录

composer常用命令

[PHP]Yaf + composer 引起大幅性能下降

codecept:找不到命令