Composer脚本无效

时间:2018-01-29 12:22:07

标签: php magento composer-php magento2

我正在开发一个Magento项目,我希望将文件从模块目录复制到composer install上的根目录。

我认为我可以使用composer scripts,但我无法使用它。我已尝试同时使用post-autoload-dumppost-install-cmd事件。任何建议,也许我可以使用在运行composer安装后触发的另一个事件?或者我错过了其他的东西

我的模块结构

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

跑完后

1 个答案:

答案 0 :(得分:1)

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

  

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

因此,您无法在模块中定义任何脚本。有一个bug report,但作曲家的维护者不是执行依赖脚本的朋友