Skip to content

Commit

Permalink
Introduce new API library
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 17, 2023
1 parent 118bdc9 commit 8350ceb
Show file tree
Hide file tree
Showing 71 changed files with 2,630 additions and 104 deletions.
120 changes: 120 additions & 0 deletions .docs/README.md
@@ -0,0 +1,120 @@
# Contributte API

Powerful, documented, validated, built-in API to Nette Framework.

## Content

- [Setup](Setup)

## Setup

```bash
composer require contributte/api
```

## Configuration

### Minimal configuration

NEON configration:

```neon
extensions:
api: Contributte\Api\DI\ApiExtension
services:
- App\Api\PingController
- App\Api\PingController
```

Router configuration:

```php
namespace App\Domain\Routing;

use App\Api\HelloController;
use Contributte\Api\Router\ApiRouter;
use Nette\Application\Routers\Route;
use Nette\Application\Routers\RouteList;
use Nette\Routing\Router;

class RouterFactory
{

public static function create(): Router
{
$router = new RouteList();

$api = (new ApiRouter($router))->withPath('api');
$api->add('GET', 'hello', HelloController::class);

$router[] = new Route('<presenter>/<action>[/<id>]', 'Home:default');

return $router;
}

}
```

Controller example:

```php
<?php declare(strict_types = 1);

namespace App\Api;

final class HelloController
{

public function __invoke(ApiRequest $request): ResponseInterface
{
return PureResponse::create()->withPayload('Hello!');
}

}
```

### Advanced configuration

```neon
extensions:
api: Contributte\Api\DI\ApiExtension
api:
middlewares:
# Catch & handler errors
- Contributte\Api\Middleware\TracyMiddleware(%debugMode%)
# Format responses
- Contributte\Api\Middleware\NegotiationMiddleware(
Contributte\Api\Formatter\MultiFormatter([
Contributte\Api\Formatter\JsonFormatter()
])
)
# Process authentication & authorization
- Contributte\Api\Middleware\MatchMiddleware(
# skip public routes
skipTags: [public]
# apply firewall middleware
middlewares: [
Contributte\Api\Middleware\FirewallMiddleware(
Contributte\Api\Security\StaticFirewall([
foobar: [user: Foo, role: Bar]
])
)
]
)
# Process controllers
- Contributte\Api\Middleware\DispatcherMiddleware
search:
# Search for controllers defined in app/Api folder
controllers:
in: %appDir%/Api
files: [*Controller.php]
```

### Getting started

## Examples

There is example project [contributte/api-skeleton](https://github.com/contributte/api-skeleton).
16 changes: 16 additions & 0 deletions .editorconfig
@@ -0,0 +1,16 @@
# EditorConfig is awesome: http://EditorConfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = tab
tab_width = 4

[*.{json,yaml,yml,md}]
indent_style = space
indent_size = 2
10 changes: 10 additions & 0 deletions .gitattributes
@@ -0,0 +1,10 @@
.docs export-ignore
.github export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
Makefile export-ignore
README.md export-ignore
phpstan.neon export-ignore
ruleset.xml export-ignore
tests export-ignore
18 changes: 18 additions & 0 deletions .github/workflows/codesniffer.yml
@@ -0,0 +1,18 @@
name: "Codesniffer"

on:
pull_request:
workflow_dispatch:

push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
codesniffer:
name: "Codesniffer"
uses: contributte/.github/.github/workflows/codesniffer.yml@master
with:
php: "8.2"
18 changes: 18 additions & 0 deletions .github/workflows/coverage.yml
@@ -0,0 +1,18 @@
name: "Coverage"

on:
pull_request:
workflow_dispatch:

push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
coverage:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester-coverage-v2.yml@master
with:
php: "8.2"
18 changes: 18 additions & 0 deletions .github/workflows/phpstan.yml
@@ -0,0 +1,18 @@
name: "Phpstan"

on:
pull_request:
workflow_dispatch:

push:
branches: ["*"]

schedule:
- cron: "0 8 * * 1"

jobs:
phpstan:
name: "Phpstan"
uses: contributte/.github/.github/workflows/phpstan.yml@master
with:
php: "8.2"
37 changes: 37 additions & 0 deletions .github/workflows/tests.yml
@@ -0,0 +1,37 @@
name: "Nette Tester"

on:
pull_request:
workflow_dispatch:

push:
branches: [ "*" ]

schedule:
- cron: "0 8 * * 1"

jobs:
test83:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.3"

test82:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.2"

test81:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.1"

testlower:
name: "Nette Tester"
uses: contributte/.github/.github/workflows/nette-tester.yml@master
with:
php: "8.1"
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"
14 changes: 14 additions & 0 deletions .gitignore
@@ -0,0 +1,14 @@
# IDE
/.idea

# Composer
/vendor
/composer.lock

# Tests
/tests/tmp
/coverage.*
/tests/**/*.log
/tests/**/*.html
/tests/**/*.expected
/tests/**/*.actual
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Contributte

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions Makefile
@@ -0,0 +1,34 @@
.PHONY: install
install:
composer update

.PHONY: qa
qa: phpstan cs

.PHONY: cs
cs:
ifdef GITHUB_ACTION
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --extensions="php,phpt" --colors -nsp -q --report=checkstyle src tests | cs2pr
else
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 --extensions="php,phpt" --colors -nsp src tests
endif

.PHONY: csf
csf:
vendor/bin/phpcbf --standard=ruleset.xml --encoding=utf-8 --extensions="php,phpt" --colors -nsp src tests

.PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse -c phpstan.neon

.PHONY: tests
tests:
vendor/bin/tester -s -p php --colors 1 -C tests/Cases

.PHONY: coverage
coverage:
ifdef GITHUB_ACTION
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.xml --coverage-src src tests/Cases
else
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage coverage.html --coverage-src src tests/Cases
endif

0 comments on commit 8350ceb

Please sign in to comment.