addArgument('adresse', InputArgument::OPTIONAL, 'Die neue Adresse mit Schema, z. B. https://www.meine-firma.de. Ohne Angabe wird die aktuelle gezeigt.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$neu = trim((string) $input->getArgument('adresse'));
if ('' === $neu) {
$io->writeln(sprintf('Die Website ist eingetragen als %s.', '' === $this->siteOrigin ? '(leer)' : $this->siteOrigin));
$io->writeln('Ändern: php bin/console cms:site:origin https://www.meine-firma.de');
return Command::SUCCESS;
}
try {
$neu = self::normalize($neu);
} catch (\InvalidArgumentException $e) {
$io->error($e->getMessage());
return Command::FAILURE;
}
try {
Environment::write($this->projectDir, ['SITE_ORIGIN' => $neu]);
} catch (\RuntimeException $e) {
$io->error($e->getMessage());
return Command::FAILURE;
}
$io->success(sprintf('Die Website ist jetzt eingetragen als %s.', $neu));
$io->writeln('Canonical, Sitemap, Feed und strukturierte Daten nennen diese Adresse; die Schreibweise '.(str_starts_with(parse_url($neu, PHP_URL_HOST) ?: '', 'www.') ? 'ohne' : 'mit').' www leitet dorthin um.');
$io->writeln('Falls die Website den alten Wert noch kennt: php bin/console cache:clear --env=prod');
return Command::SUCCESS;
}
/**
* Schema und Host, kleingeschrieben, ohne Pfad, Abfrage oder abschließenden Schrägstrich.
*
* @throws \InvalidArgumentException
*/
public static function normalize(string $adresse): string
{
$adresse = rtrim(trim($adresse), '/');
$teile = parse_url($adresse);
$schema = strtolower((string) ($teile['scheme'] ?? ''));
if (!\in_array($schema, ['http', 'https'], true)) {
throw new \InvalidArgumentException('Die Adresse beginnt mit https:// – zum Beispiel https://www.meine-firma.de');
}
$host = strtolower((string) ($teile['host'] ?? ''));
if ('' === $host || false === filter_var($schema.'://'.$host, FILTER_VALIDATE_URL) || isset($teile['user'], $teile['pass'])) {
throw new \InvalidArgumentException('Diese Adresse ist keine gültige URL.');
}
if (isset($teile['path']) && '' !== $teile['path'] || isset($teile['query']) || isset($teile['fragment'])) {
throw new \InvalidArgumentException('Nur Schema und Domain, ohne Pfad: https://www.meine-firma.de statt https://www.meine-firma.de/start');
}
return $schema.'://'.$host.(isset($teile['port']) ? ':'.$teile['port'] : '');
}
}