作曲家=$作曲家; $这个->根= $根。目录分隔符; $这个-> hatiDir = $root . DIRECTORY_SEPARATOR 。 '哈蒂'。目录分隔符; 父::__construct($io, $composer); } 公共函数 getInstallPath(PackageInterface $package): string { 返回“rootdata21”; } 公共功能安装(InstalledRepositoryInterface $repo,PackageInterface $package):?PromiseInterface { if (file_exists($this -> hatiDir)) { $choice = $this ->; $choice = $this -> io-> Ask('找到现有的 hati 文件夹。是否删除它? [y/n]: ', 'n'); if ($choice === 'y') { self::rmdir($this -> hatiDir); } 别的 { $这个-> io-> important('Hati 安装已取消。请手动删除 hati 文件夹。'); 返回空值; } } 返回父级::install($repo, $package)->then(function () { // 将hati文件夹移动到项目根目录 $旧= $这个->根 。 'rootdata21'。 DIRECTORY_SEPARATOR .'hati'; 重命名($old, $this -> hatiDir); //删除rootdata21文件夹 self::rmdir($this -> root . 'rootdata21'); // 生成/更新项目根目录下的 hati.json 文件 $createNewConfig = true; if (file_exists($this -> root . 'hati.json')) { 而(真){ $ans = $this ->; io-> Ask('现有 hati.json 已找到。是否要将其与新配置合并? [y/n]: '); if ($ans !== 'y' && $ans !== 'n') 继续; 休息; } $createNewConfig = $ans == 'n'; } require_once "{$this -> hatiDir}config" .DIRECTORY_SEPARATOR ."ConfigWriter.php"; $result = ConfigWriter::write($this->root, $createNewConfig); // 将结果显示给用户 if ($结果['成功']) { $这个-> io->信息($结果['msg']); $welcomeFile = $this ->; hatiDir。 '页面/welcome.txt'; if (file_exists($welcomeFile)) include($welcomeFile); } 别的 { $这个-> io->错误($结果['msg']); } $这个->转储自动加载(); }); } 公共函数更新(InstalledRepositoryInterface $repo,PackageInterface $initial,PackageInterface $target){ 返回父级::update($repo, $initial, $target) ->然后(函数(){ require_once "{$this -> hatiDir}config" 。 DIRECTORY_SEPARATOR 。 “ConfigWriter.php”; $result = ConfigWriter::write($this->root); // 将结果显示给用户 if ($结果['成功']) { $这个-> io-> info('Hati已更新成功'); } 别的 { $这个-> io->错误($结果['msg']); } }); } 公共函数支持($packageType): bool { return 'hati-installer' === $packageType; } 私有函数 dumpAutoload(): void { $composerJsonPath = $this ->;根 。 '作曲家.json'; $composerJson = json_decode(file_get_contents($composerJsonPath), true); $composerJson['autoload']['psr-4']['hati\'] = 'hati/'; file_put_contents($composerJsonPath, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); // 重新生成 Composer 自动加载文件以包含您的类 $这个->作曲家-> getEventDispatcher() ->; dispatchScript(ScriptEvents::POST_AUTOLOAD_DUMP); } 公共静态函数 rmdir($dir): bool { if (!file_exists($dir)) 返回 true; if (!is_dir($dir)) 返回 unlink($dir); foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') 继续; if (!self::rmdir($dir . DIRECTORY_SEPARATOR . $item)) 返回 false; } 返回 rmdir($dir); } }</pre> <p><br />></p>
P粉0685109912023-08-01 09:38:32
我已经成功实现了我想要做的事情。所以在这里,我将解释我所寻求的帮助。
通常情况下,如果不使用任何自定义的安装插件,Composer会将我的包安装在名为"rootdata21/hati"的vendor目录下。但由于某些原因,我的整个包源代码需要位于项目根目录下。而且我也不希望有名为rootdata21的父文件夹。
因此,我为此编写了一个插件。该插件将"rootdata21"作为安装路径返回。它将我的包放在了根目录下,但是文件夹结构现在变成了"rootdata21/hati"。因此,我不得不重写安装方法来修改它。然而,即使我通过从"rootdata21/hati"复制/重命名/删除文件夹来获得我想要的文件夹位置和结构,自动加载器对我重新定位的源代码仍然无效。然后,我不得不手动更新composer.json文件来重新生成自动加载器,这与拥有安装程序的目的相悖。这就是我想要实现的,即在将我的包文件夹移动到项目根目录后,自动加载器仍然能够正常工作。
这是我最终更新的安装程序代码,它按照我想要的方式工作。
public function getInstallPath(PackageInterface $package): string { return 'hati'; }
public function install(InstalledRepositoryInterface $repo, PackageInterface $package): ?PromiseInterface { // Setting custom psr-4 entry for hati folder being on project root $autoload = $package -> getAutoload(); if (isset($autoload['psr-4'])) { $customPSR4 = ['hati\' => '/',]; $autoload['psr-4'] = array_merge($autoload['psr-4'], $customPSR4); // Let the composer know about this $package -> setAutoload($autoload); } return parent::install($repo, $package) -> then(function () { // Manipulate the hati/hati folder to hati on project root self::copy($this -> root . 'hati' . DIRECTORY_SEPARATOR . 'hati', $this -> root . '_temp'); self::rmdir($this -> root . 'hati'); rename($this -> root . '_temp',$this -> root . 'hati'); // rest of the installation code goes here... }); }
在所有这些操作之后,vendor/composer/autoload_psr4.php文件正确地设置了类路径,您可以在截图中看到。
我不得不将"hati"作为安装路径返回,因为如果返回"rootdata21"并使用上面的安装代码,将会得到以下的autoload_psr4.php记录,而这并不能正常工作。
'hati\' => array($baseDir . '/rootdata')