Exemple #1
0
 /** Prepare js asset to optimize */
 private function prepareJs()
 {
     $cnt = 0;
     $arAdditional = array();
     foreach ($this->js as $js => $set) {
         /** @var  $assetTID - get first target where added asset */
         $assetTID = $set['ADDITIONAL'] ? 'TEMPLATE' : $set['TARGET'][0];
         $jsInfo = array('PATH' => $js, 'FULL_PATH' => false, 'FILE_PATH' => false, 'SKIP' => false, 'TARGET' => $assetTID, 'EXTERNAL' => \CMain::IsExternalLink($js), 'BODY' => false, 'ADDITIONAL' => $set['ADDITIONAL']);
         if ($jsInfo['EXTERNAL']) {
             if ($set['ADDITIONAL']) {
                 $tmpKey = 'TEMPLATE';
                 $tmpPrefix = 'template';
             } else {
                 $tmpKey = 'KERNEL';
                 $tmpPrefix = 'kernel';
             }
             $jsInfo['MODULE_ID'] = $cnt;
             $jsInfo['TARGET'] = $tmpKey . '_' . $cnt;
             $jsInfo['PREFIX'] = $tmpPrefix . '_' . $cnt;
             $jsInfo['FULL_PATH'] = $jsInfo['PATH'];
             $jsInfo['SKIP'] = true;
             $cnt++;
             $this->targetList[$tmpKey]['JS_LIST'][$jsInfo['TARGET']] = array('TARGET' => $jsInfo['TARGET'], 'PREFIX' => $jsInfo['PREFIX'], 'MODE' => $this->targetList[$assetTID]['MODE'], 'UNIQUE' => false);
         } else {
             $jsInfo['FILE_PATH'] = $this->documentRoot . $js;
             if (file_exists($jsInfo['FILE_PATH']) && filesize($jsInfo['FILE_PATH']) > 0) {
                 $jsInfo['FULL_PATH'] = \CUtil::GetAdditionalFileURL($jsInfo['PATH'], true);
             } else {
                 unset($this->js[$js]);
                 continue;
             }
             if (strncmp($jsInfo['PATH'], '/bitrix/js/', 11) != 0) {
                 $jsInfo['SKIP'] = !(strncmp($jsInfo['PATH'], '/bitrix/panel/', 14) != 0 && strncmp($jsInfo['PATH'], '/bitrix/themes/', 15) != 0 && strncmp($jsInfo['PATH'], '/bitrix/modules/', 16) != 0);
             } else {
                 $jsInfo['TARGET'] = 'KERNEL';
                 if ($this->sliceKernel() && $this->optimizeJs()) {
                     $moduleInfo = $this->isKernelJS($jsInfo['PATH']);
                 } else {
                     $moduleInfo = false;
                 }
                 if ($moduleInfo) {
                     $jsInfo['MODULE_ID'] = $moduleInfo['MODULE_ID'];
                     $jsInfo['TARGET'] = 'KERNEL_' . $moduleInfo['MODULE_ID'];
                     $jsInfo['PREFIX'] = 'kernel_' . $moduleInfo['MODULE_ID'];
                     $jsInfo['BODY'] = $moduleInfo['BODY'];
                 } else {
                     $jsInfo['MODULE_ID'] = $cnt;
                     $jsInfo['TARGET'] = 'KERNEL_' . $cnt;
                     $jsInfo['PREFIX'] = 'kernel_' . $cnt;
                     $jsInfo['SKIP'] = true;
                     $cnt++;
                 }
                 if ($jsInfo['BODY']) {
                     $this->targetList['BODY']['JS_LIST'][$jsInfo['TARGET']] = array('TARGET' => $jsInfo['TARGET'], 'PREFIX' => $jsInfo['PREFIX'], 'MODE' => $this->targetList[$assetTID]['MODE'], 'UNIQUE' => true);
                 } else {
                     if (isset($this->targetList['KERNEL']['JS_LIST'][$jsInfo['TARGET']]['MODE'])) {
                         $this->targetList['KERNEL']['JS_LIST'][$jsInfo['TARGET']]['MODE'] |= $this->targetList[$assetTID]['MODE'];
                     } else {
                         $this->targetList['KERNEL']['JS_LIST'][$jsInfo['TARGET']] = array('TARGET' => $jsInfo['TARGET'], 'PREFIX' => $jsInfo['PREFIX'], 'MODE' => $set['ADDITIONAL'] ? $this->targetList[$set['TARGET'][0]]['MODE'] : $this->targetList[$assetTID]['MODE'], 'UNIQUE' => true);
                     }
                 }
                 // Add information about sets where used
                 foreach ($set['TARGET'] as $setID) {
                     $this->targetList['KERNEL']['JS_LIST'][$jsInfo['TARGET']]['WHERE_USED'][$setID] = true;
                 }
             }
         }
         if ($jsInfo['ADDITIONAL']) {
             $arAdditional[] = $jsInfo;
         } else {
             $this->js[$jsInfo['TARGET']][] = $jsInfo;
         }
         unset($this->js[$js]);
     }
     // Clean body scripts
     foreach ($this->targetList['BODY']['JS_LIST'] as $item) {
         unset($this->targetList['KERNEL']['JS_LIST'][$item['TARGET']]);
     }
     foreach ($arAdditional as $jsInfo) {
         $this->js[$jsInfo['TARGET']][] = $jsInfo;
     }
 }