/** * @param Less_Functions $ctx */ public function compile($ctx) { $val = $this->value->compile($ctx); if (!$this->isEvald) { // Add the base path if the URL is relative if (Less_Parser::$options['relativeUrls'] && $this->currentFileInfo && is_string($val->value) && Less_Environment::isPathRelative($val->value)) { $rootpath = $this->currentFileInfo['uri_root']; if (!$val->quote) { $rootpath = preg_replace('/[\\(\\)\'"\\s]/', '\\$1', $rootpath); } $val->value = $rootpath . $val->value; } $val->value = Less_Environment::normalizePath($val->value); } // Add cache buster if enabled if (Less_Parser::$options['urlArgs']) { if (!preg_match('/^\\s*data:/', $val->value)) { $delimiter = strpos($val->value, '?') === false ? '?' : '&'; $urlArgs = $delimiter . Less_Parser::$options['urlArgs']; $hash_pos = strpos($val->value, '#'); if ($hash_pos !== false) { $val->value = substr_replace($val->value, $urlArgs, $hash_pos, 0); } else { $val->value .= $urlArgs; } } } return new Less_Tree_URL($val, $this->currentFileInfo, true); }
public function datauri($mimetypeNode, $filePathNode = null) { $filePath = $filePathNode ? $filePathNode->value : null; $mimetype = $mimetypeNode->value; $args = 2; if (!$filePath) { $filePath = $mimetype; $args = 1; } $filePath = str_replace('\\', '/', $filePath); if (Less_Environment::isPathRelative($filePath)) { if (Less_Parser::$options['relativeUrls']) { $temp = $this->currentFileInfo['currentDirectory']; } else { $temp = $this->currentFileInfo['entryPath']; } if (!empty($temp)) { $filePath = Less_Environment::normalizePath(rtrim($temp, '/') . '/' . $filePath); } } // detect the mimetype if not given if ($args < 2) { /* incomplete $mime = require('mime'); mimetype = mime.lookup(path); // use base 64 unless it's an ASCII or UTF-8 format var charset = mime.charsets.lookup(mimetype); useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0; if (useBase64) mimetype += ';base64'; */ $mimetype = Less_Mime::lookup($filePath); $charset = Less_Mime::charsets_lookup($mimetype); $useBase64 = !in_array($charset, array('US-ASCII', 'UTF-8')); if ($useBase64) { $mimetype .= ';base64'; } } else { $useBase64 = preg_match('/;base64$/', $mimetype); } if (file_exists($filePath)) { $buf = @file_get_contents($filePath); } else { $buf = false; } // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded // and the --ieCompat flag is enabled, return a normal url() instead. $DATA_URI_MAX_KB = 32; $fileSizeInKB = round(strlen($buf) / 1024); if ($fileSizeInKB >= $DATA_URI_MAX_KB) { $url = new Less_Tree_Url($filePathNode ? $filePathNode : $mimetypeNode, $this->currentFileInfo); return $url->compile($this); } if ($buf) { $buf = $useBase64 ? base64_encode($buf) : rawurlencode($buf); $filePath = '"data:' . $mimetype . ',' . $buf . '"'; } return new Less_Tree_Url(new Less_Tree_Anonymous($filePath)); }
/** * @param string $filename */ public function SetFileInfo($filename, $uri_root = '') { $filename = Less_Environment::normalizePath($filename); $dirname = preg_replace('/[^\\/\\\\]*$/', '', $filename); if (!empty($uri_root)) { $uri_root = rtrim($uri_root, '/') . '/'; } $currentFileInfo = array(); //entry info if (isset($this->env->currentFileInfo)) { $currentFileInfo['entryPath'] = $this->env->currentFileInfo['entryPath']; $currentFileInfo['entryUri'] = $this->env->currentFileInfo['entryUri']; $currentFileInfo['rootpath'] = $this->env->currentFileInfo['rootpath']; } else { $currentFileInfo['entryPath'] = $dirname; $currentFileInfo['entryUri'] = $uri_root; $currentFileInfo['rootpath'] = $dirname; } $currentFileInfo['currentDirectory'] = $dirname; $currentFileInfo['currentUri'] = $uri_root . basename($filename); $currentFileInfo['filename'] = $filename; $currentFileInfo['uri_root'] = $uri_root; //inherit reference if (isset($this->env->currentFileInfo['reference']) && $this->env->currentFileInfo['reference']) { $currentFileInfo['reference'] = true; } $this->env->currentFileInfo = $currentFileInfo; }
/** * Using the import directories, get the full absolute path and uri of the import * * @param Less_Tree_Import $evald */ public function PathAndUri() { $evald_path = $this->getPath(); if ($evald_path) { $import_dirs = array(); if (Less_Environment::isPathRelative($evald_path)) { //if the path is relative, the file should be in the current directory $import_dirs[$this->currentFileInfo['currentDirectory']] = $this->currentFileInfo['uri_root']; } else { //otherwise, the file should be relative to the server root $import_dirs[$this->currentFileInfo['entryPath']] = $this->currentFileInfo['entryUri']; //if the user supplied entryPath isn't the actual root $import_dirs[$_SERVER['DOCUMENT_ROOT']] = ''; } // always look in user supplied import directories $import_dirs = array_merge($import_dirs, Less_Parser::$options['import_dirs']); foreach ($import_dirs as $rootpath => $rooturi) { if (is_callable($rooturi)) { list($path, $uri) = call_user_func($rooturi, $evald_path); if (is_string($path)) { $full_path = $path; return array($full_path, $uri); } } elseif (!empty($rootpath)) { $path = rtrim($rootpath, '/\\') . '/' . ltrim($evald_path, '/\\'); if (file_exists($path)) { $full_path = Less_Environment::normalizePath($path); $uri = Less_Environment::normalizePath(dirname($rooturi . $evald_path)); return array($full_path, $uri); } elseif (file_exists($path . '.less')) { $full_path = Less_Environment::normalizePath($path . '.less'); $uri = Less_Environment::normalizePath(dirname($rooturi . $evald_path . '.less')); return array($full_path, $uri); } } } } }
public function compilePath($env) { $path = $this->path->compile($env); $rootpath = ''; if ($this->currentFileInfo && $this->currentFileInfo['rootpath']) { $rootpath = $this->currentFileInfo['rootpath']; } if (!$path instanceof Less_Tree_URL) { if ($rootpath) { $pathValue = $path->value; // Add the base path if the import is relative if ($pathValue && Less_Environment::isPathRelative($pathValue)) { $path->value = $this->currentFileInfo['uri_root'] . $pathValue; } } $path->value = Less_Environment::normalizePath($path->value); } return $path; }
/** * @param Less_Functions $ctx */ public function compile($ctx) { $val = $this->value->compile($ctx); if (!$this->isEvald) { // Add the base path if the URL is relative if (Less_Parser::$options['relativeUrls'] && $this->currentFileInfo && is_string($val->value) && Less_Environment::isPathRelative($val->value)) { $rootpath = $this->currentFileInfo['uri_root']; if (!$val->quote) { $rootpath = preg_replace('/[\\(\\)\'"\\s]/', '\\$1', $rootpath); } $val->value = $rootpath . $val->value; } $val->value = Less_Environment::normalizePath($val->value); } return new Less_Tree_URL($val, $this->currentFileInfo, true); }