Nette NEON

NEON は人にやさしいデータの直列化の言語です。Nette では設定ファイルに使われます。Nette\Neon\Neonは NEON を扱う静的なクラスです。

NEON の形式を知って、試してみてください

すべての例で、次の別名が定義されているものとします。

use Nette\Neon\Neon;

インストール

パッケージは Composerでダウンロードしてインストールします。

composer require nette/neon

*.neon のファイルに構文の誤りがないかは、neon-lint のコンソールのコマンドで確かめられます。

vendor/bin/neon-lint [--debug] <path>

encode (mixed $value, bool $blockMode=false, string $indentation="\t")string

$value を NEON に変えて返します。$blockMode のパラメータに true を渡すと、複数行の出力を作れます。$indentation のパラメータは字下げに使う文字を指定します(既定はタブ)。

Neon::encode($value); // $value を NEON に変えて返します
Neon::encode($value, true); // $value を複数行の NEON に変えて返します

encode() メソッドはエラーの場合に Nette\Neon\Exception を投げます。

try {
	$neon = Neon::encode($value);
} catch (Nette\Neon\Exception $e) {
	// 例外の処理
}

decode (string $input): mixed

渡された NEON の文字列を PHP の値に変えます。

スカラー、配列、日付は DateTimeImmutable オブジェクトとして、エンティティNette\Neon\Entityオブジェクトとして返します。

Neon::decode('hello: world'); // 配列 ['hello' => 'world'] を返します

decode() メソッドはエラーの場合に Nette\Neon\Exception を投げます。

try {
	$value = Neon::decode($neon);
} catch (Nette\Neon\Exception $e) {
	// 例外の処理
}

decodeFile (string $file)mixed

ファイルの中身を NEON から PHP へ変え、BOM があれば取り除きます。

Neon::decodeFile('config.neon');

decodeFile() メソッドはエラーの場合に Nette\Neon\Exception を投げます。

新しい版へ上げるなら、アップグレードのページをご覧ください。

バージョン: 3.x