BxDolPreloader.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. bx_import('BxDolLanguages');
  10. define('BX_PRELOADER_TYPE_CSS', 'css_system');
  11. define('BX_PRELOADER_TYPE_JS', 'js_system');
  12. define('BX_PRELOADER_TYPE_JS_OPTION', 'js_option');
  13. define('BX_PRELOADER_TYPE_JS_TRANSLATION', 'js_translation');
  14. define('BX_PRELOADER_TYPE_JS_IMAGE', 'js_image');
  15. class BxDolPreloader extends BxDolFactory implements iBxDolSingleton
  16. {
  17. protected $_oDb;
  18. protected $_aEntries;
  19. protected $_aTypes;
  20. protected $_aMarkers;
  21. protected function __construct()
  22. {
  23. parent::__construct();
  24. $this->_oDb = new BxDolPreloaderQuery();
  25. $this->_aEntries = $this->_oDb->getEntries();
  26. $this->_aTypes = [
  27. BX_PRELOADER_TYPE_CSS => ['get' => 'getCssUrl'],
  28. BX_PRELOADER_TYPE_JS => ['get' => 'getJsUrl'],
  29. BX_PRELOADER_TYPE_JS_OPTION => [],
  30. BX_PRELOADER_TYPE_JS_TRANSLATION => [],
  31. BX_PRELOADER_TYPE_JS_IMAGE => []
  32. ];
  33. foreach($this->_aTypes as $sType => $sValue)
  34. $this->_aTypes[$sType]['add'] = 'add' . bx_gen_method_name($sType);
  35. $this->_aMarkers = array(
  36. 'dir_plugins_public' => BX_DIRECTORY_PATH_PLUGINS_PUBLIC,
  37. 'dir_plugins_modules' => BX_DIRECTORY_PATH_MODULES,
  38. );
  39. }
  40. public function __clone()
  41. {
  42. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  43. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  44. }
  45. public static function getInstance()
  46. {
  47. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  48. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolPreloader();
  49. return $GLOBALS['bxDolClasses'][__CLASS__];
  50. }
  51. public function perform($oTemplateSystem)
  52. {
  53. $aTypesAvail = array_keys($this->_aTypes);
  54. foreach($this->_aEntries as $aEntry) {
  55. $sType = $aEntry['type'];
  56. if(!in_array($sType, $aTypesAvail))
  57. continue;
  58. if(BxDolService::isSerializedService($aEntry['content']))
  59. $aEntry['content'] = BxDolService::callSerialized($aEntry['content']);
  60. $sContent = bx_replace_markers($aEntry['content'], $this->_aMarkers);
  61. if(empty($sContent))
  62. continue;
  63. $oTemplateSystem->{$this->_aTypes[$sType]['add']}($sContent);
  64. }
  65. }
  66. public function getSystemAssets($oTemplateSystem)
  67. {
  68. $aResults = [];
  69. $aEntries = $this->_oDb->getEntriesSystem();
  70. foreach($aEntries as $aEntry) {
  71. $sType = $aEntry['type'];
  72. if(BxDolService::isSerializedService($aEntry['content']))
  73. $aEntry['content'] = BxDolService::callSerialized($aEntry['content']);
  74. $sContent = bx_replace_markers($aEntry['content'], $this->_aMarkers);
  75. if(empty($sContent))
  76. continue;
  77. $sEntry = $oTemplateSystem->{$this->_aTypes[$sType]['get']}($sContent);
  78. if(empty($sEntry))
  79. $sEntry = $sContent;
  80. $aResults[] = str_replace([BX_DOL_URL_ROOT, BX_DIRECTORY_PATH_ROOT], '/', $sEntry);
  81. }
  82. return $aResults;
  83. }
  84. }
  85. /** @} */