vendor/ibexa/installer/src/lib/Event/Subscriber/InstallCommandSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Installer\Event\Subscriber;
  8. use Composer\InstalledVersions;
  9. use Ibexa\Bundle\RepositoryInstaller\Command\InstallPlatformCommand;
  10. use Symfony\Component\Console\ConsoleEvents;
  11. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class InstallCommandSubscriber implements EventSubscriberInterface
  15. {
  16. /** @const array<int, string> Order of this array is very important */
  17. public const IBEXA_PRODUCTS = [
  18. 'ibexa/commerce',
  19. 'ibexa/experience',
  20. 'ibexa/headless',
  21. 'ibexa/oss',
  22. ];
  23. public function onCommand(ConsoleCommandEvent $event): void
  24. {
  25. $command = $event->getCommand();
  26. if (!$command instanceof InstallPlatformCommand) {
  27. return;
  28. }
  29. if (!$event->getInput()->getArgument('type')) {
  30. $event->getOutput()->writeln(
  31. 'No "type" argument passed. '
  32. . 'Installer will automatically choose installation '
  33. . 'type relevant to your product edition'
  34. );
  35. }
  36. $definition = $command->getDefinition();
  37. $arguments = $definition->getArguments();
  38. $typeArgument = $arguments['type'];
  39. $newTypeArgument = new InputArgument(
  40. 'type',
  41. InputArgument::OPTIONAL,
  42. $typeArgument->getDescription(),
  43. $this->getInstallerType()
  44. );
  45. $definition->setArguments([$newTypeArgument]);
  46. $command->setDefinition($definition);
  47. }
  48. public static function getSubscribedEvents(): array
  49. {
  50. return [
  51. ConsoleEvents::COMMAND => ['onCommand', 0],
  52. ];
  53. }
  54. private function getInstallerType(): string
  55. {
  56. return str_replace('/', '-', $this->getInstalledIbexaProduct());
  57. }
  58. private function getInstalledIbexaProduct(): string
  59. {
  60. $packages = InstalledVersions::getInstalledPackages();
  61. $ibexaPackages = array_filter($packages, static function (string $packageName): bool {
  62. return strpos($packageName, 'ibexa/') !== false;
  63. });
  64. // removes unrelated Ibexa packages
  65. $installedIbexaProducts = array_values(array_intersect($ibexaPackages, self::IBEXA_PRODUCTS));
  66. // sorts $installedIbexaProducts according to the order of self::IBEXA_PRODUCTS
  67. $installedIbexaProducts = array_keys(
  68. array_filter(
  69. array_replace(
  70. array_fill_keys(self::IBEXA_PRODUCTS, false),
  71. array_fill_keys($installedIbexaProducts, true)
  72. )
  73. )
  74. );
  75. // first element in the array is the package matching product edition
  76. return reset($installedIbexaProducts);
  77. }
  78. }
  79. class_alias(InstallCommandSubscriber::class, 'Ibexa\Platform\Installer\Event\Subscriber\InstallCommandSubscriber');