Last active
August 10, 2017 10:29
-
-
Save ahilles107/9495a02d39498bc5e2f3f4401792f8ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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