Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 18, 2023
1 parent bdecc08 commit 811ad51
Show file tree
Hide file tree
Showing 46 changed files with 3,703 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions .github/.kodiak.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 1

[merge]
automerge_label = "automerge"
blacklist_title_regex = "^WIP.*"
blacklist_labels = ["WIP"]
method = "rebase"
delete_branch_on_merge = true
notify_on_conflict = true
optimistic_updates = false
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: composer
directory: "/"
schedule:
interval: daily
labels:
- "dependencies"
- "automerge"
17 changes: 17 additions & 0 deletions .github/workflows/codesniffer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "Codesniffer"

on:
pull_request:

push:
branches: ["*"]

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

jobs:
codesniffer:
name: "Codesniffer"
uses: contributte/.github/.github/workflows/codesniffer.yml@master
with:
php: "8.2"
19 changes: 19 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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"
make: "init coverage"
18 changes: 18 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Phpstan"

on:
pull_request:

push:
branches: ["*"]

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

jobs:
phpstan:
name: "Phpstan"
uses: contributte/.github/.github/workflows/phpstan.yml@master
with:
php: "8.2"
make: "init phpstan"
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: "Phpunit"

on:
pull_request:

push:
branches: [ "*" ]

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

jobs:
test83:
name: "Tester"
uses: contributte/.github/.github/workflows/phpunit.yml@master
with:
php: "8.3"
make: "init tests"

test82:
name: "Tester"
uses: contributte/.github/.github/workflows/phpunit.yml@master
with:
php: "8.2"
make: "init tests"

test81:
name: "Tester"
uses: contributte/.github/.github/workflows/phpunit.yml@master
with:
php: "8.1"
make: "init tests"

testlower:
name: "Tester"
uses: contributte/.github/.github/workflows/phpunit.yml@master
with:
php: "8.1"
make: "init tests"
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# IDE
/.idea

# Composer
/vendor
/composer.lock

# Tests
/coverage.xml
82 changes: 82 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
############################################################
# PROJECT ##################################################
############################################################
.PHONY: project
project: install setup

.PHONY: init
init:
cp config/local.neon.example config/local.neon

.PHONY: install
install:
composer install

.PHONY: setup
setup:
mkdir -p var/tmp var/log
chmod +0777 var/tmp var/log

.PHONY: clean
clean:
find var/tmp -mindepth 1 ! -name '.gitignore' -type f,d -exec rm -rf {} +
find var/log -mindepth 1 ! -name '.gitignore' -type f,d -exec rm -rf {} +

############################################################
# DEVELOPMENT ##############################################
############################################################
.PHONY: qa
qa: cs phpstan

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

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

.PHONY: phpstan
phpstan:
vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=512M

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

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

.PHONY: dev
dev:
NETTE_DEBUG=1 NETTE_ENV=dev php -S 0.0.0.0:8000 -t www

.PHONY: build
build:
echo "OK"

############################################################
# DOCKER ###################################################
############################################################
.PHONY: docker-up
docker-up:
docker compose up

############################################################
# DEPLOYMENT ###############################################
############################################################
.PHONY: deploy
deploy:
$(MAKE) clean
$(MAKE) project
$(MAKE) build
$(MAKE) clean
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
![](https://heatbadger.now.sh/github/readme/contributte/api-skeleton/)

<p align=center>
<a href="https://github.com/contributte/api-skeleton/actions"><img src="https://badgen.net/github/checks/contributte/api-skeleton/master"></a>
<a href="https://coveralls.io/r/contributte/api-skeleton"><img src="https://badgen.net/coveralls/c/github/contributte/api-skeleton"></a>
<a href="https://packagist.org/packages/contributte/api-skeleton"><img src="https://badgen.net/packagist/dm/contributte/api-skeleton"></a>
<a href="https://packagist.org/packages/contributte/api-skeleton"><img src="https://badgen.net/packagist/v/contributte/api-skeleton"></a>
</p>
<p align=center>
<a href="https://packagist.org/packages/contributte/api-skeleton"><img src="https://badgen.net/packagist/php/contributte/api-skeleton"></a>
<a href="https://github.com/contributte/api-skeleton"><img src="https://badgen.net/github/license/contributte/api-skeleton"></a>
<a href="https://bit.ly/ctteg"><img src="https://badgen.net/badge/support/gitter/cyan"></a>
<a href="https://bit.ly/cttfo"><img src="https://badgen.net/badge/support/forum/yellow"></a>
<a href="https://contributte.org/partners.html"><img src="https://badgen.net/badge/sponsor/donations/F96854"></a>
</p>

<p align=center>
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>
</p>

<p align=center>
<img src="https://api.microlink.io?url=https%3A%2F%2Fexamples.contributte.org%2Fapi-skeleton%2F&overlay.browser=light&screenshot=true&meta=false&embed=screenshot.url"></img>
</p>

-----

## Goal

Main goal is to provide example of native API to [Nette](https://nette.org).

## Demo

https://examples.contributte.org/api-skeleton/

## Installation

You will need `PHP 8.1+` and [Composer](https://getcomposer.org/).

Create project using composer.

```bash
composer create-project -s dev contributte/api-skeleton acme
```

Now you have application installed. It's time to run it.

## Startup

The easiest way is to use php built-in web server.

```bash
make dev
# php -S 0.0.0.0:8000 -t www
```

Then visit [http://localhost:8000](http://localhost:8000) in your browser.

**API endpoints**

- http://localhost:8000/api/_/ping
- http://localhost:8000/api/_/apidoc
- http://localhost:8000/api/v1/product
- http://localhost:8000/api/v1/product/123456

## Development

See [how to contribute](https://contributte.org/contributing.html) to this package.

This package is currently maintaining by these authors.

<a href="https://github.com/f3l1x">
<img width="80" height="80" src="https://avatars2.githubusercontent.com/u/538058?v=3&s=80">
</a>

-----

Consider to [support](https://contributte.org/partners.html) **contributte** development team. Also thank you for using this project.
57 changes: 57 additions & 0 deletions app/Api/Product/CreateProductController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace App\Api\Product;

use Contributte\Api\Controller\JsonController;
use Contributte\Api\Description\Describer;
use Contributte\Api\Exception\ParsingException;
use Contributte\Api\Http\ApiRequest;
use Contributte\Api\Http\ErrorResponse;
use Contributte\Api\Http\ResponseInterface;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Throwable;

final class CreateProductController extends JsonController
{

public static function schema(): Schema
{
return Expect::structure([
'id' => Expect::int()->required(),
])->castTo(UpdateProductRequest::class);
}

public static function describe(): Describer
{
$d = new Describer();
$d->withPath('/api/v1/product');
$d->withMethods([Describer::METHOD_POST]);
$d->withDescription('Create product');

return $d;
}

public function __invoke(ApiRequest $request): ResponseInterface
{
try {
$entity = $this->parseBody($request, UpdateProductRequest::class);
} catch (ParsingException $e) {
return $e->getResponse();
}

try {
$product = [
'id' => $entity->id,
'name' => 'Test',
];

return UpdateProductResponse::of($product);
} catch (Throwable $e) {
return ErrorResponse::create()
->withStatusCode(400)
->withMessage('Cannot update detail');
}
}

}
10 changes: 10 additions & 0 deletions app/Api/Product/CreateProductRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace App\Api\Product;

class CreateProductRequest
{

public int $id;

}
Loading

0 comments on commit 811ad51

Please sign in to comment.