PHPackages                             php-school/psx - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-school/psx

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

php-school/psx
==============

PHP CLI Syntax Highlighter

1.1.2(10y ago)195.1k↑50%1MITPHPPHP &gt;=5.5

Since Oct 18Pushed 10y ago3 watchersCompare

[ Source](https://github.com/php-school/psx)[ Packagist](https://packagist.org/packages/php-school/psx)[ RSS](/packages/php-school-psx/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (6)Used By (0)

PHP CLI Syntax Highlighter
==========================

[](#php-cli-syntax-highlighter)

[![Build Status](https://camo.githubusercontent.com/76a68a9494d01b858f23d6fcf9cfecb648f5c4df89c60176cbdbcd28d27281eb/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7068702d7363686f6f6c2f7073782e7376673f7374796c653d666c61742d737175617265266c6162656c3d4c696e7578)](https://travis-ci.org/php-school/psx)[![Windows Build Status](https://camo.githubusercontent.com/8629e0a465162ecbb9790e243daf4921394165c402ed254c6b1e398084599d28/68747470733a2f2f696d672e736869656c64732e696f2f6170707665796f722f63692f417964696e48617373616e2f7073782f6d61737465722e7376673f7374796c653d666c61742d737175617265266c6162656c3d57696e646f7773)](https://ci.appveyor.com/project/AydinHassan/psx)[![Coverage Status](https://camo.githubusercontent.com/dce2c58a21f89a5fa66f9ef5611ca98683a33b377d43ea8785ad487d6d053ae3/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f7068702d7363686f6f6c2f7073782e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/php-school/psx)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/863235016894bfebb4fcaf09db37336ba517f90b2977bf11ae78ca166fcea2d6/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7068702d7363686f6f6c2f7073782e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/php-school/psx/)

This is a tool to syntax highlight PHP code for display on a terminal. It takes in an AST produced by [nikic/php-parser](https://github.com/nikic/PHP-Parser)and prints the code. It only decorates a subset of the language. It is literally a copy-paste of [PrettyPrinter/Standard](https://github.com/nikic/PHP-Parser/blob/39a039fa4257d3b9209de36cc54f5d3f5d6253f5/lib/PhpParser/PrettyPrinter/Standard.php)with some decorating added around the printing.

Usage
-----

[](#usage)

```
use PhpSchool\PSX\Factory;

$highlighterFactory = new Factory;
$highlighter = $highlighterFactory->__invoke();

echo $highlighter->highlight($phpCode);
```

The colouring by default uses [kevinlebrun/colors.php](https://github.com/kevinlebrun/colors.php). You can use any library you want by building an adapter class which implements `\PhpSchool\PSX\ColourAdapterInterface`, you will need to map the colours in `\PhpSchool\PSX\Colours` to your library.

Customising Colours
-------------------

[](#customising-colours)

```
use PhpParser\ParserFactory;
use Colors\Color;
use PhpSchool\PSX\SyntaxHighlighter;
use PhpSchool\PSX\SyntaxHighlightPrinter;
use PhpSchool\PSX\SyntaxHighlighterConfig;
use PhpSchool\PSX\ColorsAdapter;

$parserFactory  = new ParserFactory;
$color          = new Color;
$color->setForceStyle(true);
$highlighter = new SyntaxHighlighter(
    $parserFactory->create(ParserFactory::PREFER_PHP7),
    new SyntaxHighlightPrinter(
        new SyntaxHighlighterConfig([
            SyntaxHighlighterConfig::TYPE_KEYWORD       => Colours::GREEN,
            SyntaxHighlighterConfig::TYPE_RETURN_NEW    => Colours::BLACK,
        ]),
        new ColorsAdapter($color)
    )
);
```

This will set any keywords as green and return &amp; new statements as black.

Types
-----

[](#types)

Types are defined in `\PhpSchool\PSX\SyntaxHighlighterConfig` Each type can have a it's own colour. Every keyword inside each type will be coloured by that colour.

#### TYPE\_KEYWORD

[](#type_keyword)

- `if`
- `elseif`
- `else`
- `for`
- `foreach`
- `while`
- `do`
- `switch`
- `finally`
- `try`
- `catch`
- `break`
- `continue`
- `throw`
- `goto`
- `function`

#### TYPE\_BRACE

[](#type_brace)

- `{`
- `}`

#### TYPE\_STRING

[](#type_string)

- `'some-string'`

#### TYPE\_CONSTRUCT

[](#type_construct)

- `echo`

#### TYPE\_RETURN\_NEW

[](#type_return_new)

- `return`
- `new`

#### TYPE\_VAR\_DEREF

[](#type_var_deref)

- `->`

#### TYPE\_CALL\_PARENTHESIS

[](#type_call_parenthesis)

- `(`
- `)`

#### TYPE\_CLASS

[](#type_class)

- `SyntaxHighlighterConfig`

#### TYPE\_OPEN\_TAG

[](#type_open_tag)

- `
