You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
983 B

<?php
namespace App\Services\Crawl;
use App\Models\CrawlSource;
use Illuminate\Support\Str;
class CrawlSourceResolver
{
public function resolve(string $requestUrl, string $targetType): ?CrawlSource
{
$host = strtolower((string) parse_url($requestUrl, PHP_URL_HOST));
if ($host === '') {
return null;
}
$sources = CrawlSource::query()
->where('status', 1)
->where('target_type', $targetType)
->orderBy('sort')
->get();
foreach ($sources as $source) {
foreach ($source->match_domains ?? [] as $domain) {
$domain = strtolower((string) $domain);
if ($domain === '*' || $domain === 'any') {
return $source;
}
if ($host === $domain || Str::endsWith($host, '.'.$domain)) {
return $source;
}
}
}
return null;
}
}