vendor/contao/installation-bundle/src/EventListener/InitializeApplicationListener.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\InstallationBundle\EventListener;
  11. use Contao\InstallationBundle\Event\InitializeApplicationEvent;
  12. use Symfony\Bundle\FrameworkBundle\Command\AssetsInstallCommand;
  13. use Symfony\Bundle\FrameworkBundle\Console\Application;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\ArgvInput;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\BufferedOutput;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  20. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  21. use Webmozart\PathUtil\Path;
  22. /**
  23.  * @internal
  24.  */
  25. class InitializeApplicationListener implements ContainerAwareInterface
  26. {
  27.     use ContainerAwareTrait;
  28.     public function __invoke(InitializeApplicationEvent $event): void
  29.     {
  30.         $this->installAssets($event);
  31.         $this->installContao($event);
  32.         $this->createSymlinks($event);
  33.     }
  34.     private function installAssets(InitializeApplicationEvent $event): void
  35.     {
  36.         $webDir $this->container->getParameter('contao.web_dir');
  37.         if (file_exists(Path::join($webDir'bundles/contaocore/core.js'))) {
  38.             return;
  39.         }
  40.         $application = new Application($this->container->get('kernel'));
  41.         $command = new AssetsInstallCommand(
  42.             $this->container->get('filesystem'),
  43.             $this->container->getParameter('kernel.project_dir')
  44.         );
  45.         $command->setApplication($application);
  46.         $input = new ArgvInput(['bin/console''assets:install'$webDir'--symlink''--relative']);
  47.         if (null === ($output $this->runCommand($command$input))) {
  48.             return;
  49.         }
  50.         $event->setOutput($output);
  51.     }
  52.     private function installContao(InitializeApplicationEvent $event): void
  53.     {
  54.         $projectDir $this->container->getParameter('kernel.project_dir');
  55.         if (is_dir(Path::join($projectDir'system/config'))) {
  56.             return;
  57.         }
  58.         $webDir $this->container->getParameter('contao.web_dir');
  59.         $command $this->container->get('contao.command.install');
  60.         $input = new ArgvInput(['contao:install'Path::makeRelative($webDir$projectDir)]);
  61.         if (null === ($output $this->runCommand($command$input))) {
  62.             return;
  63.         }
  64.         $event->setOutput($output);
  65.     }
  66.     private function createSymlinks(InitializeApplicationEvent $event): void
  67.     {
  68.         $webDir $this->container->getParameter('contao.web_dir');
  69.         if (is_link(Path::join($webDir'system/themes'))) {
  70.             return;
  71.         }
  72.         $projectDir $this->container->getParameter('kernel.project_dir');
  73.         $command $this->container->get('contao.command.symlinks');
  74.         $input = new ArgvInput(['contao:symlinks'Path::makeRelative($webDir$projectDir)]);
  75.         if (null === ($output $this->runCommand($command$input))) {
  76.             return;
  77.         }
  78.         $event->setOutput($output);
  79.     }
  80.     /**
  81.      * Runs a command and returns the error (if any).
  82.      */
  83.     private function runCommand(Command $commandInputInterface $input): ?string
  84.     {
  85.         if ($command instanceof ContainerAwareInterface) {
  86.             $command->setContainer($this->container);
  87.         }
  88.         $output = new BufferedOutput(OutputInterface::VERBOSITY_NORMALtrue);
  89.         $status $command->run($input$output);
  90.         if ($status 0) {
  91.             return $output->fetch();
  92.         }
  93.         return null;
  94.     }
  95. }