--- title: "Props Builder for PHP" pageTitle: "Fylgja Props Builder for PHP" description: "Generate Design Tokens (CSS custom properties) from PHP arrays or JSON files, with the same output as the Node version." composer: "fylgja/props-builder" git: "https://github.com/fylgja/php-props-builder" sortOrder: 21 keywords: ["php", "composer", "inline style", "magento", "laravel"] faq: - question: Is the output the same as the Node version? answer: Yes. Every release matches the Node release with the same version number, and CI runs a shared set of cases through both versions and fails on any difference in the output. The only difference is the order of some properties, since JavaScript sorts integer keys of an object first. - question: Why is my scale keyed from 0 joined with commas? answer: A PHP array keyed 0, 1, 2 is a list, and lists are treated as value lists, the same as a JSON array. Pass the scale as an object, e.g. (object) ['#f8f9fa', '#f1f3f5'], or decode JSON files with json_decode() without the associative flag, so every JSON object stays a map. - question: Does it work in frameworks like Laravel or Magento 2? answer: Yes. It needs PHP 8.1 or newer and has no other dependencies, so it installs next to any framework. It does not print warnings or notices, so strict error handlers, like the one in Magento's developer mode, do not throw on it. - question: Where do the warnings for skipped dtcg tokens go? answer: To error_log() by default. Pass a callable as the onWarning option to handle them yourself, e.g. to send them to your framework's logger. --- The Fylgja Props Builder for PHP converts PHP arrays or JSON files into CSS custom properties, SCSS variables or Design Token files. It is a port of the [Node version](/library/extensions/props-builder/), so the same tokens can be built from backend logic without Node. ## Installation ```sh composer require fylgja/props-builder ``` Requires PHP 8.1 or newer. ## Usage ```php use Fylgja\PropsBuilder\PropsBuilder; PropsBuilder::build( [ 'color' => [ 'red' => '#f00', 'green' => '#0f0', 'blue' => '#00f', ], ], 'output.css', ); ``` This writes `output.css` with the following content: ```css :where(:root) { --color-red: #f00; --color-green: #0f0; --color-blue: #00f; } ``` `build()` always returns the generated output, so it can also be used without writing a file. ### Options The options are named arguments, the same as the options object in the Node version. ```php PropsBuilder::build( $props, // Required: array, object or (for Stitch input) a Markdown string. $filename, // Required: name of the output file, also picks the output format. parseAs: 'auto', // Output format, defaults to the format of the filename. writeToFile: true, // Write the output to $filename. selector: ':where(:root)', // CSS selector for the custom properties (css output only). wrapper: '', // Nests the Design Tokens under this key (e.g. for Figma). name: 'Design Tokens', // Name of the Design Tokens Resolver (dtcg output only). description: '', // Description of the Design Tokens Resolver (dtcg output only). banner: '', // Prepended to the output as is. inputTypeTokens: false, // Set to true if $props is a Design Tokens file. inputTypeSyntax: 'default', // Syntax of the input tokens: default, figma, style-dictionary or stitch. stripPrefix: '', // Dot/dash separated key path (or list of keys) to unwrap (e.g. 'tokens.values'). rename: null, // Map of [currentKey => newKey], applied recursively (e.g. ['colors' => 'color']). onWarning: null, // Receives warnings, such as skipped dtcg tokens. Defaults to error_log(). ); ``` Options can also be passed from an array with `PropsBuilder::build($props, $filename, ...$options)`. The output formats and options work the same as in the Node version, see its [output formats](/library/extensions/props-builder/#output-formats) and [Design Tokens 2025.10](/library/extensions/props-builder/#to-design-tokens-202510) sections. ### Lists and scales PHP arrays are both lists and maps, so a list is treated as a value list, the same as a JSON array. A value list is joined with a comma, e.g. the layers of a shadow or a font stack. ```php 'shadow' => [ 1 => ['0 1px 2px #0002', '0 2px 4px #0002'], // --shadow-1: 0 1px 2px #0002, 0 2px 4px #0002; ], ``` A scale keyed from `0` is also a list in PHP. Pass it as an object to keep it a scale: ```php 'gray' => (object) ['#f8f9fa', '#f1f3f5', '#e9ecef'], // --gray-0, --gray-1, --gray-2 ``` For JSON files, decode them as objects (the default of `json_decode()`), so every JSON object stays a map: ```php $tokens = json_decode(file_get_contents('tokens.json')); PropsBuilder::build($tokens, 'tokens.css', inputTypeTokens: true); ``` ## Direct output Set `writeToFile: false` to only get the output as a string, for example to render the tokens in a ` ``` Instead of a filename with the right extension, you can also set the format with `parseAs`, e.g. `PropsBuilder::build($props, 'inline', parseAs: 'css', writeToFile: false)`. To scope the tokens to a part of the page, such as a theme picked in the backend, set the `selector`: ```php $css = PropsBuilder::build( $brandTokens, 'tokens.css', writeToFile: false, selector: '[data-theme="brand"]', ); ``` Every output format works this way. For example, `parseAs: 'json'` or `parseAs: 'css-jit'` gives the tokens as JSON, to pass them to JavaScript in a `