Skip to content

Commit

Permalink
[main] Add more concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-scythe committed Apr 30, 2023
1 parent 70fb2c4 commit 19e24d4
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 80 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down Expand Up @@ -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
Expand All @@ -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;
```
```
36 changes: 32 additions & 4 deletions bin/http_generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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());
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/ConstantNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace CryptoScythe\Http\Generator;

final class ConstantNormalizer
{
public static function normalize(string $constant): string
{
return trim(
strtoupper(preg_replace('/[^0-9A-z]/', '_', $constant)),
'_',
);
}
}
2 changes: 2 additions & 0 deletions src/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CryptoScythe\Http\Generator;

use CryptoScythe\Http\Generator\Renderer\RendererInterface;

final class Definition
{
public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function generate(
$conceptLink = $webConcept->id;

foreach ($webConcept->values as $value) {
$constContent = $definition->renderer->renderConstWithContent($value);
$constContent = trim($definition->renderer->renderConstWithContent($value));

$block = implode(
"\n * \n * ",
Expand Down
27 changes: 0 additions & 27 deletions src/HeaderFieldsRenderer.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/MediaTypesRenderer.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CryptoScythe\Http\Generator\Renderer;

use CryptoScythe\Http\Generator\ConstantNormalizer;
use CryptoScythe\Http\Generator\WebConcept\Value;

abstract class AbstractRenderer implements RendererInterface
{
/** @var string */
protected const HEADLINE_INTRO = '';

public function renderHeadline(Value $value): string
{
return trim(sprintf('%s %s', static::HEADLINE_INTRO, $value->value));
}

public function renderConstWithContent(Value $value): string
{
return sprintf(
"public const %s = '%s';",
ConstantNormalizer::normalize($value->value),
$value->value,
);
}
}
8 changes: 8 additions & 0 deletions src/Renderer/AuthenticationSchemesRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CryptoScythe\Http\Generator\Renderer;

class AuthenticationSchemesRenderer extends AbstractRenderer
{
protected const HEADLINE_INTRO = 'Authentication scheme';
}
8 changes: 8 additions & 0 deletions src/Renderer/CacheDirectivesRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CryptoScythe\Http\Generator\Renderer;

final class CacheDirectivesRenderer extends AbstractRenderer
{
protected const HEADLINE_INTRO = 'Cache directive';
}
8 changes: 8 additions & 0 deletions src/Renderer/ContentCodingsRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CryptoScythe\Http\Generator\Renderer;

class ContentCodingsRenderer extends AbstractRenderer
{
protected const HEADLINE_INTRO = 'Content coding';
}
10 changes: 10 additions & 0 deletions src/Renderer/HeaderFieldsRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace CryptoScythe\Http\Generator\Renderer;

final class HeaderFieldsRenderer extends AbstractRenderer
{
protected const HEADLINE_INTRO = 'Header field';
}
10 changes: 10 additions & 0 deletions src/Renderer/MediaTypesRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace CryptoScythe\Http\Generator\Renderer;

final class MediaTypesRenderer extends AbstractRenderer
{
protected const HEADLINE_INTRO = 'Media type';
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace CryptoScythe\Http\Generator;
namespace CryptoScythe\Http\Generator\Renderer;

use CryptoScythe\Http\Generator\WebConcept\Value;

Expand Down
8 changes: 8 additions & 0 deletions src/Renderer/RequestMethodsRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CryptoScythe\Http\Generator\Renderer;

final class RequestMethodsRenderer extends AbstractRenderer
{
protected const HEADLINE_INTRO = 'Request method';
}
Loading

0 comments on commit 19e24d4

Please sign in to comment.