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

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