From 19e24d4e03fce2a28a67d9ae6d5a17d34b450707 Mon Sep 17 00:00:00 2001 From: Chris Fasel Date: Sun, 30 Apr 2023 09:32:34 +0200 Subject: [PATCH] [main] Add more concepts --- CHANGELOG.md | 11 +++ README.md | 69 ++++++++++++++++++- bin/http_generate.php | 36 ++++++++-- composer.json | 2 +- composer.lock | 2 +- src/ConstantNormalizer.php | 14 ++++ src/Definition.php | 2 + src/Generator.php | 2 +- src/HeaderFieldsRenderer.php | 27 -------- src/MediaTypesRenderer.php | 27 -------- src/Renderer/AbstractRenderer.php | 26 +++++++ .../AuthenticationSchemesRenderer.php | 8 +++ src/Renderer/CacheDirectivesRenderer.php | 8 +++ src/Renderer/ContentCodingsRenderer.php | 8 +++ src/Renderer/HeaderFieldsRenderer.php | 10 +++ src/Renderer/MediaTypesRenderer.php | 10 +++ src/{ => Renderer}/RendererInterface.php | 2 +- src/Renderer/RequestMethodsRenderer.php | 8 +++ src/{ => Renderer}/StatusCodeRenderer.php | 20 ++---- src/WebConcept.php | 4 +- 20 files changed, 216 insertions(+), 80 deletions(-) create mode 100644 src/ConstantNormalizer.php delete mode 100644 src/HeaderFieldsRenderer.php delete mode 100644 src/MediaTypesRenderer.php create mode 100644 src/Renderer/AbstractRenderer.php create mode 100644 src/Renderer/AuthenticationSchemesRenderer.php create mode 100644 src/Renderer/CacheDirectivesRenderer.php create mode 100644 src/Renderer/ContentCodingsRenderer.php create mode 100644 src/Renderer/HeaderFieldsRenderer.php create mode 100644 src/Renderer/MediaTypesRenderer.php rename src/{ => Renderer}/RendererInterface.php (83%) create mode 100644 src/Renderer/RequestMethodsRenderer.php rename src/{ => Renderer}/StatusCodeRenderer.php (67%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e022c3..0a194cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.1.0] - 2023-04-30 +### Added +- More concepts: + - AuthenticationSchemes + - CacheDirectives + - ContentCodings + - RequestMethods + +### Changed +- Moved renderer classes into own namespace + ## [2.0.0] - 2023-04-29 ### Added - Changelog diff --git a/README.md b/README.md index 0d635c2..8d3726c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,54 @@ After running the command three files will be created: Every file contains constants which can be used for convenience and looking up the underlying concepts regarding them. +### Example for `AuthenticationSchemes.php` + +```php +/** + * Authentication scheme Basic + * + * The Basic authentication scheme is based on the model that the client needs to authenticate itself with a + * user-id and a password for each protection space ("realm"). The realm value is a free-form string that can + * only be compared for equality with other realms on that server. + * + * @see https://webconcepts.info/specs/IETF/RFC/7617 + * @see https://datatracker.ietf.org/doc/html/rfc7617#section-2 + */ +public const BASIC = 'Basic'; +``` + +### Example for `CacheDirectives.php` + +```php +/** + * Cache directive max-age + * + * The "max-age" request directive indicates that the client is unwilling to accept a response whose age is + * greater than the specified number of seconds. Unless the max-stale request directive is also present, the + * client is not willing to accept a stale response. The "max-age" response directive indicates that the + * response is to be considered stale after its age is greater than the specified number of seconds. + * + * @see https://webconcepts.info/specs/IETF/RFC/7234 + * @see https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.1.1 + */ +public const MAX_AGE = 'max-age'; +``` + +### Example for `ContentCodings.php` + +```php +/** + * Content coding gzip + * + * The "gzip" coding is an LZ77 coding with a 32-bit Cyclic Redundancy Check (CRC) that is commonly produced by + * the gzip file compression program. A recipient SHOULD consider "x-gzip" to be equivalent to "gzip". + * + * @see https://webconcepts.info/specs/IETF/RFC/7230 + * @see https://datatracker.ietf.org/doc/html/rfc7230#section-4.2.3 + */ +public const GZIP = 'gzip'; +``` + ### Example for `HeaderFields.php` ```php /** @@ -65,11 +113,28 @@ public const CONTENT_TYPE = 'Content-Type'; public const APPLICATION_JSON = 'application/json'; ``` +### Example for `RequestMethods.php` + +```php +/** + * Request method GET + * + * The GET method requests transfer of a current selected representation for the target resource. GET is the + * primary mechanism of information retrieval and the focus of almost all performance optimizations. Hence, when + * people speak of retrieving some identifiable information via HTTP, they are generally referring to making a + * GET request. + * + * @see https://webconcepts.info/specs/IETF/RFC/7231 + * @see https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.1 + */ +public const GET = 'GET'; +``` + ### Example for `StatusCodes.php` ```php /** - * Status 404 + * Status code 404 * * The 404 (Not Found) status code indicates that the origin server did not find a current representation for * the target resource or is not willing to disclose that one exists. A 404 status code does not indicate @@ -84,4 +149,4 @@ public const STATUS_404 = 404; public const MESSAGE_404 = 'Not Found'; public const STATUS_NOT_FOUND = self::STATUS_404; public const MESSAGE_NOT_FOUND = self::MESSAGE_404; -``` \ No newline at end of file +``` diff --git a/bin/http_generate.php b/bin/http_generate.php index 4fe3ba1..b4a9ae3 100755 --- a/bin/http_generate.php +++ b/bin/http_generate.php @@ -9,9 +9,13 @@ use CryptoScythe\Http\Generator\Definition; use CryptoScythe\Http\Generator\Generator; -use CryptoScythe\Http\Generator\HeaderFieldsRenderer; -use CryptoScythe\Http\Generator\MediaTypesRenderer; -use CryptoScythe\Http\Generator\StatusCodeRenderer; +use CryptoScythe\Http\Generator\Renderer\AuthenticationSchemesRenderer; +use CryptoScythe\Http\Generator\Renderer\CacheDirectivesRenderer; +use CryptoScythe\Http\Generator\Renderer\ContentCodingsRenderer; +use CryptoScythe\Http\Generator\Renderer\HeaderFieldsRenderer; +use CryptoScythe\Http\Generator\Renderer\MediaTypesRenderer; +use CryptoScythe\Http\Generator\Renderer\RequestMethodsRenderer; +use CryptoScythe\Http\Generator\Renderer\StatusCodeRenderer; set_error_handler( function (int $severity, string $message, string $file, int $line): void { @@ -38,6 +42,24 @@ function (int $severity, string $message, string $file, int $line): void { $outputPath = rtrim(realpath($outputDirectory), '/'); $definitions = [ + new Definition( + 'http-authentication-scheme.json', + 'AuthenticationSchemes', + new AuthenticationSchemesRenderer(), + $namespace, + ), + new Definition( + 'http-cache-directive.json', + 'CacheDirectives', + new CacheDirectivesRenderer(), + $namespace, + ), + new Definition( + 'http-content-coding.json', + 'ContentCodings', + new ContentCodingsRenderer(), + $namespace, + ), new Definition( 'http-header.json', 'HeaderFields', @@ -50,6 +72,12 @@ function (int $severity, string $message, string $file, int $line): void { new MediaTypesRenderer(), $namespace, ), + new Definition( + 'http-method.json', + 'RequestMethods', + new RequestMethodsRenderer(), + $namespace, + ), new Definition( 'http-status-code.json', 'StatusCodes', @@ -95,6 +123,6 @@ function (int $severity, string $message, string $file, int $line): void { echo 'done' . PHP_EOL; } catch (Throwable $error) { - echo 'Error: ' . $error->getMessage() . PHP_EOL; + echo PHP_EOL . 'Error: ' . $error->getMessage() . PHP_EOL; exit($error->getCode()); } diff --git a/composer.json b/composer.json index 5d21945..81b3374 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "crypto_scythe/http_generator", - "version": "2.0.0", + "version": "2.1.0", "description": "HTTP header fields, media types and status codes", "license": "MIT", "keywords": [ diff --git a/composer.lock b/composer.lock index d32d678..dd1bfc7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "62043d5e32f65185df2c374a607ca39b", + "content-hash": "d7e718a8bec3a107901f7500c6f93e99", "packages": [], "packages-dev": [ { diff --git a/src/ConstantNormalizer.php b/src/ConstantNormalizer.php new file mode 100644 index 0000000..e3c5d43 --- /dev/null +++ b/src/ConstantNormalizer.php @@ -0,0 +1,14 @@ +id; foreach ($webConcept->values as $value) { - $constContent = $definition->renderer->renderConstWithContent($value); + $constContent = trim($definition->renderer->renderConstWithContent($value)); $block = implode( "\n * \n * ", diff --git a/src/HeaderFieldsRenderer.php b/src/HeaderFieldsRenderer.php deleted file mode 100644 index b3fd43b..0000000 --- a/src/HeaderFieldsRenderer.php +++ /dev/null @@ -1,27 +0,0 @@ -value); - } - - public function renderConstWithContent(Value $value): string - { - return sprintf( - "public const %s = '%s';", - trim( - strtoupper(preg_replace('/[^0-9A-z]/', '_', $value->value)), - '_', - ), - $value->value, - ); - } -} diff --git a/src/MediaTypesRenderer.php b/src/MediaTypesRenderer.php deleted file mode 100644 index 405bd0f..0000000 --- a/src/MediaTypesRenderer.php +++ /dev/null @@ -1,27 +0,0 @@ -value); - } - - public function renderConstWithContent(Value $value): string - { - return sprintf( - "public const %s = '%s';", - trim( - strtoupper(preg_replace('/[^0-9A-z]/', '_', $value->value)), - '_', - ), - $value->value, - ); - } -} diff --git a/src/Renderer/AbstractRenderer.php b/src/Renderer/AbstractRenderer.php new file mode 100644 index 0000000..ed5a439 --- /dev/null +++ b/src/Renderer/AbstractRenderer.php @@ -0,0 +1,26 @@ +value)); + } + + public function renderConstWithContent(Value $value): string + { + return sprintf( + "public const %s = '%s';", + ConstantNormalizer::normalize($value->value), + $value->value, + ); + } +} diff --git a/src/Renderer/AuthenticationSchemesRenderer.php b/src/Renderer/AuthenticationSchemesRenderer.php new file mode 100644 index 0000000..de36b1b --- /dev/null +++ b/src/Renderer/AuthenticationSchemesRenderer.php @@ -0,0 +1,8 @@ +value); - } + protected const HEADLINE_INTRO = 'Status code'; public function renderConstWithContent(Value $value): string { - $statusCodeConst = trim( - strtoupper(preg_replace('/[^0-9A-z]/', '_', $value->value)), - '_', - ); - $statusMessageConst = trim( - strtoupper(preg_replace('/[^0-9A-z]/', '_', $value->description)), - '_', - ); + $statusCodeConst = ConstantNormalizer::normalize($value->value); + $statusMessageConst = ConstantNormalizer::normalize($value->description); return implode( str_pad("\n", 5, ' '), diff --git a/src/WebConcept.php b/src/WebConcept.php index 19a7e37..148d432 100644 --- a/src/WebConcept.php +++ b/src/WebConcept.php @@ -17,8 +17,8 @@ final class WebConcept public function __construct(array $data) { - $this->id = (string) $data['id']; - $this->registry = (string) $data['registry']; + $this->id = (string) ($data['id'] ?? ''); + $this->registry = (string) ($data['registry'] ?? ''); $this->values = array_map($this->valueFromArray(...), (array) ($data['values'] ?? [])); }