Source of file ControllerBased.php
Size: 3,584 Bytes - Last Modified: 2021-12-24T06:50:57+00:00
/var/www/docs.ssmods.com/process/src/src/KeyCreators/ControllerBased.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 | <?php namespace Heyday\CacheInclude\KeyCreators; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Security\Security; use SilverStripe\View\SSViewer; class ControllerBased implements KeyCreatorInterface, KeyInformationProviderInterface { /** * @var Controller */ protected $controller; /** * @var Config */ protected $config; /** * @var string */ protected $environmentType; /** * @var string */ protected $themes; /** * @var int */ protected $memberID; /** * @param Controller|void $controller * @throws \Exception */ public function __construct(Controller $controller = null) { if (!$controller && !Controller::has_curr()) { throw new \Exception("Controller based key creators must have a current controller"); } $this->controller = $controller ?: Controller::curr(); $this->config = Config::inst(); $this->environmentType = Director::get_environment_type(); $this->themes = $this->config->get(SSViewer::class, 'themes'); $this->memberID = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0; } /** * @param $name * @param $config * @return mixed */ public function getKey($name, $config) { $request = $this->controller->getRequest(); $keyParts = [ $this->environmentType, md5(json_encode($this->themes)), $request->getScheme() ]; if ($request->isAjax()) { $keyParts[] = 'ajax'; } // If member context matters get the members id if (isset($config['member']) && $config['member'] && $this->memberID) { $keyParts[] = 'Members'; if ($config['member'] !== 'any') { $keyParts[] = $this->memberID; } } // Determine the context if (isset($config['context'])) { switch ($config['context']) { case 'no': break; case 'page': $keyParts[] = md5($request->getURL()); break; case 'full': $keyParts[] = md5($request->getURL(true)); break; } } if (isset($config['versions'])) { $keyParts[] = mt_rand(1, (int) $config['versions']); } $keyParts[] = $name; return $keyParts; } /** * @return array */ public function getKeyInformation() { $request = $this->controller->getRequest(); return [ 'url' => sprintf('/%s/', trim($request->getURL(true), '/')) ]; } /** * @param Config $config */ public function setConfig($config) { $this->config = $config; } /** * @param Controller $controller */ public function setController($controller) { $this->controller = $controller; } /** * @param string $environmentType */ public function setEnvironmentType($environmentType) { $this->environmentType = $environmentType; } /** * @param int $memberID */ public function setMemberID($memberID) { $this->memberID = $memberID; } /** * @param array $themes */ public function setThemes(array $themes) { $this->themes = $themes; } } |