Skip to content

Commit

Permalink
Docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-kvashnin committed May 7, 2024
1 parent 9c63777 commit fd8f937
Show file tree
Hide file tree
Showing 16 changed files with 650 additions and 134 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build Documentation

on: workflow_dispatch

jobs:
build-docs:
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
deployments: write

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to GH Packages
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Pull builder image
run: docker pull ghcr.io/norvica/docs:v0.1.0

- name: Build Documentation
run: |
docker run --rm -v $(pwd)/docs/content:/app/content -v $(pwd)/docs/config/_default:/app/config/_default -v $(pwd)/docs/public:/app/public -e HUGO_ENVIRONMENT=production -e HUGO_ENV=production ghcr.io/norvica/docs:v0.1.0 npm run build -- --gc --minify --baseURL "https://router.norvica.dev"
- name: Publish
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: ${{ secrets.CLOUDFLARE_PROJECT_NAME }}
directory: $(pwd)/docs/public
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
composer.lock
.phpunit.result.cache
vendor
# docs
docs/public/*
!docs/public/.gitkeep
132 changes: 2 additions & 130 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,138 +3,10 @@
[![Latest Stable Version](https://poser.pugx.org/norvica/pathfinder/v/stable.png)](https://packagist.org/packages/norvica/pathfinder)
[![Checks](https://github.com/norvica/pathfinder/actions/workflows/checks.yml/badge.svg)](https://github.com/norvica/pathfinder/actions/workflows/checks.yml)

**Pathfinder** is a lean and extremely fast PHP router built on the [Trie](https://en.wikipedia.org/wiki/Trie) (or
**Pathfinder** is a lean and fast PHP router built on the [Trie](https://en.wikipedia.org/wiki/Trie) (or
prefix tree) search algorithm. Its design enables quick lookups and is optimized for high performance.

## Install

```bash
composer require norvica/pathfinder
```

## Use

If you'd like to use the lower-level `Pathfinder` class directly, please refer to the
[appropriate documentation section](./doc/pathfinder.md#use). The `Router` class provides a convenient wrapper and performs
matching against PSR-7 `ServerRequestInterface` objects.

If you'd like to use a more low level class `Pathfinder` please refer to an
[appropriate documentation section](./doc/pathfinder.md#use). `Router` class provides a convenience wrapper and is matching
against PSR-7 `ServerRequestInterface`.

To get started, create a `Router` instance, define your routes, and then match your incoming request:

```php
use Norvica\Pathfinder\Router;

// Create a Pathfinder instance
$router = new Router();

// Define your routes using closures
$definitions = static function(Router $router) {
$router->route('POST', '/orders', OrderPostController::class);
$router->route('GET', '/orders/{id}', [OrderGetController::class, '__invoke']);
};

// Load your route definitions
$definitions($router);

// NOTICE: This is an example of request instantiation.
// Use the library of your choice that instantiates PSR-7 request
// (e.g. https://github.com/guzzle/psr7).
$request = ServerRequest::fromGlobals();

// Handling a request
try {
$route = $router->match($request);
$handler = $route->handler();
$parameters = $route->parameters();
// [...] execute matched handler
} catch (\Norvica\Pathfinder\Exception\NotFound $e) {
// [...] handle 404
} catch (\Norvica\Pathfinder\Exception\MethodNotAllowed $e) {
// [...] handle 405
}
```

## Routes Definition

### Basic Route

A basic route is a simple, static path:

```php
$router->route('GET', '/orders', OrderListController::class);
```

### Parameterized Route

To include parameters like a username or ID in the URL:

```php
$router->route('GET', '/orders/{id}', OrderGetController::class);
```

### Regular Expression Constraints

You can add regex constraints to your parameters:

```php
$router->route('GET', '/orders/{id:[0-9]+}', OrderGetController::class);
```

### Optional Parameters

You can define routes with optional segments:

```php
$router->route('GET', '/articles/{id:[0-9]+}[/{slug}]', ArticleGetController::class);
```

### Complex Routes

You can also define more complex routes with multiple parameters:

```php
$router->route('GET', '/course/{year:[0-9]{4}}/{subject:[a-z]+}/{code:[0-9a-f]{4}}', CourseGetController::class);
```

### Enumerated Parameters

For when you have a specific set of acceptable parameter values:

```php
$router->route('GET', '/{language:en|de}/profile', ProfileGetController::class);
```

### Shortcuts

For convenience, `Router` offers shortcut methods for common HTTP request methods.

```php
$router->get('/orders', 'get_orders_handler');
$router->post('/orders', 'post_orders_handler');
$router->put('/orders/{id}', 'put_order_handler');
$router->patch('/orders/{id}', 'patch_order_handler');
$router->delete('/orders/{id}', 'delete_order_handler');
// ...
```

You can use these shortcut methods just like you would use the route method, but without the need to specify the HTTP
method as the first argument.

> [!IMPORTANT]
> The router only facilitates matching `HEAD` requests to `GET` routes when a specific `HEAD` handler is not found.
> Developers must explicitly ensure that `HEAD` method calls always return
> [empty response bodies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD).
## Tests

Run the PHPUnit tests to ensure that the package is functioning as expected:

```bash
./vendor/bin/phpunit --testdox
```
Read more on [https://router.norvica.dev](https://router.norvica.dev)

## References

Expand Down
73 changes: 73 additions & 0 deletions docs/config/_default/hugo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
title = "Pathfinder"
baseurl = "/"
canonifyURLs = false
disableAliases = true
disableHugoGeneratorInject = true
disableKinds = ["taxonomy", "term"]
enableEmoji = true
enableGitInfo = false
enableRobotsTXT = true
languageCode = "en-US"
paginate = 10
rssLimit = 10
summarylength = 20 # 70 (default)

# Multilingual
defaultContentLanguage = "en"
disableLanguages = []
defaultContentLanguageInSubdir = false

copyRight = "Copyright (c) 2024"

[build.buildStats]
enable = true

[outputs]
home = ["HTML", "searchIndex"]
section = ["HTML", "SITEMAP"]

[outputFormats.searchIndex]
mediaType = "application/json"
baseName = "search-index"
isPlainText = true
notAlternative = true

# Add output format for section sitemap.xml
[outputFormats.SITEMAP]
mediaType = "application/xml"
baseName = "sitemap"
isHTML = false
isPlainText = true
noUgly = true
rel = "sitemap"

[sitemap]
changefreq = "monthly"
filename = "sitemap.xml"
priority = 0.5

[caches]
[caches.getjson]
dir = ":cacheDir/:project"
maxAge = -1 # "30m"

#[permalinks]
# docs = "/:sections[1:]/:slug/"

[minify.tdewolff.html]
keepWhitespace = false

[related]
threshold = 80
includeNewer = true
toLower = false
[[related.indices]]
name = "date"
weight = 10

[imaging]
anchor = "Center"
bgColor = "#ffffff"
hint = "photo"
quality = 85
resampleFilter = "Lanczos"
9 changes: 9 additions & 0 deletions docs/config/_default/languages.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[en]
languageName = "English"
contentDir = "content/en"
weight = 10
[en.params]
languageISO = "EN"
languageTag = "en-US"
footer = '<a href="https://getdoks.org/" target="_blank">Doks <svg xmlns="http://www.w3.org/2000/svg" class="ms-1 mb-1 icon icon-tabler icon-tabler-external-link" width="20" height="20" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 6h-6a2 2 0 0 0 -2 2v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-6" /><path d="M11 13l9 -9" /><path d="M15 4h5v5" /></svg></a>'
alertText = ''
33 changes: 33 additions & 0 deletions docs/config/_default/markup.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defaultMarkdownHandler = "goldmark"

[goldmark]
[goldmark.extensions]
linkify = false
[goldmark.parser]
autoHeadingID = true
autoHeadingIDType = "github"
[goldmark.parser.attribute]
block = true
title = true
[goldmark.renderer]
unsafe = true

[highlight]
anchorLineNos = false
codeFences = true
guessSyntax = false
hl_Lines = ''
hl_inline = false
lineAnchors = ''
lineNoStart = 1
lineNos = false
lineNumbersInTable = false
noClasses = false
noHl = false
style = 'monokai'
tabWidth = 2

[tableOfContents]
endLevel = 3
ordered = false
startLevel = 2
22 changes: 22 additions & 0 deletions docs/config/_default/menus/menus.en.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[[main]]
name = "Docs"
url = "/usage/quick-start/"
weight = 10

[[main]]
name = "Issues"
url = "https://github.com/norvica/pathfinder/issues"
post = '<svg xmlns="http://www.w3.org/2000/svg" class="ms-1 mb-1 icon icon-tabler icon-tabler-external-link" width="20" height="20" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 6h-6a2 2 0 0 0 -2 2v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-6" /><path d="M11 13l9 -9" /><path d="M15 4h5v5" /></svg>'
weight = 20

[[social]]
name = "GitHub"
pre = '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-github" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5"></path></svg>'
url = "https://github.com/norvica/pathfinder"
post = '<svg xmlns="http://www.w3.org/2000/svg" class="ms-1 mb-1 icon icon-tabler icon-tabler-external-link" width="20" height="20" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 6h-6a2 2 0 0 0 -2 2v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-6" /><path d="M11 13l9 -9" /><path d="M15 4h5v5" /></svg>'
weight = 30

[[footer]]
name = "MIT License"
url = "/license/"
weight = 10
Loading

0 comments on commit fd8f937

Please sign in to comment.