vendor/pimcore/pimcore/models/Site.php line 144

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @category   Pimcore
  12.  * @package    Site
  13.  *
  14.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  15.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  16.  */
  17. namespace Pimcore\Model;
  18. use Pimcore\Cache\Runtime;
  19. use Pimcore\Logger;
  20. /**
  21.  * @method \Pimcore\Model\Site\Dao getDao()
  22.  * @method delete()
  23.  */
  24. class Site extends AbstractModel
  25. {
  26.     /**
  27.      * @var Site
  28.      */
  29.     protected static $currentSite;
  30.     /**
  31.      * @var int
  32.      */
  33.     public $id;
  34.     /**
  35.      * @var array
  36.      */
  37.     public $domains;
  38.     /**
  39.      * Contains the ID to the Root-Document
  40.      *
  41.      * @var int
  42.      */
  43.     public $rootId;
  44.     /**
  45.      * @var Document\Page
  46.      */
  47.     public $rootDocument;
  48.     /**
  49.      * @var string
  50.      */
  51.     public $rootPath;
  52.     /**
  53.      * @var string
  54.      */
  55.     public $mainDomain '';
  56.     /**
  57.      * @var string
  58.      */
  59.     public $errorDocument '';
  60.     /**
  61.      * @var bool
  62.      */
  63.     public $redirectToMainDomain false;
  64.     /**
  65.      * @var int
  66.      */
  67.     public $creationDate;
  68.     /**
  69.      * @var int
  70.      */
  71.     public $modificationDate;
  72.     /**
  73.      * @param int $id
  74.      *
  75.      * @return Site|null
  76.      */
  77.     public static function getById($id)
  78.     {
  79.         $cacheKey 'site_id_'$id;
  80.         if (Runtime::isRegistered($cacheKey)) {
  81.             $site Runtime::get($cacheKey);
  82.         } elseif (!$site = \Pimcore\Cache::load($cacheKey)) {
  83.             try {
  84.                 $site = new self();
  85.                 $site->getDao()->getById(intval($id));
  86.             } catch (\Exception $e) {
  87.                 $site 'failed';
  88.             }
  89.             \Pimcore\Cache::save($site$cacheKey, ['system''site'], null999);
  90.         }
  91.         if ($site === 'failed' || !$site) {
  92.             $site null;
  93.         }
  94.         Runtime::set($cacheKey$site);
  95.         return $site;
  96.     }
  97.     /**
  98.      * @param int $id
  99.      *
  100.      * @return Site|null
  101.      */
  102.     public static function getByRootId($id)
  103.     {
  104.         try {
  105.             $site = new self();
  106.             $site->getDao()->getByRootId(intval($id));
  107.             return $site;
  108.         } catch (\Exception $e) {
  109.             return null;
  110.         }
  111.     }
  112.     /**
  113.      * @param $domain
  114.      *
  115.      * @return Site|null
  116.      */
  117.     public static function getByDomain($domain)
  118.     {
  119.         // cached because this is called in the route
  120.         $cacheKey 'site_domain_'md5($domain);
  121.         if (Runtime::isRegistered($cacheKey)) {
  122.             $site Runtime::get($cacheKey);
  123.         } elseif (!$site = \Pimcore\Cache::load($cacheKey)) {
  124.             try {
  125.                 $site = new self();
  126.                 $site->getDao()->getByDomain($domain);
  127.             } catch (\Exception $e) {
  128.                 $site 'failed';
  129.             }
  130.             \Pimcore\Cache::save($site$cacheKey, ['system''site'], null999);
  131.         }
  132.         if ($site === 'failed' || !$site) {
  133.             $site null;
  134.         }
  135.         Runtime::set($cacheKey$site);
  136.         return $site;
  137.     }
  138.     /**
  139.      * @param $mixed
  140.      *
  141.      * @return Site|null
  142.      */
  143.     public static function getBy($mixed)
  144.     {
  145.         $site null;
  146.         if (is_numeric($mixed)) {
  147.             $site self::getById($mixed);
  148.         } elseif (is_string($mixed)) {
  149.             $site self::getByDomain($mixed);
  150.         } elseif ($mixed instanceof Site) {
  151.             $site $mixed;
  152.         }
  153.         return $site;
  154.     }
  155.     /**
  156.      * @param array $data
  157.      *
  158.      * @return Site
  159.      */
  160.     public static function create($data)
  161.     {
  162.         $site = new self();
  163.         $site->setValues($data);
  164.         return $site;
  165.     }
  166.     /**
  167.      * returns true if the current process/request is inside a site
  168.      *
  169.      * @static
  170.      *
  171.      * @return bool
  172.      */
  173.     public static function isSiteRequest()
  174.     {
  175.         if (null !== self::$currentSite) {
  176.             return true;
  177.         }
  178.         return false;
  179.     }
  180.     /**
  181.      * @return Site
  182.      *
  183.      * @throws \Exception
  184.      */
  185.     public static function getCurrentSite()
  186.     {
  187.         if (null !== self::$currentSite) {
  188.             return self::$currentSite;
  189.         } else {
  190.             throw new \Exception('This request/process is not inside a subsite');
  191.         }
  192.     }
  193.     /**
  194.      * Register the current site
  195.      *
  196.      * @param Site $site
  197.      */
  198.     public static function setCurrentSite(Site $site)
  199.     {
  200.         self::$currentSite $site;
  201.     }
  202.     /**
  203.      * @return int
  204.      */
  205.     public function getId()
  206.     {
  207.         return $this->id;
  208.     }
  209.     /**
  210.      * @return array
  211.      */
  212.     public function getDomains()
  213.     {
  214.         return $this->domains;
  215.     }
  216.     /**
  217.      * @return int
  218.      */
  219.     public function getRootId()
  220.     {
  221.         return $this->rootId;
  222.     }
  223.     /**
  224.      * @return Document\Page
  225.      */
  226.     public function getRootDocument()
  227.     {
  228.         return $this->rootDocument;
  229.     }
  230.     /**
  231.      * @param int $id
  232.      *
  233.      * @return $this
  234.      */
  235.     public function setId($id)
  236.     {
  237.         $this->id = (int) $id;
  238.         return $this;
  239.     }
  240.     /**
  241.      * @param mixed $domains
  242.      *
  243.      * @return $this
  244.      */
  245.     public function setDomains($domains)
  246.     {
  247.         if (is_string($domains)) {
  248.             $domains = \Pimcore\Tool\Serialize::unserialize($domains);
  249.         }
  250.         $this->domains $domains;
  251.         return $this;
  252.     }
  253.     /**
  254.      * @param int $rootId
  255.      *
  256.      * @return $this
  257.      */
  258.     public function setRootId($rootId)
  259.     {
  260.         $this->rootId = (int) $rootId;
  261.         $rd Document::getById($this->rootId);
  262.         $this->setRootDocument($rd);
  263.         return $this;
  264.     }
  265.     /**
  266.      * @param Document\Page $rootDocument
  267.      *
  268.      * @return $this
  269.      */
  270.     public function setRootDocument($rootDocument)
  271.     {
  272.         $this->rootDocument $rootDocument;
  273.         return $this;
  274.     }
  275.     /**
  276.      * @param $path
  277.      *
  278.      * @return $this
  279.      */
  280.     public function setRootPath($path)
  281.     {
  282.         $this->rootPath $path;
  283.         return $this;
  284.     }
  285.     /**
  286.      * @return string
  287.      */
  288.     public function getRootPath()
  289.     {
  290.         if (!$this->rootPath && $this->getRootDocument()) {
  291.             return $this->getRootDocument()->getRealFullPath();
  292.         }
  293.         return $this->rootPath;
  294.     }
  295.     /**
  296.      * @param string $errorDocument
  297.      */
  298.     public function setErrorDocument($errorDocument)
  299.     {
  300.         $this->errorDocument $errorDocument;
  301.     }
  302.     /**
  303.      * @return string
  304.      */
  305.     public function getErrorDocument()
  306.     {
  307.         return $this->errorDocument;
  308.     }
  309.     /**
  310.      * @param string $mainDomain
  311.      */
  312.     public function setMainDomain($mainDomain)
  313.     {
  314.         $this->mainDomain $mainDomain;
  315.     }
  316.     /**
  317.      * @return string
  318.      */
  319.     public function getMainDomain()
  320.     {
  321.         return $this->mainDomain;
  322.     }
  323.     /**
  324.      * @param bool $redirectToMainDomain
  325.      */
  326.     public function setRedirectToMainDomain($redirectToMainDomain)
  327.     {
  328.         $this->redirectToMainDomain = (bool) $redirectToMainDomain;
  329.     }
  330.     /**
  331.      * @return bool
  332.      */
  333.     public function getRedirectToMainDomain()
  334.     {
  335.         return $this->redirectToMainDomain;
  336.     }
  337.     public function clearDependentCache()
  338.     {
  339.         // this is mostly called in Site\Dao not here
  340.         try {
  341.             \Pimcore\Cache::clearTag('site');
  342.         } catch (\Exception $e) {
  343.             Logger::crit($e);
  344.         }
  345.     }
  346.     /**
  347.      * @param $modificationDate
  348.      *
  349.      * @return $this
  350.      */
  351.     public function setModificationDate($modificationDate)
  352.     {
  353.         $this->modificationDate = (int) $modificationDate;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @return int
  358.      */
  359.     public function getModificationDate()
  360.     {
  361.         return $this->modificationDate;
  362.     }
  363.     /**
  364.      * @param $creationDate
  365.      *
  366.      * @return $this
  367.      */
  368.     public function setCreationDate($creationDate)
  369.     {
  370.         $this->creationDate = (int) $creationDate;
  371.         return $this;
  372.     }
  373.     /**
  374.      * @return int
  375.      */
  376.     public function getCreationDate()
  377.     {
  378.         return $this->creationDate;
  379.     }
  380. }