expressioncache.class.inc.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // Copyright (c) 2010-2017 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. //
  19. class ExpressionCache
  20. {
  21. static private $aCache = array();
  22. static public function GetCachedExpression($sClass, $sAttCode)
  23. {
  24. // read current cache
  25. @include_once (static::GetCacheFileName());
  26. $oExpr = null;
  27. $sKey = static::GetKey($sClass, $sAttCode);
  28. if (array_key_exists($sKey, static::$aCache))
  29. {
  30. $oExpr = static::$aCache[$sKey];
  31. }
  32. else
  33. {
  34. if (class_exists('ExpressionCacheData'))
  35. {
  36. if (array_key_exists($sKey, ExpressionCacheData::$aCache))
  37. {
  38. $sVal = ExpressionCacheData::$aCache[$sKey];
  39. $oExpr = unserialize($sVal);
  40. static::$aCache[$sKey] = $oExpr;
  41. }
  42. }
  43. }
  44. return $oExpr;
  45. }
  46. static public function Warmup()
  47. {
  48. $sFilePath = static::GetCacheFileName();
  49. if (!is_file($sFilePath))
  50. {
  51. $content = <<<EOF
  52. <?php
  53. // Copyright (c) 2010-2017 Combodo SARL
  54. // Generated Expression Cache file
  55. class ExpressionCacheData
  56. {
  57. static \$aCache = array(
  58. EOF;
  59. foreach(MetaModel::GetClasses() as $sClass)
  60. {
  61. $content .= static::GetSerializedExpression($sClass, 'friendlyname');
  62. if (MetaModel::IsObsoletable($sClass))
  63. {
  64. $content .= static::GetSerializedExpression($sClass, 'obsolescence_flag');
  65. }
  66. }
  67. $content .= <<<EOF
  68. );
  69. }
  70. EOF;
  71. file_put_contents($sFilePath, $content);
  72. }
  73. }
  74. static private function GetSerializedExpression($sClass, $sAttCode)
  75. {
  76. $sKey = static::GetKey($sClass, $sAttCode);
  77. $oExpr = DBObjectSearch::GetPolymorphicExpression($sClass, $sAttCode);
  78. return "'".$sKey."' => '".serialize($oExpr)."',\n";
  79. }
  80. /**
  81. * @param $sClass
  82. * @param $sAttCode
  83. * @return string
  84. */
  85. static private function GetKey($sClass, $sAttCode)
  86. {
  87. return $sClass.'::'.$sAttCode;
  88. }
  89. public static function GetCacheFileName()
  90. {
  91. return utils::GetCachePath().'expressioncache.php';
  92. }
  93. }