Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json_path: coveralls-upload.json
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: composer
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 10
versioning-strategy: increase
49 changes: 49 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Tests

on: [push, pull_request]

jobs:
tests:
name: Tests PHP ${{ matrix.php }}
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
strategy:
fail-fast: false
matrix:
php: [7.4, 8.0, 8.1]
experimental: [false]
include:
- php: 8.1
analysis: true

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

- name: Set up PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug

- name: Install dependencies with Composer
uses: ramsey/composer-install@v2

- name: Coding standards
if: matrix.analysis
run: vendor/bin/phpcs

- name: Static analysis
if: matrix.analysis
run: vendor/bin/phpstan

- name: Tests
run: vendor/bin/phpunit --coverage-clover clover.xml

- name: Upload coverage results to Coveralls
if: matrix.analysis
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer require php-coveralls/php-coveralls -n -W
vendor/bin/php-coveralls --coverage_clover=clover.xml -v
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea/
.vscode/
/coverage/
/vendor/
/logs/*
!/logs/README.md
.phpunit.result.cache
.DS_Store
19 changes: 19 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Options All -Indexes

<Files .htaccess>
order allow,deny
deny from all
</Files>

<IfModule mod_rewrite.c>
# Redirect to the public folder
RewriteEngine On
# RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

# Redirect to HTTPS
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# How to Contribute

## Pull Requests

1. Fork the Slim Skeleton repository
2. Create a new branch for each feature or improvement
3. Send a pull request from each feature branch to the **4.x** branch

It is very important to separate new features or improvements into separate feature branches, and to send a
pull request for each branch. This allows us to review and pull in new features or improvements individually.

## Style Guide

All pull requests must adhere to the [PSR-12 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-extended-coding-style-guide.md).
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# MTEX Mock API

Simple REST API providing sample JSON data for testing and prototyping.

## Installation

```bash
composer install
```

## Local Development

```bash
php -S localhost:8000 -t public
```

Visit `http://localhost:8000` to see the API documentation.

## Endpoints

### Documentation

- `GET /` - API documentation

### Users

- `GET /users` - List all users
- `GET /users/{id}` - Get user by ID

### Posts

- `GET /posts` - List all posts
- `GET /posts/{id}` - Get post by ID

### Products

- `GET /products` - List all products
- `GET /products/{id}` - Get product by ID

## Example Requests

```bash
curl https://mock.mtex.dev/users
curl https://mock.mtex.dev/users/1
curl https://mock.mtex.dev/posts
curl https://mock.mtex.dev/products/2
```

## Features

- [x] RESTful API design
- [x] JSON responses
- [x] CORS enabled
- [x] Error handling
- [x] Self-documenting
- [x] No database required

## Tech Stack

- PHP 8.1+
- Slim Framework 4
- PSR-7 HTTP Messages

## License

MIT
30 changes: 30 additions & 0 deletions app/dependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
LoggerInterface::class => function (ContainerInterface $c) {
$settings = $c->get(SettingsInterface::class);

$loggerSettings = $settings->get('logger');
$logger = new Logger($loggerSettings['name']);

$processor = new UidProcessor();
$logger->pushProcessor($processor);

$handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']);
$logger->pushHandler($handler);

return $logger;
},
]);
};
10 changes: 10 additions & 0 deletions app/middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use App\Application\Middleware\SessionMiddleware;
use Slim\App;

return function (App $app) {
$app->add(SessionMiddleware::class);
};
14 changes: 14 additions & 0 deletions app/repositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use App\Domain\User\UserRepository;
use App\Infrastructure\Persistence\User\InMemoryUserRepository;
use DI\ContainerBuilder;

return function (ContainerBuilder $containerBuilder) {
// Here we map our UserRepository interface to its in memory implementation
$containerBuilder->addDefinitions([
UserRepository::class => \DI\autowire(InMemoryUserRepository::class),
]);
};
27 changes: 27 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use App\Application\Actions\User\ListUsersAction;
use App\Application\Actions\User\ViewUserAction;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;

return function (App $app) {
$app->options('/{routes:.*}', function (Request $request, Response $response) {
// CORS Pre-Flight OPTIONS Request Handler
return $response;
});

$app->get('/', function (Request $request, Response $response) {
$response->getBody()->write('Hello world!');
return $response;
});

$app->group('/users', function (Group $group) {
$group->get('', ListUsersAction::class);
$group->get('/{id}', ViewUserAction::class);
});
};
27 changes: 27 additions & 0 deletions app/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use App\Application\Settings\Settings;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder) {

// Global Settings Object
$containerBuilder->addDefinitions([
SettingsInterface::class => function () {
return new Settings([
'displayErrorDetails' => true, // Should be set to false in production
'logError' => false,
'logErrorDetails' => false,
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
],
]);
}
]);
};
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "mtex/mock-api",
"description": "Simple REST API providing sample JSON data",
"type": "project",
"require": {
"php": "^8.1",
"slim/slim": "^4.12",
"slim/psr7": "^1.6"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
Loading
Loading