【发布时间】:2018-07-08 03:26:25
【问题描述】:
我正在处理一个 Magento 项目,我想在其中将文件从模块目录复制到 composer install 的根目录。
我以为我可以为此使用composer scripts,但我无法让它工作。我试过同时使用post-autoload-dump 和post-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