alto/code-highlight
Composer 安装命令:
composer require alto/code-highlight
包简介
A high-performance, semantic syntax highlighter for PHP 8.4+ with zero dependencies. Features context-aware parsing, language embedding, and compatibility with Highlight.js and Prism themes.
关键字:
README 文档
README
Syntax highlighting for PHP projects
Server-side highlighting for the full PHP stack: 27 languages covering PHP, HTML, Twig, JavaScript, CSS, YAML, and more. Zero dependencies, semantic PHP syntax understanding definitions vs. calls, embedded language support, and 490+ compatible themes. Works in Twig templates, Laravel Blade, Symfony controllers—anywhere PHP runs.
Features
- Built for PHP ecosystems: Highlight the full stack—PHP, HTML, Twig, JavaScript, CSS, SQL, YAML, and 20+ more languages in a single library.
- Zero dependencies: No Node.js, Python, or external processes. Just Composer. Works in any PHP 8.4+ environment.
- Full PHP syntax: Semantic parser that understands context—distinguishes
class User(definition) fromnew User()(usage),function greet()( definition) fromgreet()(call). - Embedded languages: Automatically switches parsers for
<style>and<script>tags in HTML, fenced code blocks in Markdown, and{% block css %}in Twig. - Dark mode support: Multiple dark themes included out of the box ( CupertinoDark, Dracula, Noctis) plus compatibility with 490+ Highlight.js and Prism themes.
Install
composer require alto/code-highlight
Usage
use Alto\Code\Highlight\Highlighter; use Alto\Code\Highlight\Theme\GitHubTheme; // 1. Initialize with a theme $highlighter = new Highlighter(new GitHubTheme()); // 2. Output the theme's CSS (typically in your <head>) echo "<style>" . $highlighter->getTheme()->getStylesheet() . "</style>"; // 3. Highlight your code echo $highlighter->highlight($code, 'php');
Languages
PHP
Alto uses a semantic parser for PHP that goes beyond pattern matching to understand code context. It correctly distinguishes between:
- Definitions vs. usage:
class Uservs.new User(),function greet()vs.greet() - Context-aware scoping: Variables, function calls, class instantiation, method calls
Embeddings
Four languages support automatic embedded language detection:
- HTML — CSS in
<style>tags, JavaScript in<script>tags - SVG — CSS in
<style>tags, JavaScript in<script>tags - Markdown — Any language in fenced code blocks (
```language) - Twig — Languages via block names (
{% block css %},{% block javascript %})
Full list
| Category | Languages |
|---|---|
| Programming | Bash, C#, Go, Java, JavaScript, PHP, Python, Ruby, Rust, Swift, TypeScript |
| Markup | HTML, SVG, XML |
| Data | Diff, DotEnv, HTTP, JSON, YAML |
| Prose | Markdown |
| Query | SQL |
| Stylesheet | CSS, SCSS |
| Template | Twig |
| Config | Dockerfile, INI, Makefile |
Themes
Built-in themes
Alto includes 7 built-in themes ready to use:
- Light themes:
Alto,GitHub,Polar,Solar - Dark themes:
CupertinoDark,Dracula,Noctis
use Alto\Code\Highlight\Theme\DraculaTheme; $highlighter = new Highlighter(new DraculaTheme());
Dark mode
Three dark themes are included out of the box:
- CupertinoDark — macOS-inspired dark theme
- Dracula — Popular dark theme with vibrant colors
- Noctis — Low-contrast dark theme for extended coding sessions
Compatibility
Use existing CSS from the Highlight.js (240+ themes), Prism (250+ themes), or TextMate (.tmTheme) ecosystems:
use Alto\Code\Highlight\Adapter\HighlightJsThemeAdapter; $theme = HighlightJsThemeAdapter::fromFile('/path/to/github-dark.css'); $highlighter = new Highlighter($theme);
use Alto\Code\Highlight\Adapter\TextMateThemeAdapter; $theme = TextMateThemeAdapter::fromFile('/path/to/monokai.tmTheme', isDark: true); $highlighter = new Highlighter($theme);
Integrations
Twig Extension
Twig Extension — Highlight code directly in Twig templates using blocks or filters.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
Released by the Alto project under the MIT License. See the LICENSE file for details.
alto/code-highlight 适用场景与选型建议
alto/code-highlight 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「themes」 「tokenizer」 「prism」 「syntax-highlighter」 「php84」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alto/code-highlight 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alto/code-highlight 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alto/code-highlight 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library emulating the PHP internal reflection using just the tokenized source code.
Laravel Themes
WordPress mu-plugin to register the theme directory for the default WordPress themes.
This is twig version of Flow Theme for OXID eShop.
Test theme for Altis DXP, based on Automattic/_s starter theme.
This is a Smarty version of Admin Theme for OXID eShop.
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 46
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-06