Contao\Combiner::add PHP Method

add() public method

Add a file to the combined file
public add ( string $strFile, string $strVersion = null, string $strMedia = 'all' )
$strFile string The file to be added
$strVersion string An optional version number
$strMedia string The media type of the file (.css only)
    public function add($strFile, $strVersion = null, $strMedia = 'all')
    {
        $strType = strrchr($strFile, '.');
        // Check the file type
        if ($strType != self::CSS && $strType != self::JS && $strType != self::SCSS && $strType != self::LESS) {
            throw new \InvalidArgumentException("Invalid file {$strFile}");
        }
        $strMode = $strType == self::JS ? self::JS : self::CSS;
        // Set the operation mode
        if ($this->strMode === null) {
            $this->strMode = $strMode;
        } elseif ($this->strMode != $strMode) {
            throw new \LogicException('You cannot mix different file types. Create another Combiner object instead.');
        }
        // Prevent duplicates
        if (isset($this->arrFiles[$strFile])) {
            return;
        }
        // Check the source file
        if (!file_exists(TL_ROOT . '/' . $strFile)) {
            // Handle public bundle resources
            if (file_exists(TL_ROOT . '/web/' . $strFile)) {
                $strFile = 'web/' . $strFile;
            } else {
                return;
            }
        }
        // Default version
        if ($strVersion === null) {
            $strVersion = filemtime(TL_ROOT . '/' . $strFile);
        }
        // Store the file
        $arrFile = array('name' => $strFile, 'version' => $strVersion, 'media' => $strMedia, 'extension' => $strType);
        $this->arrFiles[$strFile] = $arrFile;
        $this->strKey .= '-f' . $strFile . '-v' . $strVersion . '-m' . $strMedia;
    }

Usage Example

Esempio n. 1
0
 /**
  * Replace the dynamic script tags (see #4203)
  *
  * @param string $strBuffer The string with the tags to be replaced
  *
  * @return string The string with the replaced tags
  */
 public static function replaceDynamicScriptTags($strBuffer)
 {
     // HOOK: add custom logic
     if (isset($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags']) && is_array($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'])) {
         foreach ($GLOBALS['TL_HOOKS']['replaceDynamicScriptTags'] as $callback) {
             $strBuffer = static::importStatic($callback[0])->{$callback}[1]($strBuffer);
         }
     }
     $arrReplace = array();
     $strScripts = '';
     // Add the internal jQuery scripts
     if (!empty($GLOBALS['TL_JQUERY']) && is_array($GLOBALS['TL_JQUERY'])) {
         foreach (array_unique($GLOBALS['TL_JQUERY']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_JQUERY]]'] = $strScripts;
     $strScripts = '';
     // Add the internal MooTools scripts
     if (!empty($GLOBALS['TL_MOOTOOLS']) && is_array($GLOBALS['TL_MOOTOOLS'])) {
         foreach (array_unique($GLOBALS['TL_MOOTOOLS']) as $script) {
             $strScripts .= "\n" . trim($script) . "\n";
         }
     }
     $arrReplace['[[TL_MOOTOOLS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal <body> tags
     if (!empty($GLOBALS['TL_BODY']) && is_array($GLOBALS['TL_BODY'])) {
         foreach (array_unique($GLOBALS['TL_BODY']) as $script) {
             $strScripts .= trim($script) . "\n";
         }
     }
     // Command scheduler
     if (!\Config::get('disableCron')) {
         $strScripts .= "\n" . \Template::generateInlineScript('setTimeout(function(){var e=function(e,t){try{var n=new XMLHttpRequest}catch(r){return}n.open("GET",e,!0),n.onreadystatechange=function(){this.readyState==4&&this.status==200&&typeof t=="function"&&t(this.responseText)},n.send()},t="system/cron/cron.";e(t+"txt",function(n){parseInt(n||0)<Math.round(+(new Date)/1e3)-' . \Frontend::getCronTimeout() . '&&e(t+"php")})},5e3);') . "\n";
     }
     $arrReplace['[[TL_BODY]]'] = $strScripts;
     $strScripts = '';
     $objCombiner = new \Combiner();
     // Add the CSS framework style sheets
     if (!empty($GLOBALS['TL_FRAMEWORK_CSS']) && is_array($GLOBALS['TL_FRAMEWORK_CSS'])) {
         foreach (array_unique($GLOBALS['TL_FRAMEWORK_CSS']) as $stylesheet) {
             $objCombiner->add($stylesheet);
         }
     }
     // Add the internal style sheets
     if (!empty($GLOBALS['TL_CSS']) && is_array($GLOBALS['TL_CSS'])) {
         foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n";
             }
         }
     }
     // Add the user style sheets
     if (!empty($GLOBALS['TL_USER_CSS']) && is_array($GLOBALS['TL_USER_CSS'])) {
         foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet) {
             $options = \StringUtil::resolveFlaggedUrl($stylesheet);
             if ($options->static) {
                 $objCombiner->add($stylesheet, $options->mtime, $options->media);
             } else {
                 $strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media) . "\n";
             }
         }
     }
     // Create the aggregated style sheet
     if ($objCombiner->hasEntries()) {
         $strScripts .= \Template::generateStyleTag($objCombiner->getCombinedFile(), 'all') . "\n";
     }
     $arrReplace['[[TL_CSS]]'] = $strScripts;
     $strScripts = '';
     // Add the internal scripts
     if (!empty($GLOBALS['TL_JAVASCRIPT']) && is_array($GLOBALS['TL_JAVASCRIPT'])) {
         $objCombiner = new \Combiner();
         $objCombinerAsync = new \Combiner();
         foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript) {
             $options = \StringUtil::resolveFlaggedUrl($javascript);
             if ($options->static) {
                 if ($options->async) {
                     $objCombinerAsync->add($javascript, $options->mtime);
                 } else {
                     $objCombiner->add($javascript, $options->mtime);
                 }
             } else {
                 $strScripts .= \Template::generateScriptTag(static::addStaticUrlTo($javascript), $options->async) . "\n";
             }
         }
         // Create the aggregated script and add it before the non-static scripts (see #4890)
         if ($objCombiner->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombiner->getCombinedFile()) . "\n" . $strScripts;
         }
         if ($objCombinerAsync->hasEntries()) {
             $strScripts = \Template::generateScriptTag($objCombinerAsync->getCombinedFile(), true) . "\n" . $strScripts;
         }
     }
     // Add the internal <head> tags
     if (!empty($GLOBALS['TL_HEAD']) && is_array($GLOBALS['TL_HEAD'])) {
         foreach (array_unique($GLOBALS['TL_HEAD']) as $head) {
             $strScripts .= trim($head) . "\n";
         }
     }
     $arrReplace['[[TL_HEAD]]'] = $strScripts;
     return str_replace(array_keys($arrReplace), array_values($arrReplace), $strBuffer);
 }
All Usage Examples Of Contao\Combiner::add