Skip to content

Instantly share code, notes, and snippets.

@ahilles107
Last active August 10, 2017 10:29
Show Gist options
  • Save ahilles107/9495a02d39498bc5e2f3f4401792f8ae to your computer and use it in GitHub Desktop.
Save ahilles107/9495a02d39498bc5e2f3f4401792f8ae to your computer and use it in GitHub Desktop.
<?php
namespace SWP\Bundle\CoreBundle\Twig\Cache;
use SWP\Bundle\MultiTenancyBundle\Context\TenantContext;
class TenantAwareCache extends \Twig_Cache_Filesystem
{
private $directory;
private $tenantContext;
/**
* TenantAwareCache constructor.
*
* @param string $directory
* @param TenantContext $tenantContext
*/
public function __construct(string $directory, TenantContext $tenantContext)
{
$this->directory = $directory;
$this->tenantContext = $tenantContext;
parent::__construct($this->directory);
}
/**
* {@inheritdoc}
*/
public function generateKey($name, $className)
{
if (null === $this->tenantContext->getTenant()) {
return parent::generateKey($name, $className);
}
$hash = hash('sha256', $className);
return $this->generateCacheDir().$hash.'.php';
}
/**
* @return string
*/
private function generateCacheDir()
{
$tenantCode = $this->tenantContext->getTenant()->getCode();
$themeName = str_replace('/', '_', $this->tenantContext->getTenant()->getThemeName());
return $this->directory.'/'.$tenantCode.'/themes/'.$themeName.'/';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment