Exemple #1
0
 /**
  * prepare a link to .js or .css file for declare in final template
  * will search of a minified version in production mode
  *
  * @global type $context
  * @param string $path, relative from yacs root, or external url
  * @param string $forced_type = 'js' or 'css', if path does not end by .js or .css.
  * @param string $forced_position = 'header', 'defer' of 'footer' to specify where to
  * load a js file (defer always before footer).
  * @param boolean $straitnow to get the link without adding it to stack
  * @return false if unsucceed
  */
 public static function link_file($path, $forced_type = '', $forced_position = '', $straitnow = false)
 {
     global $context;
     // enable shorter function call
     if (strtolower($forced_type) === 'now') {
         $straitnow = true;
         $forced_type = '';
     }
     // avoid linking twice the same file
     $key = md5($path);
     if (isset($context['linked_files'][$key])) {
         return Js_Css::link_exit(true, $path, $straitnow);
     } else {
         $context['linked_files'][$key] = $path;
     }
     // gather info on file
     $path_parts = pathinfo($path);
     // just to avoid warnings
     if (!isset($path_parts['extension'])) {
         $path_parts['extension'] = '';
     }
     // how the script will be considered ?
     $ext = $forced_type ? $forced_type : $path_parts['extension'];
     // we need a extension
     if (!isset($ext)) {
         return Js_Css::link_exit(false, $path, $straitnow);
     }
     // if path is a local file
     if (strncmp($path, 'http', 4) != 0) {
         // check if file exists
         if (!file_exists(Safe::realpath($path))) {
             return Js_Css::link_exit(false, $path, $straitnow);
         }
         // and we are in production mode
         // and file not already minified
         if ($context['with_debug'] == 'N' && !preg_match('/\\.min\\./', $path_parts['filename'])) {
             // minified version path
             $min_v = $path_parts['dirname'] . '/' . $path_parts['filename'] . '.min.' . $path_parts['extension'];
             if (file_exists(Safe::realpath($min_v))) {
                 $path = $min_v;
             }
             // TODO : warning case exept if .core. ;
         }
         // get last revision date
         $revision = Js_css::get_revision(Safe::realpath($path));
         // add root url
         if (isset($context['static_subdom']) && $context['static_subdom']) {
             $path = $context['static_subdom'] . $path;
         } else {
             $path = $context['url_to_master'] . $context['url_to_root'] . $path;
         }
     } else {
         // we can't know the revision date of external files
         $revision = '';
     }
     // css or js ?
     switch ($ext) {
         case 'css':
             $tag = Js_css::build_css_declaration($path . $revision);
             if ($straitnow) {
                 return $tag;
             }
             Js_css::add_css($tag);
             break;
         case 'js':
             $tag = Js_css::build_js_declaration($path . $revision);
             if ($straitnow) {
                 return $tag;
             }
             // target is header if .head. is part of filename
             if (!$forced_position && preg_match('/\\.head\\./', $path_parts['filename'])) {
                 $forced_position = 'header';
             }
             // by default .js goes to page footer
             $target = $forced_position ? $forced_position : 'defer';
             Js_css::add_js($tag, $target);
             break;
         default:
             // error
             return Js_Css::link_exit(false, $path, $straitnow);
     }
     // count files calls over time
     if ($context['with_debug'] == 'Y' && !defined('NO_MODEL_PRELOAD')) {
         $query = 'INSERT INTO ' . SQL::table_name('js_css_calls') . ' SET' . ' id = "' . $key . '",' . ' path = "' . $path . '",' . ' calls = 1' . ' ON DUPLICATE KEY UPDATE calls=calls+1';
         SQL::query($query, TRUE);
     }
     return Js_Css::link_exit(true, $path, $straitnow);
 }