Skip to content

Instantly share code, notes, and snippets.

@ilyautkin
Last active March 2, 2022 06:41
Show Gist options
  • Save ilyautkin/b1a754eaf56b800b17071dd4c5459417 to your computer and use it in GitHub Desktop.
Save ilyautkin/b1a754eaf56b800b17071dd4c5459417 to your computer and use it in GitHub Desktop.
<?php
$url = 'https://fonts.googleapis.com/css2?family=Lato:wght@100;300;400;700&display=swap';
$opts = [
'http' => [
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
]
];
$context = stream_context_create($opts);
$contents = file_get_contents($url, false, $context);
$rules = explode('}', $contents);
foreach ($rules as &$rule) {
if (!empty(trim($rule))) {
$open = explode('url(', $rule);
if (count($open) == 2) {
$close = explode(')', $open[1]);
$link = array_shift($close);
$file = str_replace('https://fonts.gstatic.com/', '/assets/fonts/', $link);
$modx->cacheManager->writeFile(MODX_BASE_PATH . $file, file_get_contents($link));
$rule = $open[0] . 'url(' . $file . ')' . implode(')', $close);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Something wrong with rule:' . PHP_EOL . $rule);
}
}
}
$modx->cacheManager->writeFile(MODX_BASE_PATH . 'assets/fonts/fonts.css', implode('}', $rules));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment