本文整理汇总了PHP中CssMin类的典型用法代码示例。如果您正苦于以下问题:PHP CssMin类的具体用法?PHP CssMin怎么用?PHP CssMin使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CssMin类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: minifycss
/**
* minify css and return css link
* if minify is disabled: return direct css links
*
* @return string with html tag
* @param array $stylesheets with css files
*/
public function minifycss($stylesheets)
{
if (Zend_Registry::get('config')->cache->enable == 1 && Zend_Registry::get('config')->cache->minifycss == 1) {
// check file
$target = Zend_Registry::get('config')->pub->path . 'stylesheets/' . Zend_Registry::get('config')->cache->minifiedcssfile;
$targeturl = 'stylesheets/' . Zend_Registry::get('config')->cache->minifiedcssfile;
if (file_exists($target)) {
return "<link rel=\"stylesheet\" media=\"screen, handheld, projection, tv\" href=\"" . $targeturl . "\" />\n";
}
// load and minify files
$all = "";
foreach ($stylesheets as $css) {
$csscontent = file_get_contents(Zend_Registry::get('config')->pub->path . $css);
$csscontent = CssMin::minify($csscontent);
$all .= $csscontent;
}
file_put_contents($target, $all);
return "<link rel=\"stylesheet\" media=\"screen, handheld, projection, tv\" href=\"" . $targeturl . "\" />\n";
} else {
$ret = "";
foreach ($stylesheets as $css) {
$ret = $ret . "<link rel=\"stylesheet\" media=\"screen, handheld, projection, tv\" href=\"" . $css . "\" />\n";
}
return $ret;
}
}
示例2: load
/**
*
*/
public function load($resource, $type = null)
{
$publishPath = $this->pathHelper->joinPaths($this->assetDirectory, $resource);
$tmpPath = $this->pathHelper->joinPaths(sys_get_temp_dir(), $resource);
$fs = $this->getFilesystem();
if ($this->debugMode) {
$fs->dumpFile($tmpPath, "/* --- composition: {$resource} ---*/");
}
$tmpFile = fopen($tmpPath, "a");
foreach ($this->compositions[$resource] as $asset) {
$path = $this->locator->locate($asset);
switch ($type) {
case 'css':
if (preg_match('/\\.min\\.css/', $asset)) {
$content = file_get_contents($path);
} else {
$content = \CssMin::minify(file_get_contents($path));
}
break;
default:
$content = file_get_contents($path);
}
if ($this->debugMode) {
fwrite($tmpFile, "\n\n/* --- asset: {$asset} ({$path}) ---*/\n\n" . $content);
}
}
fclose($tmpFile);
if ($this->publishMode && $resource !== null) {
$fs->copy($tmpPath, $publishPath);
}
return $tmpPath;
}
示例3: parse
/**
* Implements {@link aCssParserPlugin::parse()}.
*
* @param integer $index Current index
* @param string $char Current char
* @param string $previousChar Previous char
* @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
*/
public function parse($index, $char, $previousChar, $state)
{
if ($char === "@" && $state === "T_DOCUMENT" && strtolower(substr($this->parser->getSource(), $index, 7)) === "@import") {
$this->parser->pushState("T_AT_IMPORT");
$this->parser->clearBuffer();
return $index + 7;
} elseif (($char === ";" || $char === "\n") && $state === "T_AT_IMPORT") {
$this->buffer = $this->parser->getAndClearBuffer(";");
$pos = false;
foreach (array(")", "\"", "'") as $needle) {
if (($pos = strrpos($this->buffer, $needle)) !== false) {
break;
}
}
$import = substr($this->buffer, 0, $pos + 1);
if (stripos($import, "url(") === 0) {
$import = substr($import, 4, -1);
}
$import = trim($import, " \t\n\r\v'\"");
$mediaTypes = array_filter(array_map("trim", explode(",", trim(substr($this->buffer, $pos + 1), " \t\n\r\v{"))));
if ($pos) {
$this->parser->appendToken(new CssAtImportToken($import, $mediaTypes));
} else {
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Invalid @import at-rule syntax", $this->parser->buffer));
}
$this->parser->popState();
} else {
return false;
}
return true;
}
示例4: __invoke
public function __invoke($file, $minify = null)
{
if (!is_file($this->getOptions()->getPublicDir() . $file)) {
throw new \InvalidArgumentException('File "' . $this->getOptions()->getPublicDir() . $file . '" not found.');
}
$less = new \lessc();
$info = pathinfo($file);
$newFile = $this->getOptions()->getDestinationDir() . $info['filename'] . '.' . filemtime($this->getOptions()->getPublicDir() . $file) . '.css';
$_file = $this->getOptions()->getPublicDir() . $newFile;
if (!is_file($_file)) {
$globPattern = $this->getOptions()->getPublicDir() . $this->getOptions()->getDestinationDir() . $info['filename'] . '.*.css';
foreach (Glob::glob($globPattern, Glob::GLOB_BRACE) as $match) {
if (preg_match("/^" . $info['filename'] . "\\.[0-9]{10}\\.css\$/", basename($match))) {
unlink($match);
}
}
$compiledFile = new \SplFileObject($_file, 'w');
$result = $less->compileFile($this->getOptions()->getPublicDir() . $file);
if (is_null($minify) && $this->getOptions()->getMinify() || $minify === true) {
$result = \CssMin::minify($result);
}
$compiledFile->fwrite($result);
}
return $newFile;
}
示例5: prepare
function prepare($source, $min = true)
{
if ($min) {
$source = \CssMin::minify($source);
}
return trim($source);
}
示例6: load
/**
*
*/
public function load($resource, $type = null)
{
$publishPath = $this->pathHelper->joinPaths($this->assetDirectory, $resource);
$tmpPath = $this->pathHelper->joinPaths(sys_get_temp_dir(), $resource);
$path = $this->locator->locate($resource);
$fs = $this->getFilesystem();
$fs->dumpFile($tmpPath, null);
switch ($type) {
case 'css':
// minify only if it isnt already minified
if (preg_match('/\\.min\\.css/', $resource)) {
$content = file_get_contents($path);
} else {
$content = \CssMin::minify(file_get_contents($path));
}
break;
default:
$content = file_get_contents($path);
}
file_put_contents($tmpPath, $content);
if ($this->publishMode && $resource !== null) {
$fs->copy($tmpPath, $publishPath);
}
return $tmpPath;
}
示例7: output
/**
* Apply CssMin to $content.
*
* @param string $filename target filename
* @param string $content Content to filter.
* @throws Exception
* @return string
*/
public function output($filename, $content) {
App::import('Vendor', 'cssmin', array('file' => $this->_settings['path']));
if (!class_exists('CssMin')) {
throw new Exception(sprintf('Cannot not load filter class "%s".', 'CssMin'));
}
return CssMin::minify($content);
}
示例8: __construct
/**
* Constructer.
*
* Creates instances of {@link aCssMinifierFilter filters} and {@link aCssMinifierPlugin plugins}.
*
* @param string $source CSS source [optional]
* @param array $filters Filter configuration [optional]
* @param array $plugins Plugin configuration [optional]
* @return void
*/
public function __construct($source = null, array $filters = null, array $plugins = null)
{
$filters = array_merge(array("ImportImports" => false, "RemoveComments" => true, "RemoveEmptyRulesets" => true, "RemoveEmptyAtBlocks" => true, "ConvertLevel3Properties" => false, "ConvertLevel3AtKeyframes" => false, "Variables" => true, "RemoveLastDelarationSemiColon" => true), is_array($filters) ? $filters : array());
$plugins = array_merge(array("Variables" => true, "ConvertFontWeight" => false, "ConvertHslColors" => false, "ConvertRgbColors" => false, "ConvertNamedColors" => false, "CompressColorValues" => false, "CompressUnitValues" => false, "CompressExpressionValues" => false), is_array($plugins) ? $plugins : array());
// Filters
foreach ($filters as $name => $config) {
if ($config !== false) {
$class = "Css" . $name . "MinifierFilter";
$config = is_array($config) ? $config : array();
if (class_exists($class)) {
$this->filters[] = new $class($this, $config);
} else {
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The filter <code>" . $name . "</code> with the class name <code>" . $class . "</code> was not found"));
}
}
}
// Plugins
foreach ($plugins as $name => $config) {
if ($config !== false) {
$class = "Css" . $name . "MinifierPlugin";
$config = is_array($config) ? $config : array();
if (class_exists($class)) {
$this->plugins[] = new $class($this, $config);
} else {
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>" . $name . "</code> with the class name <code>" . $class . "</code> was not found"));
}
}
}
// --
if (!is_null($source)) {
$this->minify($source);
}
}
示例9: minifyCss
public static function minifyCss($cssList, $concat = false)
{
$websiteHelper = Zend_Controller_Action_HelperBroker::getExistingHelper('website');
$cacheHelper = Zend_Controller_Action_HelperBroker::getExistingHelper('cache');
if (null === ($hashStack = $cacheHelper->load(strtolower(__CLASS__), ''))) {
$hashStack = array();
}
$container = $cssList->getContainer();
foreach ($container->getArrayCopy() as $css) {
if (preg_match('/^https?:\\/\\//', $css->href) != false && strpos($css->href, $websiteHelper->getUrl()) !== 0) {
continue;
}
$path = str_replace($websiteHelper->getUrl(), '', $css->href);
if (!is_file($websiteHelper->getPath() . $path) || !file_exists($websiteHelper->getPath() . $path)) {
continue;
}
$hash = sha1_file($websiteHelper->getPath() . $path);
$name = Tools_Filesystem_Tools::basename($path);
if (!$hash) {
continue;
}
if (!isset($hashStack[$path]) || $hashStack[$path]['hash'] !== $hash) {
$compressor = new CssMin();
$cssContent = Tools_Filesystem_Tools::getFile($path);
$cssContent = preg_replace('/url\\([\'"]?([^)\'"]*)[\'"]?\\)/', 'url("../' . dirname($path) . DIRECTORY_SEPARATOR . '${1}")', $cssContent);
$hashStack[$path] = array('hash' => $hash, 'content' => $compressor->run($cssContent));
Tools_Filesystem_Tools::saveFile($websiteHelper->getPath() . $websiteHelper->getTmp() . $hash . '.css', $hashStack[$path]['content']);
unset($cssContent);
}
if (!$concat) {
$css->href = $websiteHelper->getUrl() . $websiteHelper->getTmp() . $hash . '.css?' . $name;
} else {
$concatCss = isset($concatCss) ? $concatCss . PHP_EOL . "/* {$path} */" . PHP_EOL . $hashStack[$path]['content'] : "/* {$path} */" . PHP_EOL . $hashStack[$path]['content'];
}
}
if (isset($concatCss) && !empty($concatCss)) {
$cname = sha1($concatCss) . '.concat.min.css';
$concatPath = $websiteHelper->getPath() . $websiteHelper->getTmp() . $cname;
if (!file_exists($concatPath) || sha1_file($concatPath) !== sha1($concatCss)) {
Tools_Filesystem_Tools::saveFile($concatPath, $concatCss);
}
$cssList->setStylesheet($websiteHelper->getUrl() . $websiteHelper->getTmp() . $cname);
}
$cacheHelper->save(strtolower(__CLASS__), $hashStack, '', array(), Helpers_Action_Cache::CACHE_LONG);
return $cssList;
}
示例10: apply
/**
*/
public function apply($in, $params = [])
{
require_php_lib('cssmin');
if (!class_exists('\\CssMin')) {
throw new Exception('Assets: class \\CssMin not found');
return $in;
}
return \CssMin::minify($in);
}
示例11: renderer
/**
* New resource file update handler.
*
* @param string $resource Resource full path
* @param string $extension Resource extension
* @param string $content Compiled output resource content
*/
public function renderer($resource, &$extension, &$content)
{
// If CSS resource has been updated
if ($extension === 'css') {
// Read updated CSS resource file and compile it
$content = \CssMin::minify($content);
} elseif ($extension === 'js') {
$content = \JShrink\Minifier::minify($content);
}
}
示例12: parse
/**
* Implements {@link aCssParserPlugin::parse()}.
*
* @param integer $index Current index
* @param string $char Current char
* @param string $previousChar Previous char
* @return mixed TRUE will break the processing; FALSE continue with the next plugin; integer set a new index and break the processing
*/
public function parse($index, $char, $previousChar, $state)
{
// Start of Ruleset and selectors
if ($char === "," && ($state === "T_DOCUMENT" || $state === "T_AT_MEDIA" || $state === "T_RULESET::SELECTORS")) {
if ($state !== "T_RULESET::SELECTORS") {
$this->parser->pushState("T_RULESET::SELECTORS");
}
$this->selectors[] = $this->parser->getAndClearBuffer(",{");
} elseif ($char === "{" && ($state === "T_DOCUMENT" || $state === "T_AT_MEDIA" || $state === "T_RULESET::SELECTORS")) {
if ($this->parser->getBuffer() !== "") {
$this->selectors[] = $this->parser->getAndClearBuffer(",{");
if ($state == "T_RULESET::SELECTORS") {
$this->parser->popState();
}
$this->parser->pushState("T_RULESET");
$this->parser->appendToken(new CssRulesetStartToken($this->selectors));
$this->selectors = array();
}
} elseif ($char === ":" && $state === "T_RULESET") {
$this->parser->pushState("T_RULESET_DECLARATION");
$this->buffer = $this->parser->getAndClearBuffer(":;", true);
} elseif ($char === ":" && $state === "T_RULESET_DECLARATION") {
// Ignore Internet Explorer filter declarations
if ($this->buffer === "filter") {
return false;
}
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": Unterminated declaration", $this->buffer . ":" . $this->parser->getBuffer() . "_"));
} elseif (($char === ";" || $char === "}") && $state === "T_RULESET_DECLARATION") {
$value = $this->parser->getAndClearBuffer(";}");
if (strtolower(substr($value, -10, 10)) === "!important") {
$value = trim(substr($value, 0, -10));
$isImportant = true;
} else {
$isImportant = false;
}
$this->parser->popState();
$this->parser->appendToken(new CssRulesetDeclarationToken($this->buffer, $value, $this->parser->getMediaTypes(), $isImportant));
// Declaration ends with a right curly brace; so we have to end the ruleset
if ($char === "}") {
$this->parser->appendToken(new CssRulesetEndToken());
$this->parser->popState();
}
$this->buffer = "";
} elseif ($char === "}" && $state === "T_RULESET") {
$this->parser->popState();
$this->parser->clearBuffer();
$this->parser->appendToken(new CssRulesetEndToken());
$this->buffer = "";
$this->selectors = array();
} else {
return false;
}
return true;
}
示例13: init
public function init($cssData)
{
$this->parsedCSS = CssMin::parse($cssData, $plugins);
$this->countSelectors();
if ($this->selCount > $this->conf['css.']['scriptmergerBless.']['threshold'] && isset($this->conf['css.']['scriptmergerBless.']['activate']) && $this->conf['css.']['scriptmergerBless.']['activate'] == 1) {
$this->split();
} else {
$this->cssFiles[] = $cssData;
}
return array_reverse($this->cssFiles);
}
示例14: apply
/**
* Implements {@link aCssMinifierFilter::filter()}.
*
* @param array $tokens Array of objects of type aCssToken
* @return integer Count of added, changed or removed tokens; a return value large than 0 will rebuild the array
*/
public function apply(array &$tokens)
{
$variables = array();
$defaultMediaTypes = array("all");
$mediaTypes = array();
$remove = array();
for ($i = 0, $l = count($tokens); $i < $l; $i++) {
// @variables at-rule block found
if (get_class($tokens[$i]) === "CssAtVariablesStartToken") {
$remove[] = $i;
$mediaTypes = count($tokens[$i]->MediaTypes) == 0 ? $defaultMediaTypes : $tokens[$i]->MediaTypes;
foreach ($mediaTypes as $mediaType) {
if (!isset($variables[$mediaType])) {
$variables[$mediaType] = array();
}
}
// Read the variable declaration tokens
for ($i = $i; $i < $l; $i++) {
// Found a variable declaration => read the variable values
if (get_class($tokens[$i]) === "CssAtVariablesDeclarationToken") {
foreach ($mediaTypes as $mediaType) {
$variables[$mediaType][$tokens[$i]->Property] = $tokens[$i]->Value;
}
$remove[] = $i;
} elseif (get_class($tokens[$i]) === "CssAtVariablesEndToken") {
$remove[] = $i;
break;
}
}
}
}
// Variables in @variables at-rule blocks
foreach ($variables as $mediaType => $null) {
foreach ($variables[$mediaType] as $variable => $value) {
// If a var() statement in a variable value found...
if (stripos($value, "var") !== false && preg_match_all("/var\\((.+)\\)/iSU", $value, $m)) {
// ... then replace the var() statement with the variable values.
for ($i = 0, $l = count($m[0]); $i < $l; $i++) {
$variables[$mediaType][$variable] = str_replace($m[0][$i], isset($variables[$mediaType][$m[1][$i]]) ? $variables[$mediaType][$m[1][$i]] : "", $variables[$mediaType][$variable]);
}
}
}
}
// Remove the complete @variables at-rule block
foreach ($remove as $i) {
$tokens[$i] = null;
}
if (!($plugin = $this->minifier->getPlugin("CssVariablesMinifierPlugin"))) {
CssMin::triggerError(new CssError(__FILE__, __LINE__, __METHOD__ . ": The plugin <code>CssVariablesMinifierPlugin</code> was not found but is required for <code>" . __CLASS__ . "</code>"));
} else {
$plugin->setVariables($variables);
}
return count($remove);
}
示例15: filterDump
public function filterDump(AssetInterface $asset)
{
$filters = $this->filters;
$plugins = $this->plugins;
if (isset($filters['ImportImports']) && true === $filters['ImportImports']) {
if ($dir = $asset->getSourceDirectory()) {
$filters['ImportImports'] = array('BasePath' => $dir);
} else {
unset($filters['ImportImports']);
}
}
$asset->setContent(\CssMin::minify($asset->getContent(), $filters, $plugins));
}