2021-05-22 00:15:34 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core\Action;
|
|
|
|
|
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Shlinkio\Shlink\Core\Crawling\CrawlingHelperInterface;
|
2024-07-05 01:52:41 -05:00
|
|
|
use Shlinkio\Shlink\Core\Options\RobotsOptions;
|
2021-05-22 00:15:34 -05:00
|
|
|
|
|
|
|
use function sprintf;
|
|
|
|
|
|
|
|
use const PHP_EOL;
|
|
|
|
|
2024-04-21 10:09:20 -05:00
|
|
|
readonly class RobotsAction implements RequestHandlerInterface, StatusCodeInterface
|
2021-05-22 00:15:34 -05:00
|
|
|
{
|
2024-07-05 01:52:41 -05:00
|
|
|
public function __construct(private CrawlingHelperInterface $crawlingHelper, private RobotsOptions $robotsOptions)
|
2021-05-22 00:15:34 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
|
|
|
{
|
2021-07-17 13:58:24 -05:00
|
|
|
// @phpstan-ignore-next-line The "Response" phpdoc is wrong
|
2021-05-22 00:15:34 -05:00
|
|
|
return new Response(self::STATUS_OK, ['Content-type' => 'text/plain'], $this->buildRobots());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRobots(): iterable
|
|
|
|
{
|
|
|
|
yield <<<ROBOTS
|
|
|
|
# For more information about the robots.txt standard, see:
|
|
|
|
# https://www.robotstxt.org/orig.html
|
|
|
|
|
|
|
|
|
|
|
|
ROBOTS;
|
|
|
|
|
2024-07-05 01:52:41 -05:00
|
|
|
$userAgents = $this->robotsOptions->hasUserAgents() ? $this->robotsOptions->userAgents : ['*'];
|
|
|
|
foreach ($userAgents as $userAgent) {
|
|
|
|
yield sprintf('User-agent: %s%s', $userAgent, PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->robotsOptions->allowAllShortUrls) {
|
2024-04-21 10:09:20 -05:00
|
|
|
// Disallow rest URLs, but allow all short codes
|
|
|
|
yield 'Disallow: /rest/';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-22 00:15:34 -05:00
|
|
|
$shortCodes = $this->crawlingHelper->listCrawlableShortCodes();
|
|
|
|
foreach ($shortCodes as $shortCode) {
|
|
|
|
yield sprintf('Allow: /%s%s', $shortCode, PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
yield 'Disallow: /';
|
|
|
|
}
|
|
|
|
}
|