【问题标题】:Composer script not working作曲家脚本不起作用
【发布时间】:2018-07-08 03:26:25
【问题描述】:

我正在处理一个 Magento 项目,我想在其中将文件从模块目录复制到 composer install 的根目录。

我以为我可以为此使用composer scripts,但我无法让它工作。我试过同时使用post-autoload-dumppost-install-cmd 事件。有什么建议,也许我可以使用运行 composer install 后触发的另一个事件?还是我错过了其他东西

我的模块结构

app/code/Holy/Composer/
├── Scripts.php
└── composer.json

composer.json

{
  "name": "holy/module-composer",
  "description": "Copies app/code/vendor/module/file.php to project root",
  "autoload": {
    "psr-4": {
      "Holy\\Composer\\": ""
    }
  },
  "scripts": {
    "post-install-cmd": "Holy\\Composer\\Scripts::postInstall",
    "post-autoload-dump": "Holy\\Composer\\Scripts::postAutoloadDump"
  }
}

Scripts.php

<?php

namespace Holy\Composer;

use Composer\Script\Event;
use Composer\Installer\PackageEvent;

class Scripts
{
    public static function postInstall(Event $event)
    {
        $composer = $event->getComposer();

        $filename = './app/code/Vendor/Module/file.php';

        if (file_exists($filename)) {
            echo "Copying $filename to the root directory.";
            copy($filename, './file.php');

        } else {
            echo "$filename does not exist, cannot copy it to the root directory";
        }
    }

    public static function postAutoloadDump(Event $event)
    {
        $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
        require $vendorDir . '/autoload.php';

        $filename = './app/code/Vendor/Module/ampiframe.php';

        if (file_exists($filename)) {
            echo "Copying $filename to the root directory.";
            copy($filename, './file.php');

        } else {
            echo "$filename does not exist, cannot copy it to the root directory";
        }
    }
}

终端输出

$ composer install
Warning: This development build of composer is over 60 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Package sjparkinson/static-review is abandoned, you should avoid using it. Use phpro/grumphp instead.
Package fabpot/php-cs-fixer is abandoned, you should avoid using it. Use friendsofphp/php-cs-fixer instead.
Generating autoload files

$ ls
CHANGELOG.md         LICENSE_AFL.txt  index.php            pub
CONTRIBUTING.md      app              lib                  setup
COPYING.txt          bin              nginx.conf.sample    var
Gruntfile.js.sample  composer.json    package.json.sample  vendor
ISSUE_TEMPLATE.md    composer.lock    php.ini.sample
LICENSE.txt          dev              phpserver

跑步后

【问题讨论】:

    标签: php magento composer-php magento2


    【解决方案1】:

    有关脚本的文档,请参阅https://getcomposer.org/doc/articles/scripts.md。最相关的部分是:

    注意:只有在根包的composer.json 中定义的脚本是 执行。如果根包的依赖项指定了自己的 脚本,Composer 不会执行那些额外的脚本。

    因此,您不能在模块中定义任何脚本。有一个bug report关于那个,但是composer的维护者不是执行依赖脚本的朋友

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 2017-03-13
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多