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

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