mishal /

Less.js 的 PHP 版本

2.2.0 2016-04-25 07:03 UTC

README

Build Status Dependency Status Downloads Latest release

ILess - Less.js 的 PHP 版本

ILess

什么是 Less?

Less 是一种 CSS 预处理器,这意味着它扩展了 CSS 语言,添加了变量、混合、函数和其他许多技术,使 CSS 更易于维护、主题化和扩展。

有关语言的更多信息,请参阅官方网站: https://lesscss.org.cn

这是什么?

ILess 是官方 LESS 处理器(用 JavaScript 编写)的 PHP 版本。当前 ILess 版本与 less.js 2.5.x 兼容。

入门

要在项目中使用 ILess,您可以

要求

要运行 ILess,您需要 PHP >= 5.4.0

功能亮点

  • 允许注册 自定义文件导入器(从文件系统、数据库等)
  • 允许设置 导入目录 以搜索导入
  • 允许使用 PHP 回调定义 自定义 LESS 函数,支持上下文感知动态函数
  • 允许使用 插件 对 CSS 进行预/后处理(目前仅通过 API,而不是命令行)
  • 生成 源映射(用于调试生成的 CSS)
  • 生成与 SASS 兼容的调试信息以及/或简单注释
  • 允许缓存预编译文件和生成的 CSS
  • 使用 PHPUnit 进行 单元测试
  • 编译后的 CSS 与 less.js 编译的 CSS 100% 相同(例外是 JavaScript,无法使用 PHP 进行评估)
  • 提供命令行实用程序
  • 具有友好的异常消息,包括错误位置和文件摘录(当通过命令行使用时,输出会着色)
  • 具有完善的 API 文档,请参阅 文档
  • 兼容 PHP 7 和 HHVM

用法

基本用法

<?php

use ILess\Parser;
use ILess\FunctionRegistry;
use ILess\Node\ColorNode;
use ILess\Node\DimensionNode;

// setup autoloading
// 1) when installed with composer
require 'vendor/autoload.php';

// 2) when installed manually
// require_once 'lib/ILess/Autoloader.php';
// ILess\Autoloader::register();

$parser = new Parser();

// parses the file
$parser->parseFile('screen.less');

// parse string
$parser->parseString('body { color: @color; }');

// assign variables via the API
$parser->setVariables([
    'color' => 'white'
]);

// Add a custom function
$parser->addFunction('superdarken', function(FunctionRegistry $registry, ColorNode $color) {
    return $registry->call('darken', [$color, new DimensionNode(80, '%')]);
});

$css = $parser->getCSS();

echo $css;

使用缓存

<?php

use ILess\Parser;
use ILess\Cache\FileSystemCache;

// setup the parser to use the cache
$parser = new Parser(array(), new FileSystemCache([
    'cache_dir' => sys_get_temp_dir() . '/iless',
    'ttl' => 86400 // the lifetime of cached files in seconds (1 day by default)
]);

解析器将使用缓存驱动程序保存解析的文件和字符串的 序列化数据 以及保存 生成的 CSSttl 选项允许设置缓存文件的生存期。导入文件的 更改将自动重新生成这些文件的缓存

如果通过 API 分配不同的变量,CSS 缓存将不同,以及不同的选项,如 compress 等。

生成的 CSS 也将缓存 ttl 秒。导入文件(变量和选项)的更改将导致 CSS 重新生成。

注意:生成的缓存文件可以复制到云端,导入文件的修改时间不依赖于缓存文件的修改时间。

自定义缓存驱动程序

如果您想将解析的数据和生成的 CSS 缓存到其他地方(如 memcached数据库),请创建自己的驱动程序,实现 ILess\Cache\CacheInterface。请参阅 lib/ILess/Cache/CacheInterface.php

有关更多示例,请参阅源文件中的 examples 文件夹。

命令行使用方法

要编译 LESS 文件(或来自 stdin 的输入),您可以使用位于 bin 目录中的 ILess\CLI 脚本或 PHAR 可执行文件。

PHAR 安装

下载 PHAR 归档

wget http://mishal.github.io/iless/iless-latest.phar
chmod +x iless-latest.phar
mv iless-latest.phar /usr/local/bin/iless
iless --version

从 NetBeans IDE 使用

要从您的 NetBeans IDE(需要版本 7.4)编译 LESS 文件,您需要配置 iless 可执行文件路径。请参阅如何设置编译

您必须配置 less 路径以指向 bin/iless 或 PHAR 可执行文件。

从 PhpStorm IDE 使用

要从您的 PhpStorm IDE 编译 LESS 文件,您需要配置 File watcher 以监视 .less 文件。请参阅手册了解如何操作。您必须配置 program 选项以指向 bin/iless 或 PHAR 可执行文件。

注意:请参阅下面解析器的附加命令行选项。

插件

  • 自定义导入 – 允许创建自定义模式,如导入指令 foo://file.less
  • Autoprefix – 使用 postcss autoprefixer 插件自动添加 CSS 前缀

示例

解析 my.less 并将其压缩后保存到 my.css

$ iless my.less my.css --compress

stdin 解析输入并将其保存到文件 my.css

$ iless - my.css

使用方法和可用选项

 _____        _______ _______ _______
   |   |      |______ |______ |______
 __|__ |_____ |______ ______| ______|

usage: iless [option option=parameter ...] source [destination]

If source is set to `-` (dash or hyphen-minus), input is read from stdin.

options:
   -h, --help               Print help (this message) and exit.
   -v, --version            Print version number and exit.
   -s, --silent             Suppress output of error messages.
   --setup-file             Setup file for the parser. Allows to setup custom variables, plugins...
   --no-color               Disable colorized output.
   -x, --compress           Compress output by removing the whitespace.
   -a, --append             Append the generated CSS to the target file?
   --no-ie-compat           Disable IE compatibility checks.
   --source-map             Outputs an inline sourcemap to the generated CSS (or output to filename.map).
   --source-map-url         The complete url and filename put in the less file.
   --source-map-base-path   Sets sourcemap base path, defaults to current working directory.
   -sm, --strict-math       Strict math. Requires brackets.
   -su, --strict-units      Allows mixed units, e.g. 1px+1em or 1px*1px which have units that cannot be
                            represented.
   -rp, --root-path         Sets rootpath for url rewriting in relative imports and urls. Works with or
                            without the relative-urls option.
   -ru, --relative-urls     Re-writes relative urls to the base less file.
   --url-args               Adds params into url tokens (e.g. 42, cb=42 or a=1&b=2)
   --dump-line-numbers      Outputs filename and line numbers. TYPE can be either 'comments', which will
                            output the debug info within comments, 'mediaquery' that will output the
                            information within a fake media query which is compatible with the SASS
                            format, and 'all' which will do both.

CLI 设置

您可以通过使用项目根目录中的 .iless 文件来设置解析器(插件、自定义变量...),解析器实例作为 $parser 变量可用。

.iless 设置文件的示例

<?php

use ILess\FunctionRegistry;
use ILess\Node\ColorNode;
use ILess\Node\DimensionNode;

/* @var $parser ILess\Parser */

$parser->addVariables([
    'color' => 'white'
]);

$parser->addFunction('superdarken', function (FunctionRegistry $registry, ColorNode $color) {
    return $registry->call('darken', [$color, new DimensionNode(80, '%')]);
});

如果您想从另一个位置使用设置文件,只需从命令行传递 --setup-file 选项即可。

$ iless foo.less --setup-file=/home/user/project/setup.php

问题

在提交任何问题之前,请搜索现有问题。之后,如果您发现了一个错误或想提出功能请求,请创建一个新的问题。请始终创建单元测试。

贡献

欢迎贡献!如果您想参与开发、提交错误报告等,请参阅贡献指南

免责声明 & 关于

iless = I less: He must increase, but I must decrease. [John 3:30]

我出生在一个非信仰家庭,并作为一个无神论者长大。当我30岁的时候,我的女朋友回家告诉我,她现在是一名基督徒,她信仰上帝!这对我是个很大的冲击!我以为她一定是完全疯了!

我决定对这个话题进行深入研究,并给她提供一些证据,证明没有上帝。我对她说,我会不带偏见地去寻找,无论结果如何。大约一年后,我检查了我认为可以带来关于上帝存在证据的论题——科学。

我非常惊讶地看到,在我周围的事物中,甚至在我自己身上,有大量的设计证据。DNA 是一种编程语言,但比我的计算机使用的只有 1 和 0 要复杂得多。我知道,即使我有 10 亿年的时间,任何计算机应用程序也不可能仅仅通过偶然出现或发展。

我得出了一个对我来说是革命性的结论。 上帝存在! 我30岁时的盲目。

我的女朋友告诉我,上帝爱我,希望与我建立关系。耶稣为我而死,并等待我对他的邀请做出回应。我说,我愿意!

现在我是上帝的养子,被拯救到永恒之中。上帝照顾我。他使我摆脱了毒品依赖和其他丑陋的事情。

我知道上帝爱你(这在圣经中有记载),并且希望你也得到拯救。邀请耶稣进入你的生活!

注意:这不是一种宗教!而是与活上帝的关系。

提升你的生活

  • 同意并接受上帝提供的许可。没有接受按钮,但你必须通过信仰来做这件事。接受耶稣为你而死,并承担了你的惩罚。
  • 悔改你的罪。罪是违反上帝赐予的律法的一切(不爱上帝、偷窃、欺诈、撒谎……查看完整列表)。
  • 请求耶稣原谅你,并成为你的个人主人和救主(http://bible.com/37/mrk.2.5-12.ceb)。

如果你全心全意地做了上述步骤,你现在就是一个新的创造。你属于上帝的家庭,你现在拥有了永恒的生命。你已经从永恒的惩罚——从那外黑暗、哭泣和咬牙的地方——中被救赎了。

阅读圣经,并请求上帝与你交谈,并引导你到一个(真正的)教会。周围有许多所谓的教会,但它们既不教导也不实践圣经。

鸣谢

本作品基于Matt AgarMartin JantošovičJosh Schmidt的代码。源代码基于phpsourcemaps,由bspot提供。

所有贡献者均列在单独的wiki页面上。