popphp/pop-css
Composer 安装命令:
composer require popphp/pop-css
包简介
Pop CSS Component for Pop PHP Framework
README 文档
README
Overview
pop-css provides the ability to create new CSS files as well as parse existing ones.
There is support for a number of CSS-based features such as media queries, comments
and even minification.
pop-css is a component of the Pop PHP Framework.
Install
Install pop-css using Composer.
composer require popphp/pop-css
Or, require it in your composer.json file
"require": {
"popphp/pop-css" : "^2.0.3"
}
Quickstart
In creating CSS, you will use the main CSS object and create and add selector, media and comment objects to it to build your CSS.
use Pop\Css\Css; use Pop\Css\Selector; $css = new Css(); $html = new Selector('html'); $html->setProperties([ 'margin' => 0, 'padding' => 0, 'background-color' => '#fff', 'font-family' => 'Arial, sans-serif' ]); $login = new Selector('#login'); $login->setProperty('margin', 0) ->setProperty('padding', 0); $css->addSelectors([$html, $login]); echo $css;
The above code will produce:
html { margin: 0; padding: 0; background-color: #fff; font-family: Arial, sans-serif; } #login { margin: 0; padding: 0; }
Rendering the CSS can happen a number of ways:
Call the render method
$cssString = $css->render();
Call a string function
echo $css;
Write to file
$css->writeToFile(__DIR__ . '/styles.css');
The main CSS object's constructor is also flexible and other CSS-related objects can be injected into it:
use Pop\Css\Css; use Pop\Css\Selector; $html = new Selector('html'); $html->setProperties([ 'margin' => 0, 'padding' => 0, 'background-color' => '#fff', 'font-family' => 'Arial, sans-serif' ]); $login = new Selector('#login'); $login->setProperty('margin', 0) ->setProperty('padding', 0); $css = new Css($html, $login);
Selectors
The selector object is the main object in which to define the various selectors and add them to the main CSS object. The selector constructor accepts any valid CSS selector.
use Pop\Css\Css; use Pop\Css\Selector; $css = new Css(); // Element selector $html = new Selector('p'); $html->setProperties([ 'margin' => 0, 'padding' => '3px', 'color' => '#555', 'font-family' => 'Arial, sans-serif' ]); // ID selector $login = new Selector('#login'); $login->setProperty('margin', 0) ->setProperty('padding', 0); // Class selector $bold = new Selector('.bold'); $bold->setProperty('font-weight', 'bold'); $css->addSelectors([$html, $login, $bold]); echo $css;
The above code will produce:
p { margin: 0; padding: 3px; color: #555; font-family: Arial, sans-serif; } #login { margin: 0; padding: 0; } .bold { font-weight: bold; }
Media Queries
Media queries can be created as separate objects that contain their own selector objects. They are then added to the main CSS object.
use Pop\Css\Css; use Pop\Css\Selector; use Pop\Css\Media; $css = new Css(); $html = new Selector('html'); $html->setProperties([ 'margin' => 0, 'padding' => 0, 'background-color' => '#fff', 'font-family' => 'Arial, sans-serif' ]); $login = new Selector('#login'); $login->setProperty('margin', 0); $login->setProperty('padding', 0); $login->setProperty('width', '50%'); $p = new Selector('p'); $p->setProperty('margin', 0); $p->setProperty('padding', 0); $p->setProperty('width', '50%'); $media = new Media('screen'); $media->setFeature('max-width', '480px'); $media['#login'] = new Selector(); $media['#login']->setProperty('width', '75%'); $media['p'] = new Selector(); $media['p']->setProperty('width', '75%'); $css->addSelectors([$html, $login, $p]) ->addMedia($media); echo $css;
The above code will produce:
html { margin: 0; padding: 0; background-color: #fff; font-family: Arial, sans-serif; } p { margin: 0; padding: 0; width: 50%; } #login { margin: 0; padding: 0; width: 50%; } @media screen and (max-width: 480px) { p { width: 75%; } #login { width: 75%; } }
Comments
Comments can be added in a number of places. They can be added to the top of the main CSS content, to the top of a selector or to the top of a media query.
use Pop\Css\Css; use Pop\Css\Selector; use Pop\Css\Media; $css = new Css(); $css->addComment('This is a global comment'); $html = new Selector('html'); $html->setProperties([ 'margin' => 0, 'padding' => 0, 'background-color' => '#fff', 'font-family' => 'Arial, sans-serif' ]); $p = new Selector('p'); $p->setProperty('margin', 0); $p->setProperty('padding', 0); $p->setProperty('width', '50%'); $p->addComment('This is a comment for the P selector'); $media = new Media('screen'); $media->setFeature('max-width', '480px'); $media['html'] = new Selector(); $media['html']->setProperty('padding', '1%'); $media['html']->addComment('This is a comment for the HTML selector in the media query'); $media->addComment('This is a comment for the media query'); $css->addSelectors([$html, $p]) ->addMedia($media); echo $css;
The above code will produce:
/* * This is a global comment */ html { margin: 0; padding: 0; background-color: #fff; font-family: Arial, sans-serif; } /* * This is a comment for the P selector */ p { margin: 0; padding: 0; width: 50%; } /* * This is a comment for the media query */ @media screen and (max-width: 480px) { /* * This is a comment for the HTML selector in the media query */ html { padding: 1%; } }
Comments can be tailored with the $wrap and $trailingNewLine properties that can be passed into
the comment constructor. If the $wrap is set to 0, it will force a single-line comment.
use Pop\Css\Css; use Pop\Css\Selector; $p = new Selector('p'); $p->setProperty('margin', 0); $p->setProperty('padding', 0); $p->setProperty('width', '50%'); $p->addComment('This is a comment for the P selector', 0, false); $css = new Css($p); echo $css;
The above code will produce:
/* This is a comment for the P selector */ p { margin: 0; padding: 0; width: 50%; }
Parse CSS
You can parse CSS with the following methods:
use Pop\Css\Css; $css = Css::parseFile('path/to/styles.css'); $css = Css::parseString($cssString); $css = Css::parseUri('http://www.domain.com/css/styles.css');
In each case, it will return a CSS object populated with the related CSS objects from the content of the source.
Minify
Setting the minify property on the main CSS object will perform a basic minification of the CSS content.
use Pop\Css\Css; use Pop\Css\Selector; use Pop\Css\Media; $css = new Css(); $css->addComment('This is a global comment'); $html = new Selector('html'); $html->setProperties([ 'margin' => 0, 'padding' => 0, 'background-color' => '#fff', 'font-family' => 'Arial, sans-serif' ]); $p = new Selector('p'); $p->setProperty('margin', 0); $p->setProperty('padding', 0); $p->setProperty('width', '50%'); $media = new Media('screen'); $media->setFeature('max-width', '480px'); $media['html'] = new Selector(); $media['html']->setProperty('padding', '1%'); $css->addSelectors([$html, $p]) ->addMedia($media); $css->minify(true); echo $css;
The above code will produce:
html{margin:0;padding:0;background-color:#fff;font-family:Arial, sans-serif;}p{margin:0;padding:0;width:50%;} @media screen and (max-width: 480px) {html{padding:1%;}}
popphp/pop-css 适用场景与选型建议
popphp/pop-css 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.72k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「CSS parsing」 「pop」 「pop php」 「css generation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/pop-css 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/pop-css 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/pop-css 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Twig extension to insert css as inline styles with a tag
A port of the PEAR Net_HL7 library to PHP5.3+
Caching and compression for Twig assets (JavaScript and CSS).
An MT940 bank statement parser for PHP
Html5 stream tokenizer/reader (not using libxml)
Rudl UDP cluster logging library (PSR4 compliant)
统计信息
- 总下载量: 7.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 5
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-11-01