So deinstallieren Sie Autoloading im benutzerdefinierten Installations-Plugin
P粉466909449
P粉466909449 2023-07-31 10:06:51
0
1
384

Ich habe versucht, ein benutzerdefiniertes Installationsprogramm für mein Composer-Paket zu schreiben, kann es aber nicht zum Laufen bringen. Was ich jetzt brauche ist:

  • Ich möchte, dass mein Paket im Stammverzeichnis installiert wird. Mein Paketname ist rootdata21/hati, daher habe ich den Hati-Ordner in das Stammverzeichnis des Projekts verschoben.
  • Jetzt habe ich es aktualisiert, indem ich einen Eintrag zum Autoload-PSR4-Attribut der Composer.json-Datei wie folgt hinzugefügt habe: { "autoload": { "psr-4": { "hati" : "hati /" } } }

Aber ich weiß eigentlich nicht, wie ich Composer dazu bringen kann, den Autoloader neu zu generieren, um die in der Composer.json-Datei widerzuspiegeln. Dies Neuer Autoload-Eintrag. Unten ist meine Installer-Klasse.


 komponist = $composer; $this -> root = $root . DIRECTORY_SEPARATOR; $this -> hatiDir = $root . DIRECTORY_SEPARATOR . 'hati' . DIRECTORY_SEPARATOR; parent::__construct($io, $composer); } öffentliche Funktion getInstallPath(PackageInterface $package): string { return 'rootdata21'; } öffentliche Funktion install(InstalledRepositoryInterface $repo, PackageInterface $package): ?PromiseInterface { if (file_exists($this -> hatiDir)) { $choice = $this -> io -> ask('Vorhandener Hati-Ordner gefunden. Möchten Sie ihn löschen? [y/n]: ', 'n'); if ($choice === 'y') { self::rmdir($this -> hatiDir); } anders { $this -> io -> Critical('Hati-Installation wurde abgebrochen. Bitte löschen Sie den Hati-Ordner manuell.'); null zurückgeben; } } return parent::install($repo, $package)->then(function () { // Hati-Ordner in das Stammverzeichnis des Projekts verschieben $old = $this -> Wurzel . 'rootdata21'. DIRECTORY_SEPARATOR .'hati'; rename($old, $this -> hatiDir); // den Ordner rootdata21 löschen self::rmdir($this -> root . 'rootdata21'); // die Datei hati.json im Stammverzeichnis des Projekts generieren/aktualisieren $createNewConfig = true; if (file_exists($this -> root . 'hati.json')) { while(true) { $ans = $this -> io -> ask('Vorhandenes hati.json gefunden. Möchten Sie es mit der neuen Konfiguration zusammenführen? [y/n]: '); if ($ans !== 'y' && $ans !== 'n') continue; brechen; } $createNewConfig = $ans == 'n'; } require_once "{$this -> hatiDir}config" . DIRECTORY_SEPARATOR . "ConfigWriter.php"; $result = ConfigWriter::write($this->root, $createNewConfig); // zeige dem Benutzer das Ergebnis if ($result['success']) { $this -> io -> info($result['msg']); $welcomeFile = $this -> hatiDir . 'page/welcome.txt'; if (file_exists($welcomeFile)) include($welcomeFile); } anders { $this -> io -> Fehler($result['msg']); } $this -> dumpAutoload(); }); } öffentliche Funktionsaktualisierung(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { return parent::update($repo, $initial, $target) -> then(Funktion () { require_once "{$this -> hatiDir}config" . DIRECTORY_SEPARATOR . „ConfigWriter.php“; $result = ConfigWriter::write($this->root); // zeige dem Benutzer das Ergebnis if ($result['success']) { $this -> io -> info('Hati wurde erfolgreich aktualisiert'); } anders { $this -> io -> Fehler($result['msg']); } }); } öffentliche Funktion unterstützt($packageType): bool { return 'hati-installer' === $packageType; } private Funktion dumpAutoload(): void { $composerJsonPath = $this -> Wurzel . 'composer.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)); // Generieren Sie die Composer-Autoload-Dateien neu, um Ihre Klassen einzuschließen $this -> Komponist -> getEventDispatcher() -> DispatchScript(ScriptEvents::POST_AUTOLOAD_DUMP); } öffentliche statische Funktion rmdir($dir): bool { if (!file_exists($dir)) return true; if (!is_dir($dir)) return unlink($dir); foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; if (!self::rmdir($dir . DIRECTORY_SEPARATOR . $item)) return false; } return rmdir($dir); } }


P粉466909449
P粉466909449

Antworte allen (1)
P粉068510991

我已经成功实现了我想要做的事情。所以在这里,我将解释我所寻求的帮助。

通常情况下,如果不使用任何自定义的安装插件,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')
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage
    Über uns Haftungsausschluss Sitemap
    Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!