作曲家=$作曲家; $這個->根= $根。目錄分隔符號; $這個-> 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) 如 $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')