system/modules/pct_customelements/PCT/CustomElements/Core/Cache.php line 332

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  * 
  5.  * Copyright (C) 2005-2013 Leo Feyer
  6.  * 
  7.  * @copyright    Tim Gatzky 2015, Premium Contao Webworks, Premium Contao Themes
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package        pct_customelements
  10.  * @link        http://contao.org
  11.  */
  12.  
  13. /**
  14.  * Namespace
  15.  */
  16. namespace PCT\CustomElements\Core;
  17. /**
  18.  * Imports
  19.  */
  20. use PCT\CustomElements\Core\CustomElement as CustomElement;
  21. use PCT\CustomElements\Core\Attribute as Attribute;
  22. use PCT\CustomElements\Core\Group as Group;
  23. /**
  24.  * Cache
  25.  */
  26. class Cache
  27. {
  28.     /**
  29.      * Initialize the global cache array
  30.      */
  31.     public function __construct()
  32.     {
  33.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']))
  34.         {
  35.             $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'] = array();
  36.         }
  37.         foreach(array('CUSTOMELEMENT','CONTENTELEMENTSET','PLUGINS','CUSTOMCATALOG','GROUP','ATTRIBUTE','ACTIVE_CUSTOMCATALOGS') as $k )
  38.         {
  39.             if( !isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'][$k]) )
  40.             {
  41.                 $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'][$k] = array();
  42.             }
  43.         }
  44.     }
  45.     
  46.     
  47.     /**
  48.      * Add any data
  49.      * @param string
  50.      * @param mixed
  51.      */
  52.     public static function add($strKey,$varInput)
  53.     {
  54.         switch($strKey)
  55.         {
  56.             case 'customcatalog':
  57.                 static::addCustomCatalog($strKey,$varInput);
  58.                 break;
  59.             case 'attribute':
  60.                 static::addAttribute($strKey,$varInput);
  61.                 break;
  62.             default:
  63.                 $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'][$strKey] = $varInput;
  64.                 break;
  65.         }
  66.     }
  67.     
  68.     
  69.     /**
  70.      * Return data by key
  71.      * @param string
  72.      * @param mixed
  73.      */
  74.     public static function get($strKey)
  75.     {
  76.         if( !isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'][$strKey]) )
  77.         {
  78.             return null;
  79.         }
  80.         
  81.         if($GLOBALS['PCT_CUSTOMCATALOG']['SETTINGS']['bypassCache'])
  82.         {
  83.             return null;
  84.         }
  85.         
  86.         switch($strKey)
  87.         {
  88.             default:
  89.                 return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'][$strKey];
  90.                 break;
  91.         }
  92.         
  93.         return null;
  94.     }
  95.     
  96.     
  97.     /**
  98.      * Set the whole cache by an array
  99.      * @param array
  100.      */
  101.     public static function setData($arrData)
  102.     {
  103.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'] = $arrData;
  104.     }
  105.     
  106.     
  107.     /**
  108.      * Return the cached array data
  109.      * @return array
  110.      */
  111.     public static function getData()
  112.     {
  113.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']))
  114.         {
  115.             $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'] = array();
  116.         }
  117.         return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE'];
  118.     }
  119.     
  120.     
  121.     /**
  122.      * Add an attribute object to the cache by its alias or ID or any other key
  123.      * @param object    CE/CC Attribute object
  124.      * @param mixed        String identifiyer
  125.      */
  126.     public static function addAttribute($varKey,$objInput)
  127.     {
  128.         if($objInput === null)
  129.         {
  130.             return;
  131.         }
  132.         
  133.         $objSet null;
  134.         
  135.         // create a real object if a database result has been added
  136.         if(strlen(strpos(get_class($objInput), 'Database')) > 0)
  137.         {
  138.             $objSet = new Attribute($objInput->row());
  139.             $objSet $objSet->generate();
  140.             if(!$objSet)
  141.             {
  142.                 return;
  143.             }
  144.             $objSet->db_result $objInput;
  145.         }
  146.         else
  147.         {
  148.             $objSet $objInput;
  149.         }
  150.         
  151.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ATTRIBUTE'][$varKey] = $objSet;
  152.     }
  153.     
  154.     
  155.     /**
  156.      * Return an object by its identifiyer (e.g. alias or id or any other identifiyer used)
  157.      * @param mixed
  158.      * @return object     CE/CC Attribute object
  159.      */
  160.     public static function getAttribute($varKey)
  161.     {
  162.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ATTRIBUTE'][$varKey]))
  163.         {
  164.             return null;
  165.         }
  166.         return clone($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ATTRIBUTE'][$varKey]);
  167.     }
  168.     
  169.     
  170.     /**
  171.      * Add an group object to the cache by its alias or ID or any other key
  172.      * @param object    CE/CC Group object
  173.      * @param mixed        String identifiyer
  174.      */
  175.     public static function addGroup($varKey,$objInput)
  176.     {
  177.         $objSet null;
  178.         
  179.         // create a real object if a database result has been added
  180.         if(strlen(strpos(get_class($objInput), 'Database')) > 0)
  181.         {
  182.             $objSet = new Group($objInput->row());
  183.             $objSet $objSet->generate();
  184.             if(!$objSet)
  185.             {
  186.                 return;
  187.             }
  188.             $objSet->db_result $objInput;
  189.         }
  190.         else
  191.         {
  192.             $objSet $objInput;
  193.         }
  194.         
  195.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUP'][$varKey] = $objSet;
  196.     }
  197.     
  198.     
  199.     /**
  200.      * Return an object by its identifiyer (e.g. alias or id or any other identifiyer used)
  201.      * @param mixed
  202.      * @return object     CE/CC Group object
  203.      */
  204.     public static function getGroup($varKey)
  205.     {
  206.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUP'][$varKey]) || empty($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUP'][$varKey]) )
  207.         {
  208.             return null;
  209.         }
  210.         return clone($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUP'][$varKey]);
  211.     }
  212.     
  213.     
  214.     /**
  215.      * Add an array of groups by an identifiyer
  216.      * @param array|object
  217.      * @return object     array
  218.      */
  219.     public static function addGroupsByPid($varKey,$arrInput)
  220.     {
  221.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUPS'][$varKey] = $arrInput;
  222.     }
  223.         
  224.     /**
  225.      * Return a groups array by its identifiyer
  226.      * @param mixed
  227.      * @return array
  228.      */
  229.     public static function getGroupsByPid($varKey)
  230.     {
  231.         if(!$GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUPS'][$varKey])
  232.         {
  233.             return array();
  234.         }
  235.         return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['GROUPS'][$varKey];
  236.     }
  237.     
  238.     
  239.     /**
  240.      * Add a customelement object to the cache
  241.      * @param string
  242.      * @param object    CustomElement instance
  243.      */
  244.     public static function addCustomElement($varKey,$objInput)
  245.     {
  246.         $objSet null;
  247.         
  248.         // create a real object if a database result has been added
  249.         if(strlen(strpos(get_class($objInput), 'Database')) > 0)
  250.         {
  251.             $objSet = new CustomElement($objInput->row());
  252.             if(!$objSet)
  253.             {
  254.                 return;
  255.             }
  256.             $objSet->db_result $objInput;
  257.         }
  258.         else
  259.         {
  260.             $objSet $objInput;
  261.         }
  262.         
  263.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CUSTOMELEMENT'][$varKey] = $objSet;
  264.     }
  265.     
  266.     
  267.     /**
  268.      * Return a customcatalog object from the cache
  269.      * @param mixed
  270.      * @return object
  271.      */
  272.     public static function getCustomElement($varKey)
  273.     {
  274.         if( !isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CUSTOMELEMENT'][$varKey]) )
  275.         {
  276.             return null;
  277.         }
  278.         return clone($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CUSTOMELEMENT'][$varKey]);
  279.     }
  280.     
  281.     
  282.     /**
  283.      * Add an active plugin to the cache
  284.      * @param string
  285.      */
  286.     public static function addPlugin($varKey)
  287.     {
  288.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['PLUGINS'][$varKey] = $varKey;
  289.     }
  290.     
  291.     
  292.     /**
  293.      * Return active plugins from the cache
  294.      * @param string
  295.      * @return array
  296.      */
  297.     public static function getPlugins()
  298.     {
  299.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['PLUGINS']))
  300.         {
  301.             return null;
  302.         }
  303.         
  304.         return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['PLUGINS'];
  305.     }
  306.     
  307.     
  308.     /**
  309.      * Add a stack of plugins as array
  310.      * @param array
  311.      */
  312.     public static function addPlugins($arrData)
  313.     {
  314.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['PLUGINS'] = $arrData;
  315.     }
  316.     
  317.     
  318.     /**
  319.      * Add wizard data to cache
  320.      * @param integer
  321.      * @param string
  322.      * @param integer
  323.      * @param array||string
  324.      */
  325.     public static function addWizard($intPid,$strSource,$intGenericAttribut=0,$arrData)
  326.     {
  327.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['WIZARD'][$strSource][$intPid][$intGenericAttribut] = $arrData;
  328.     }
  329.     
  330.     
  331.     /**
  332.      * Return active plugins from the cache
  333.      * @param string
  334.      */
  335.     public static function getWizard($intPid,$strSource,$intGenericAttribute=0)
  336.     {
  337.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['WIZARD'][$strSource][$intPid][$intGenericAttribute]))
  338.         {
  339.             return array();
  340.         }
  341.         
  342.         return \Contao\StringUtil::deserialize($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['WIZARD'][$strSource][$intPid][$intGenericAttribute]);
  343.     }
  344.     
  345.     
  346.     /**
  347.      * Add a content element set data array to the cache
  348.      * @param string    Key name of the set file
  349.      * @param array        Data array
  350.      */
  351.     public static function addContentElementSet($strKey,$arrData)
  352.     {
  353.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET'][$strKey] = $arrData;
  354.     }
  355.     
  356.     
  357.     /**
  358.      * Add a content element set data array to the cache
  359.      * @return array
  360.      */
  361.     public static function getContentElementSet($strKey)
  362.     {
  363.         if(!isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET'][$strKey]))
  364.         {
  365.             return array();
  366.         }
  367.         
  368.         return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET'][$strKey];
  369.     }
  370.     
  371.     
  372.     /**
  373.      * Add a whole stack of content element set data 
  374.      * @param array
  375.      */
  376.     public static function addContentElementSets($arrInput)
  377.     {
  378.         if( !isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET']) )
  379.         {
  380.             $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET'] = null;
  381.         }
  382.         $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET'] = $arrInput;
  383.     }
  384.     
  385.     
  386.     /**
  387.      * Return all content element sets
  388.      * @return array||null
  389.      */
  390.     public static function getContentElementSets()
  391.     {
  392.         if( !isset($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET']) )
  393.         {
  394.             return null;
  395.         }
  396.         return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CONTENTELEMENTSET'];
  397.     }
  398.     
  399.     
  400.     /**
  401.      * @shortcut to getCustomElement
  402.      */
  403.     public static function findCustomElementByAlias($strKey)
  404.     {
  405.         return static::getCustomElement($strKey);
  406.     }
  407.     
  408.     
  409.     /**
  410.      * @shortcut to getCustomElement
  411.      */
  412.     public static function findCustomElementById($intKey)
  413.     {
  414.         return static::getCustomCatalog($intKey);
  415.     }
  416.     
  417.     
  418.     /**
  419.      * Return an customelement object by an attribute object
  420.      * @param object    Attribute
  421.      * @return object
  422.      */
  423.     public static function findCustomElementByGroup($varGroup)
  424.     {
  425.         if(!$GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CUSTOMELEMENT']['_byGroup_'][$varGroup])
  426.         {
  427.             return null;
  428.         }
  429.         return $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['CUSTOMELEMENT']['_byGroup_'][$varGroup];
  430.     }
  431. }