summaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
authorZhineng Li <[email protected]>2026-02-14 10:08:30 +0800
committerZhineng Li <[email protected]>2026-02-14 10:08:30 +0800
commiteccd5cd5a067f5659982685afdeea209b1888efb (patch)
tree1626c903b854317be1708ce3ccd3b4f4e6e0fc97 /build
downloadacs-metadata-786a07360cbe6e0241e4703da25d35c59185fb09.tar.gz
acs-metadata-786a07360cbe6e0241e4703da25d35c59185fb09.zip
Diffstat (limited to 'build')
-rw-r--r--build/.gitignore2
-rw-r--r--build/Arr.php26
-rw-r--r--build/Strip.php189
-rw-r--r--build/composer.json12
-rw-r--r--build/main.php31
5 files changed, 260 insertions, 0 deletions
diff --git a/build/.gitignore b/build/.gitignore
new file mode 100644
index 0000000..d1502b0
--- /dev/null
+++ b/build/.gitignore
@@ -0,0 +1,2 @@
+vendor/
+composer.lock
diff --git a/build/Arr.php b/build/Arr.php
new file mode 100644
index 0000000..e9f357d
--- /dev/null
+++ b/build/Arr.php
@@ -0,0 +1,26 @@
+<?php
+
+declare(strict_types=1);
+
+final class Arr
+{
+ /**
+ * Extract the specified keys from the given array.
+ *
+ * @param array<array-key, mixed> $array
+ * @param array<int, array-key> $keys
+ * @return array<array-key, mixed>
+ */
+ public static function only(array $array, array $keys): array
+ {
+ $result = [];
+
+ foreach ($keys as $key) {
+ if (array_key_exists($key, $array)) {
+ $result[$key] = $array[$key];
+ }
+ }
+
+ return $result;
+ }
+}
diff --git a/build/Strip.php b/build/Strip.php
new file mode 100644
index 0000000..1b0bb0a
--- /dev/null
+++ b/build/Strip.php
@@ -0,0 +1,189 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @link https://api.aliyun.com/openmeta/guide
+ * @link https://api.alibabacloud.com/openmeta/guide
+ */
+final class Strip
+{
+ /**
+ * Strip the list of product info.
+ *
+ * @param mixed[][] $products
+ * @return mixed[][]
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/ProductInfo
+ */
+ public static function product(array $products): array
+ {
+ return array_map(static fn (array $product) => Arr::only($product, [
+ 'code', 'style', 'versions', 'defaultVersion',
+ ]), $products);
+ }
+
+ /**
+ * Strip the API docs.
+ *
+ * @param array<string, mixed> $apiDocs
+ * @return array<string, mixed>
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/ApiDocs
+ */
+ public static function apiDocs(array $apiDocs): array
+ {
+ $data = Arr::only($apiDocs, [
+ 'version', 'info', 'components', 'apis', 'endpoints',
+ ]);
+
+ if (isset($data['apis']) && is_array($data['apis'])) {
+ $data['apis'] = array_map(
+ static fn (array $api): array => static::api($api),
+ $data['apis']
+ );
+ }
+
+ if (isset($data['components']['schemas']) && is_array($data['components']['schemas'])) {
+ $data['components']['schemas'] = array_map(
+ static fn (array $schema): array => static::schema($schema),
+ $data['components']['schemas']
+ );
+ }
+
+ return $data;
+ }
+
+ /**
+ * Strip the API data structure.
+ *
+ * @param mixed[] $api
+ * @return mixed[]
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/Api
+ */
+ public static function api(array $api): array
+ {
+ $data = Arr::only($api, [
+ 'path', 'methods', 'schemes', 'security', 'consumes',
+ 'produces', 'deprecated', 'parameters',
+ ]);
+
+ if (isset($data['parameters']) && is_array($data['parameters'])) {
+ $data['parameters'] = array_map(
+ static fn (array $parameter): array => static::parameter($parameter),
+ $data['parameters']
+ );
+ }
+
+ if (isset($api['responses']) && is_array($api['responses']) && static::shouldIncludeResponse($api)) {
+ $data['responses'] = array_map(
+ static fn (array $response): array => static::response($response),
+ $api['responses']
+ );
+ }
+
+ return $data;
+ }
+
+ /**
+ * Strip the parameter data structure.
+ *
+ * @param array<int, mixed[]> $parameter
+ * @return array<int, mixed[]>
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/Parameter
+ */
+ public static function parameter(array $parameter): array
+ {
+ $data = Arr::only($parameter, ['name', 'in', 'style', 'schema']);
+
+ if (isset($data['schema']) && is_array($data['schema'])) {
+ $data['schema'] = static::schema($data['schema']);
+ }
+
+ return $data;
+
+ }
+
+ /**
+ * Strip the schema data structure.
+ *
+ * @param mixed[] $schema
+ * @return mixed[]
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/Schema
+ */
+ public static function schema(array $schema): array
+ {
+ $data = Arr::only($schema, [
+ '$ref', 'type', 'format', 'deprecated', 'required',
+ 'minimum', 'exclusiveMinimum', 'maximum', 'exclusiveMaximum',
+ 'minLength', 'maxLength', 'enum', 'pattern', 'properties',
+ 'additionalProperties', 'items', 'minItems', 'maxItems',
+ ]);
+
+ if (isset($data['properties']) && is_array($data['properties'])) {
+ $data['properties'] = array_map(static fn (array $property) => static::schema($property), $data['properties']);
+ }
+
+ if (isset($data['additionalProperties']) && is_array($data['additionalProperties'])) {
+ $data['additionalProperties'] = static::schema($data['additionalProperties']);
+ }
+
+ if (isset($data['items']) && is_array($data['items'])) {
+ $data['items'] = static::schema($data['items']);
+ }
+
+ return $data;
+ }
+
+ /**
+ * Strip the response data structure.
+ *
+ * @param mixed[] $response
+ * @return mixed[]
+ *
+ * @link https://api.alibabacloud.com/openmeta/struct/Response
+ */
+ public static function response(array $response): array
+ {
+ $data = Arr::only($response, ['schema']);
+
+ if (isset($data['schema']) && is_array($data['schema'])) {
+ $data['schema'] = static::schema($data['schema']);
+ }
+
+ return $data;
+ }
+
+ /**
+ * Determine whether to include the response data structure.
+ *
+ * @param mixed[] $api
+ */
+ private static function shouldIncludeResponse(array $api): bool
+ {
+ // Response schemas are required to correctly parse XML bodies
+ // and ensure consistent data structures.
+ return static::consumesXml($api);
+ }
+
+ /**
+ * Determine whether the API consumes XML content.
+ *
+ * @param mixed[] $api
+ */
+ private static function consumesXml(array $api): bool
+ {
+ if (isset($api['consumes']) && is_array($api['consumes'])) {
+ foreach ($api['consumes'] as $contentType) {
+ if (is_string($contentType) && str_contains($contentType, 'xml')) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/build/composer.json b/build/composer.json
new file mode 100644
index 0000000..07a548e
--- /dev/null
+++ b/build/composer.json
@@ -0,0 +1,12 @@
+{
+ "require": {
+ "php": "^8.4",
+ "symfony/var-exporter": "^8.0"
+ },
+ "autoload": {
+ "classmap": [
+ "Arr.php",
+ "Strip.php"
+ ]
+ }
+}
diff --git a/build/main.php b/build/main.php
new file mode 100644
index 0000000..0446be3
--- /dev/null
+++ b/build/main.php
@@ -0,0 +1,31 @@
+<?php
+
+declare(strict_types=1);
+
+use Symfony\Component\VarExporter\VarExporter;
+
+require_once __DIR__.'/vendor/autoload.php';
+
+$path = __DIR__.'/../data/products.php';
+$products = Strip::product(require $path);
+
+$contents = sprintf('<?php return %s;'.PHP_EOL, VarExporter::export($products));
+if (file_put_contents($path, $contents, LOCK_EX) === false) {
+ throw new \RuntimeException(sprintf('Failed to write file "%s".', $path));
+}
+
+foreach ($products as $product) {
+ foreach ($product['versions'] as $version) {
+ printf('==> Processing %s %s'.PHP_EOL, $product['code'], $version);
+
+ $apiDocsPath = sprintf('%s/%s/%s/api-docs.php', __DIR__.'/../data', $product['code'], $version);
+ $apiDocs = Strip::apiDocs(require $apiDocsPath);
+
+ $contents = sprintf('<?php return %s;'.PHP_EOL, VarExporter::export($apiDocs));
+ if (file_put_contents($apiDocsPath, $contents, LOCK_EX) === false) {
+ throw new \RuntimeException(sprintf('Failed to write file "%s".', $apiDocsPath));
+ }
+ }
+}
+
+printf('==> Done!'.PHP_EOL);