php - 为 Composer 创建事件/插件

标签 php composer-php

我开发了一个可移植 WebServer,我也在创建一个可移植控制台,以使用 Composer。

我有一个问题。我需要创建一个插件来向 Composer 添加额外的行为。

我需要在使用 Composer 下载任何包时编辑该包的 composer.json“脚本”,以便它可以在可移植控制台上运行。

例如下载Laravel时:

原始 composer.json:

{
    "name": "laravel/laravel",
    ...
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        ...
    },
    ...
}

插件编辑的composer.json:

{
    "name": "laravel/laravel",
    ...
    "scripts": {
        "post-root-package-install": [
            "F:/portable_php_path/php.exe -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        ...
    },
    ...
}
  • 请注意,已为 php.exe 生成物理路径,因为在便携版本中它可以位于任何路径。

(我的问题是关于 Composer 插件的创建。我用 PHP 编辑 composer.json 没问题。)

我在 Composer 网站上阅读了创建插件的教程,但我很困惑。 ( https://getcomposer.org/doc/articles/plugins.md )

如果有另一种方法可以做到这一点,那也很有趣。我接受其他建议和想法。

感谢任何能提供帮助的人。

[抱歉我的英语不好]

最佳答案

我想你可以有一个插件实现 PluginInterfaceEventSubscriberInterface

public static function getSubscribedEvents()
{
    return [
        'post-package-install' => 'onPostPackageInstall'
        // hook  post-package-install using onPostPackageInstall method
    ];
}

public function onPostPackageInstall(\Composer\Installer\PackageEvent $event)
{
    $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir') . '/';

    /** @var InstallOperation $item */
    foreach ($event->getOperations() as $item) {

        $packageInstalled = $item->getPackage()->getName();
        // do any thing with the package name like `laravel/laravel`
        //You can now edit the composer.json file 

        echo $vendorDir . $packageInstalled . '/composer.json';

    }

}

关于php - 为 Composer 创建事件/插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45334519/

相关文章:

php - 如何使用 MySQL/PHP 对数据库结果进行排序和显示

php - 搜索按钮和文本框的CSS

PHP:是否有可能以某种方式将 HTML 嵌入到三元运算符的中间?

php - 但这些与您的要求或最低稳定性相冲突

git - Composer 在单独的存储库上提取最新的提交

php - 使用 CSS 加载从文件夹更改的图像

php - 如何检测PHP脚本是否已经在运行?

symfony - 我的composer.json 文件在生产环境中应该是什么样子?

symfony - 什么时候应该在composer.json中使用 "dev-master"?

未找到 PHPUnit 类 TestCase