P粉0685109912023-08-01 09:38:32
I have successfully achieved what I wanted to do. So here I'll explain the help I'm looking for.
Normally, if I do not use any custom installation plug-in, Composer will install my package in the vendor directory named "rootdata21/hati". But for some reason my entire package source code needs to be in the project root. And I don't want to have a parent folder named rootdata21 either.
So I wrote a plugin for this. The plugin returns "rootdata21" as the installation path. It puts my package in the root directory, but the folder structure now becomes "rootdata21/hati". Therefore, I had to override the installation method to modify it. However, even if I get the folder location and structure I want by copying/renaming/deleting the folders from "rootdata21/hati", the autoloader still doesn't work with my relocated source code. I then had to manually update the composer.json file to regenerate the autoloader, which defeats the purpose of having an installer. This is what I want to achieve, is that after moving my package folder to the project root, the autoloader still works properly.
This is my final updated installer code that works the way I want it to.
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... }); }
After all these operations, the vendor/composer/autoload_psr4.php file has the classpath correctly set, as you can see in the screenshot.
I had to return "hati" as the installation path because if I returned "rootdata21" and used the installation code above, I would get the following autoload_psr4.php record, which does not work properly.
'hati\' => array($baseDir . '/rootdata')