CacheTagsHash.php in Fastly 8.3
File
src/CacheTagsHash.php
View source
<?php
namespace Drupal\fastly;
use Drupal\Component\Utility\Random;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
class CacheTagsHash implements CacheTagsHashInterface {
protected $configFactory;
protected $config;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
$this->config = $config_factory
->get('fastly.settings');
}
public function cacheTagsToHashes(array $cache_tags) {
$hashes = [];
$siteId = $this
->getSiteId();
foreach ($cache_tags as $cache_tag) {
$cache_tag = $siteId ? $siteId . ':' . $cache_tag : $cache_tag;
$hashes[] = $this
->hashInput($cache_tag);
}
return $hashes;
}
public function hashInput($input) {
$cache_tags_length = getenv('FASTLY_CACHE_TAG_HASH_LENGTH') ?: $this->config
->get('cache_tag_hash_length');
$cache_tags_length = $cache_tags_length ?: self::CACHE_TAG_HASH_LENGTH;
return substr(base64_encode(md5($input, true)), 0, $cache_tags_length);
}
public function getSiteId() {
$siteId = getenv('FASTLY_SITE_ID') ?: $this->config
->get('site_id');
if (!$siteId) {
$random = new Random();
$siteId = $random
->name();
$siteId = mb_strtolower($siteId);
if ($this->configFactory
->get('fastly.settings')) {
$config = $this->configFactory
->getEditable('fastly.settings');
$config
->set('site_id', $siteId)
->save(TRUE);
}
}
return $siteId;
}
}