Skip to content

Commit 90ac63c

Browse files
committed
WIP
1 parent bdecc08 commit 90ac63c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3655
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
indent_style = tab
11+
indent_size = tab
12+
tab_width = 4
13+
14+
[{*.json, *.yaml, *.yml, *.md}]
15+
indent_style = space
16+
indent_size = 2

.github/.kodiak.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version = 1
2+
3+
[merge]
4+
automerge_label = "automerge"
5+
blacklist_title_regex = "^WIP.*"
6+
blacklist_labels = ["WIP"]
7+
method = "rebase"
8+
delete_branch_on_merge = true
9+
notify_on_conflict = true
10+
optimistic_updates = false

.github/dependabot.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: composer
4+
directory: "/"
5+
schedule:
6+
interval: daily
7+
labels:
8+
- "dependencies"
9+
- "automerge"

.github/workflows/codesniffer.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: "Codesniffer"
2+
3+
on:
4+
pull_request:
5+
6+
push:
7+
branches: ["*"]
8+
9+
schedule:
10+
- cron: "0 8 * * 1"
11+
12+
jobs:
13+
codesniffer:
14+
name: "Codesniffer"
15+
uses: contributte/.github/.github/workflows/codesniffer.yml@v1

.github/workflows/coverage.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "Coverage"
2+
3+
on:
4+
pull_request:
5+
6+
push:
7+
branches: ["*"]
8+
9+
schedule:
10+
- cron: "0 8 * * 1"
11+
12+
jobs:
13+
coverage:
14+
name: "Phpunit"
15+
uses: contributte/.github/.github/workflows/phpunit-coverage.yml@v1
16+
with:
17+
make: "init coverage"

.github/workflows/phpstan.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "Phpstan"
2+
3+
on:
4+
pull_request:
5+
6+
push:
7+
branches: ["*"]
8+
9+
schedule:
10+
- cron: "0 8 * * 1"
11+
12+
jobs:
13+
phpstan:
14+
name: "Phpstan"
15+
uses: contributte/.github/.github/workflows/phpstan.yml@v1
16+
with:
17+
make: "init phpstan"

.github/workflows/tests.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: "Phpunit"
2+
3+
on:
4+
pull_request:
5+
6+
push:
7+
branches: [ "*" ]
8+
9+
schedule:
10+
- cron: "0 8 * * 1"
11+
12+
jobs:
13+
test82:
14+
name: "Phpunit"
15+
uses: contributte/.github/.github/workflows/phpunit.yml@v1
16+
with:
17+
php: "8.2"
18+
make: "init tests"
19+
20+
test81:
21+
name: "Phpunit"
22+
uses: contributte/.github/.github/workflows/phpunit.yml@v1
23+
with:
24+
php: "8.1"
25+
make: "init tests"
26+
27+
testlower:
28+
name: "Phpunit"
29+
uses: contributte/.github/.github/workflows/phpunit.yml@v1
30+
with:
31+
php: "8.1"
32+
make: "init tests"
33+
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# IDE
2+
.idea
3+
4+
# Composer
5+
/vendor/*
6+
7+
# Apache
8+
!.htaccess
9+
10+
# Nette
11+
/var/m
12+
# Nette config
13+
/config/local.neon

Makefile

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
############################################################
2+
# PROJECT ##################################################
3+
############################################################
4+
.PHONY: project
5+
project: install setup
6+
7+
.PHONY: init
8+
init:
9+
cp config/local.neon.example config/local.neon
10+
11+
.PHONY: install
12+
install:
13+
composer install
14+
15+
.PHONY: setup
16+
setup:
17+
mkdir -p var/tmp var/log
18+
chmod +0777 var/tmp var/log
19+
20+
.PHONY: clean
21+
clean:
22+
find var/tmp -mindepth 1 ! -name '.gitignore' -type f,d -exec rm -rf {} +
23+
find var/log -mindepth 1 ! -name '.gitignore' -type f,d -exec rm -rf {} +
24+
25+
############################################################
26+
# DEVELOPMENT ##############################################
27+
############################################################
28+
.PHONY: qa
29+
qa: cs phpstan
30+
31+
.PHONY: cs
32+
cs:
33+
vendor/bin/codesniffer app tests
34+
35+
.PHONY: csf
36+
csf:
37+
vendor/bin/codefixer app tests
38+
39+
.PHONY: phpstan
40+
phpstan:
41+
vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=512M
42+
43+
.PHONY: tests
44+
tests:
45+
vendor/bin/tester -s -p php --colors 1 -C tests
46+
47+
.PHONY: coverage
48+
coverage:
49+
vendor/bin/tester -s -p phpdbg --colors 1 -C --coverage ./coverage.xml --coverage-src ./app tests
50+
51+
.PHONY: dev
52+
dev:
53+
NETTE_DEBUG=1 NETTE_ENV=dev php -S 0.0.0.0:8000 -t www
54+
55+
.PHONY: build
56+
build:
57+
echo "OK"
58+
59+
############################################################
60+
# DOCKER ###################################################
61+
############################################################
62+
.PHONY: docker-up
63+
docker-up:
64+
docker compose up
65+
66+
############################################################
67+
# DEPLOYMENT ###############################################
68+
############################################################
69+
.PHONY: deploy
70+
deploy:
71+
$(MAKE) clean
72+
$(MAKE) project
73+
$(MAKE) build
74+
$(MAKE) clean

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
![](https://heatbadger.now.sh/github/readme/contributte/api-skeleton/)
2+
3+
<p align=center>
4+
<a href="https://github.com/contributte/api-skeleton/actions"><img src="https://badgen.net/github/checks/contributte/api-skeleton/master"></a>
5+
<a href="https://coveralls.io/r/contributte/api-skeleton"><img src="https://badgen.net/coveralls/c/github/contributte/api-skeleton"></a>
6+
<a href="https://packagist.org/packages/contributte/api-skeleton"><img src="https://badgen.net/packagist/dm/contributte/api-skeleton"></a>
7+
<a href="https://packagist.org/packages/contributte/api-skeleton"><img src="https://badgen.net/packagist/v/contributte/api-skeleton"></a>
8+
</p>
9+
<p align=center>
10+
<a href="https://packagist.org/packages/contributte/api-skeleton"><img src="https://badgen.net/packagist/php/contributte/api-skeleton"></a>
11+
<a href="https://github.com/contributte/api-skeleton"><img src="https://badgen.net/github/license/contributte/api-skeleton"></a>
12+
<a href="https://bit.ly/ctteg"><img src="https://badgen.net/badge/support/gitter/cyan"></a>
13+
<a href="https://bit.ly/cttfo"><img src="https://badgen.net/badge/support/forum/yellow"></a>
14+
<a href="https://contributte.org/partners.html"><img src="https://badgen.net/badge/sponsor/donations/F96854"></a>
15+
</p>
16+
17+
<p align=center>
18+
Website 🚀 <a href="https://contributte.org">contributte.org</a> | Contact 👨🏻‍💻 <a href="https://f3l1x.io">f3l1x.io</a> | Twitter 🐦 <a href="https://twitter.com/contributte">@contributte</a>
19+
</p>
20+
21+
<p align=center>
22+
<img src="https://api.microlink.io?url=https%3A%2F%2Fexamples.contributte.org%2Fsentry-skeleton%2F&overlay.browser=light&screenshot=true&meta=false&embed=screenshot.url"></img>
23+
</p>
24+
25+
-----
26+
27+
## Goal
28+
29+
Main goal is to provide example of native API to [Nette](https://nette.org).
30+
31+
## Demo
32+
33+
https://examples.contributte.org/api-skeleton/
34+
35+
## Installation
36+
37+
You will need `PHP 8.1+` and [Composer](https://getcomposer.org/).
38+
39+
Create project using composer.
40+
41+
```bash
42+
composer create-project -s dev contributte/api-skeleton acme
43+
```
44+
45+
Now you have application installed. It's time to run it.
46+
47+
## Startup
48+
49+
The easiest way is to use php built-in web server.
50+
51+
```bash
52+
# make dev
53+
php -S 0.0.0.0:8000 -t www
54+
```
55+
56+
Then visit [http://localhost:8000](http://localhost:8000) in your browser.
57+
58+
**API endpoints**
59+
- http://localhost:8000/api/v1/ping
60+
- http://localhost:8000/api/v1/users
61+
- http://localhost:8000/api/_/apidoc
62+
63+
## Development
64+
65+
See [how to contribute](https://contributte.org/contributing.html) to this package.
66+
67+
This package is currently maintaining by these authors.
68+
69+
<a href="https://github.com/f3l1x">
70+
<img width="80" height="80" src="https://avatars2.githubusercontent.com/u/538058?v=3&s=80">
71+
</a>
72+
73+
-----
74+
75+
Consider to [support](https://contributte.org/partners.html) **contributte** development team. Also thank you for using this project.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace App\Api\Product;
4+
5+
use Contributte\Api\Controller\JsonController;
6+
use Contributte\Api\Description\Describer;
7+
use Contributte\Api\Exception\ParsingException;
8+
use Contributte\Api\Http\ApiRequest;
9+
use Contributte\Api\Http\ErrorResponse;
10+
use Contributte\Api\Http\ResponseInterface;
11+
use Nette\Schema\Expect;
12+
use Nette\Schema\Schema;
13+
use Throwable;
14+
15+
final class CreateProductController extends JsonController
16+
{
17+
18+
public static function schema(): Schema
19+
{
20+
return Expect::structure([
21+
'id' => Expect::int()->required(),
22+
])->castTo(UpdateProductRequest::class);
23+
}
24+
25+
public static function describe(): Describer
26+
{
27+
$d = new Describer();
28+
$d->withPath('/api/v1/product');
29+
$d->withMethods([Describer::METHOD_POST]);
30+
$d->withDescription('Create product');
31+
32+
return $d;
33+
}
34+
35+
public function __invoke(ApiRequest $request): ResponseInterface
36+
{
37+
try {
38+
$entity = $this->parseBody($request, UpdateProductRequest::class);
39+
} catch (ParsingException $e) {
40+
return $e->getResponse();
41+
}
42+
43+
try {
44+
$product = [
45+
'id' => $entity->id,
46+
'name' => 'Test',
47+
];
48+
49+
return UpdateProductResponse::of($product);
50+
} catch (Throwable $e) {
51+
return ErrorResponse::create()
52+
->withStatusCode(400)
53+
->withMessage('Cannot update detail');
54+
}
55+
}
56+
57+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace App\Api\Product;
4+
5+
class CreateProductRequest
6+
{
7+
8+
public int $id;
9+
10+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace App\Api\Product;
4+
5+
use Contributte\Api\Http\EntityResponse;
6+
7+
class CreateProductResponse extends EntityResponse
8+
{
9+
10+
/**
11+
* @param array<string, scalar> $detail
12+
*/
13+
public static function of(array $detail): self
14+
{
15+
$self = self::create();
16+
$self->payload = $detail;
17+
18+
return $self;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)