colinodell/indentation
Composer 安装命令:
composer require colinodell/indentation
包简介
Library to detect and manipulate indentation in strings and files
README 文档
README
PHP library to detect and manipulate the indentation of files and strings
Installation
composer require --dev colinodell/indentation
Usage
Detecting the indentation of a string or file
use ColinODell\Indentation\Indentation; $indentation = Indentation::detect(file_get_contents('composer.json')); assert($indentation->getAmount() === 4); assert($indentation->getType() === Indentation::TYPE_SPACE); assert((string)$indentation === ' ');
Changing the indentation of a string or file
use ColinODell\Indentation\Indentation; $composerJson = file_get_contents('composer.json'); $composerJson = Indentation::change($composerJson, new Indentation(1, Indentation::TYPE_TAB)); file_put_contents('composer.json', $composerJson);
Adding leading indentation to all lines
Need to add indent all lines by some amount?
use ColinODell\Indentation\Indentation; $codeExample = file_get_contents('file.php'); $indented = Indentation::indent($codeExample, new Indentation(4, Indentation::TYPE_SPACE));
Now you can embed the indented code into a Markdown document! (Hint: This works great with the league/commonmark library.)
Removing leading indentation from all lines
Imagine you have a file where every line is indented by at least 4 spaces:
/**
* Just a silly example
*/
class Cat extends Animal
{
// ...
}
You can trim that leading indentation while preserving the nested indentation with the unindent() method:
use ColinODell\Indentation\Indentation; $contents = file_get_contents('Cat.php'); $trimmed = Indentation::unindent($contents); file_put_contents('Cat.php', $trimmed);
Giving you:
/**
* Just a silly example
*/
class Cat extends Animal
{
// ...
}
Note how the leading 4 spaces are removed but all other indentation (like in the docblock and method body) is preserved.
Detection Algorithm
The current algorithm looks for the most common difference between two consecutive non-empty lines.
In the following example, even if the 4-space indentation is used 3 times whereas the 2-space one is used 2 times, it is detected as less used because there were only 2 differences with this value instead of 4 for the 2-space indentation:
html { box-sizing: border-box; } body { background: gray; } p { line-height: 1.3em; margin-top: 1em; text-indent: 2em; }
Furthermore, if there are more than one most used difference, the indentation with the most lines is selected.
In the following example, the indentation is detected as 4-spaces:
body { background: gray; } p { line-height: 1.3em; margin-top: 1em; text-indent: 2em; }
colinodell/indentation 适用场景与选型建议
colinodell/indentation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.36k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2022 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「files」 「tabs」 「strings」 「spaces」 「indent」 「indentation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 colinodell/indentation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 colinodell/indentation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 colinodell/indentation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
An HTML5 upload field for the CMS and frontend forms.
A very simple yet useful helper class to handle PHP file uploads.
The flysystem adapter for yandex disk rest api.
Create tabs as content element. Extension for Contao Open Source CMS
Generate pseudo locale string for unicode testing and marking missing strings
Laravel Livewire Tab Form component
统计信息
- 总下载量: 13.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 39
- 点击次数: 25
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-16