PHP's YAML functions (https://www.php.net/manual/ja/ref.yaml.php) are not bundled with PHP by default.
To parse YAML data into a PHP array (syntax parse and transform), or from a PHP array to YAML data with a YAML function, you need to install the PECL extension Jules (https://www.php) .net / manual / ja / yaml.installation.php).
However, PECL is not installed on the latest (as of 08/28/2020) php: 8.0.0beta2
and php: 8.0.0beta2-alpine
images. It seems that PEAR / PECL installer has been eliminated since PHP 7.4.
TL; DR
I need to build PECL from source, which is also a hassle, so I created an image with the pecl
command. It works not only with Intel / AMD but also with ARMv5, v7, ARM64 such as Raspberry Pi.
docker pull keinos/php8-jit:latest
--Image: keinos / php8-jit @ DockerHub
$ docker run --rm keinos/php8-jit pecl version
PEAR Version: 1.10.12
PHP Version: 8.0.0-dev
Zend Engine Version: 4.0.0-dev
Running on: Linux 3fc54c34122a 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64
Also, since many pre-built packages in the PECL repository do not work, we also make it possible to install PECL packages from source.
Need yaml-dev
docker-php-ext-pecl install yaml
TS; DR
Dockerfile
FROM keinos/php8-jit:latest
USER root
COPY sample.php /app/sample.php
RUN \
apk --no-cache add yaml-dev && \
docker-php-ext-pecl install yaml
ENTRYPOINT [ "php", "/app/sample.php" ]
sample.php
<?php
//YAML sample string
$yaml = <<<EOD
---
invoice: 34843
date: "2001-01-23"
bill-to: &id001
given: Chris
family: Dumars
address:
lines: |-
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: *id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450
- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392
tax: 251.420000
total: 4443.520000
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...
EOD;
//Parse a YAML string into a PHP array
$parsed = yaml_parse($yaml);
//Comparison test
$actual = json_encode($parsed);
$expect = '{"invoice":34843,"date":"2001-01-23","bill-to":{"given":"Chris","family":"Dumars","address":{"lines":"458 Walkman Dr.\n Suite #292","city":"Royal Oak","state":"MI","postal":48046}},"ship-to":{"given":"Chris","family":"Dumars","address":{"lines":"458 Walkman Dr.\n Suite #292","city":"Royal Oak","state":"MI","postal":48046}},"product":[{"sku":"BL394D","quantity":4,"description":"Basketball","price":450},{"sku":"BL4438H","quantity":1,"description":"Super Hoop","price":2392}],"tax":251.42,"total":4443.52,"comments":"Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338."}';
echo '- Function test ... ', ($expect === $actual) ? 'OK' : 'NG', PHP_EOL;
Recommended Posts