dongliehuan3925 2018-01-29 12:22
浏览 102

Composer脚本无法正常工作

I'm working on a Magento project in which I want to copy a file from a module directory to the root directory on composer install.

I thought I could use composer scripts for this but I can't get it to work. I've tried using both the post-autoload-dump and post-install-cmd events. Any suggestions, maybe I can use another event that is fired after composer install is run? Or am I missing something else

My Module Structure

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";
        }
    }
}

Temrinal Output

$ 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

After runnin

  • 写回答

1条回答 默认 最新

  • dsf6281 2018-01-29 13:47
    关注

    See https://getcomposer.org/doc/articles/scripts.md for the documentation of scripts. The most relevant part is:

    Note: Only scripts defined in the root package's composer.json are executed. If a dependency of the root package specifies its own scripts, Composer does not execute those additional scripts.

    So, you cannot define any scripts in your module. There is a bug report about that, but the maintainer of composer is not a friend of executing the scripts of dependencies

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看