function TestSiteInstallCommand::execute

Same name and namespace in other branches
  1. 9 core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php \Drupal\TestSite\Commands\TestSiteInstallCommand::execute()
  2. 8.9.x core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php \Drupal\TestSite\Commands\TestSiteInstallCommand::execute()
  3. 10 core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php \Drupal\TestSite\Commands\TestSiteInstallCommand::execute()

File

core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php, line 121

Class

TestSiteInstallCommand
Command to create a test Drupal site.

Namespace

Drupal\TestSite\Commands

Code

protected function execute(InputInterface $input, OutputInterface $output) : int {
    // Determines and validates the setup class prior to installing a database
    // to avoid creating unnecessary sites.
    $root = dirname(__DIR__, 5);
    chdir($root);
    $class_name = $this->getSetupClass($input->getOption('setup-file'));
    // Ensure we can install a site in the sites/simpletest directory.
    $this->ensureDirectory($root);
    $db_url = $input->getOption('db-url');
    $base_url = $input->getOption('base-url');
    putenv("SIMPLETEST_DB={$db_url}");
    putenv("SIMPLETEST_BASE_URL={$base_url}");
    // Manage site fixture.
    $this->setup($input->getOption('install-profile'), $class_name, $input->getOption('langcode'));
    // Make sure there is an entry in sites.php for the new site.
    $fs = new Filesystem();
    if (!$fs->exists($root . '/sites/sites.php')) {
        $fs->copy($root . '/sites/example.sites.php', $root . '/sites/sites.php');
    }
    $parsed = parse_url($base_url);
    $port = $parsed['port'] ?? 80;
    $host = $parsed['host'] ?? 'localhost';
    // Remove 'sites/' from the beginning of the path.
    $site_path = substr($this->siteDirectory, 6);
    $fs->appendToFile($root . '/sites/sites.php', "\$sites['{$port}.{$host}'] = '{$site_path}';");
    $user_agent = drupal_generate_test_ua($this->databasePrefix);
    if ($input->getOption('json')) {
        $output->writeln(json_encode([
            'db_prefix' => $this->databasePrefix,
            'user_agent' => $user_agent,
            'site_path' => $this->siteDirectory,
        ]));
    }
    else {
        $output->writeln('<info>Successfully installed a test site</info>');
        $io = new SymfonyStyle($input, $output);
        $io->table([], [
            [
                'Database prefix',
                $this->databasePrefix,
            ],
            [
                'User agent',
                $user_agent,
            ],
            [
                'Site path',
                $this->siteDirectory,
            ],
        ]);
    }
    return 0;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.