Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #10

Merged
merged 24 commits into from
Dec 9, 2023
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
*.md text eol=lf
*.gitattributes text eol=lf
*.gitignore text eol=lf
*.php text eol=lf
*.env text eol=lf
9 changes: 8 additions & 1 deletion .github/workflows/build-app-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ on:
inputs:
push:
description: 'Push Image (true or false)'
type: choice
options:
- "true"
- "false"
required: true
default: 'true'
run_tests:
description: 'Run unit tests (true or false)'
type: choice
options:
- "true"
- "false"
required: true
default: 'true'

Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ node_modules
.mysql
.phpunit.result.cache
TODO

thunder-tests

.secrets
.env
ignore.txt
8 changes: 8 additions & 0 deletions .idea/runConfigurations/down_local_dev.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/runConfigurations/openapi.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/runConfigurations/up_local_dev.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "PHPUnit Debug (Current file)",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/vendor/bin/phpunit",
"args": [
"${file}"
],
"cwd": "${workspaceFolder}",
"port": 9003,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"APP_ENV": "test",
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
}
]
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Joao Gilberto Magalhaes

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.
59 changes: 35 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Boilerplate Project Template for RESTFul API

[![Build Status](https://github.com/byjg/php-rest-template/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-rest-template/actions/workflows/phpunit.yml)
[![Build Status](https://github.com/byjg/php-rest-template/actions/workflows/build-app-image.yml/badge.svg?branch=master)](https://github.com/byjg/php-rest-template/actions/workflows/build-app-image.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-rest-template/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-rest-template.svg)](https://opensource.byjg.com/opensource/licensing.html)
Expand All @@ -9,7 +9,34 @@

This project is a boilerplate for create Rest Applications API Ready to Use with the best techniques to improve your productivity.

## Features
## What is a PHP Rest Template?

It is a PHP-based RESTful API template or boilerplate that aims to simplify the development process of RESTful web services in PHP.
It provides a foundation or starting point for building APIs following REST architectural principles.

Using PHP Rest Template you can focus on the business logic of your application and not in the infrastructure as for example:

- Rapid Development: By offering a pre-defined structure and essential components, the template can expedite the process of building RESTful APIs in PHP.
- Standardization: The template promotes consistency and adherence to RESTful design principles, making it easier for developers to understand and collaborate on the codebase.
- Customizability: Developers can modify and extend the template to fit their specific requirements, allowing for flexibility in implementing additional features or business logic.

Key features and components:

- Uses [OpenAPI](https://swagger.io/specification/) specification for API documentation and endpoint creation.
- Routing: Includes a routing system that helps map incoming HTTP requests to specific API endpoints or resources.
- Middleware: It allows developers to add custom logic or perform operations before and after the request is processed.
- Handling: The project offer utilities to handle and parse incoming requests, extract parameters, and handle request methods (GET, POST, PUT, DELETE, etc.).
- Response Formatting: It provide mechanisms to format and structure API responses, including JSON serialization, error handling, and status codes.
- Authentication and Authorization: The template include support for implementing authentication and authorization mechanisms to secure API endpoints using JWT.
- Database Integration: It offer integration for connecting to databases, executing queries, and managing data persistence.
- Error Handling: The project include error handling mechanisms to properly handle and format error responses.
- Dependency Injection: It include support for dependency injection and inversion of control (IoC) containers.
- Testing: It include support for testing the API endpoints and resources, including unit testing and functional testing.
- PHP Standards: PSR-7 (Http Message Interface), PSR-11 (Container), PSR-16 and PSR-6 (Cache Interface) and others.

This project is not a framework. It is a template that you can use to create your own project. You can use the template as a starting point for your own project and customize it to fit your specific requirements.

## Some Features Explained

This project install the follow components (click on the link for more details):

Expand All @@ -21,30 +48,14 @@ This project install the follow components (click on the link for more details):
- [Database Migration](docs/migration.md)
- [Database ORM](docs/orm.md)

## Install

### Requirements

This project requires in order to run:

- PHP
- composer

### Create a Project from the Stable Release

```bash
composer create-project byjg/resttemplate YOURPATH 4.9.*
```

### Create a Project from Development Release

In the case you want to use the development version you can use the following command:
## Getting Started

```bash
composer -sdev create-project byjg/resttemplate YOURPATH master
```
Here some examples of how to use the template:

Use instead of `master` branch the feature branch version like `5.0.0.x-dev`
- [Getting Started, Installing and create a new project](docs/getting_started.md)
- [Add a new Table](docs/getting_started_01_create_table.md)
- [Add a new Field](docs/getting_started_02_add_field.md)
- [Add a new Rest Method](docs/getting_started_03_add_rest_method.md)

----
[Open source ByJG](http://opensource.byjg.com)
20 changes: 13 additions & 7 deletions builder/BaseScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Builder;

use ByJG\Config\Exception\ConfigException;
use ByJG\Config\Exception\ConfigNotFoundException;
use ByJG\Config\Exception\EnvironmentException;
use ByJG\Config\Exception\DependencyInjectionException;
use ByJG\Config\Exception\InvalidDateException;
use ByJG\Config\Exception\KeyNotFoundException;
use Closure;
use Psr\SimpleCache\InvalidArgumentException;
Expand All @@ -13,14 +15,14 @@
class BaseScripts
{
protected $workdir;
protected $systemOs;
protected string $systemOs;

public function __construct()
{
$this->workdir = realpath(__DIR__ . '/..');
}

public function getSystemOs()
public function getSystemOs(): string
{
if (!$this->systemOs) {
$this->systemOs = php_uname('s');
Expand Down Expand Up @@ -48,13 +50,15 @@ public function fixDir($command)
* @param string|array $cmd : command to be executed
* @return array exit_status : exit status of the executed command
* output : console output of the executed command
* @throws ConfigException
* @throws ConfigNotFoundException
* @throws EnvironmentException
* @throws DependencyInjectionException
* @throws InvalidArgumentException
* @throws InvalidDateException
* @throws KeyNotFoundException
* @throws ReflectionException
*/
protected function liveExecuteCommand($cmd)
protected function liveExecuteCommand($cmd): ?array
{
// while (@ ob_end_flush()); // end all output buffers if any

Expand Down Expand Up @@ -102,12 +106,14 @@ protected function liveExecuteCommand($cmd)

/**
* @param string|Closure $variableValue
* @return mixed
* @return array|Closure|string|string[]
* @throws ConfigNotFoundException
* @throws EnvironmentException
* @throws DependencyInjectionException
* @throws InvalidArgumentException
* @throws KeyNotFoundException
* @throws ReflectionException
* @throws ConfigException
* @throws InvalidDateException
*/
protected function replaceVariables($variableValue)
{
Expand Down
11 changes: 8 additions & 3 deletions builder/PostCreateScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql

$composerName = strtolower($composerName);
$composerParts = explode("/", $composerName);
$phpVersionMSimple = str_replace(".", "", $phpVersion);

// ------------------------------------------------
//Replace composer name:
Expand All @@ -45,13 +46,15 @@ public function execute($workdir, $namespace, $composerName, $phpVersion, $mysql

// ------------------------------------------------
// Replace Docker PHP Version
$files = [ 'docker/Dockerfile', 'docker/Dockerfile' ];
$files = [ 'docker/Dockerfile' ];
foreach ($files as $file) {
$contents = file_get_contents("$workdir/$file");
$contents = str_replace('ENV TZ=UTC', "ENV TZ=$timezone", $contents);
$contents = str_replace('php:8.1-fpm', "php:$phpVersion-fpm", $contents);
$contents = str_replace('php81', "php$phpVersionMSimple", $contents);
file_put_contents(
"$workdir/$file",
str_replace('FROM byjg/php:7.4-fpm-nginx', "FROM byjg/php:$phpVersion-fpm-nginx", $contents)
$contents
);
}

Expand Down Expand Up @@ -118,13 +121,15 @@ public static function run(Event $event)
$workdir = realpath(__DIR__ . '/..');
$stdIo = $event->getIO();

$currentPhpVersion = PHP_MAJOR_VERSION . "." .PHP_MINOR_VERSION;

$stdIo->write("========================================================");
$stdIo->write(" Setup Project");
$stdIo->write(" Answer the questions below");
$stdIo->write("========================================================");
$stdIo->write("");
$stdIo->write("Project Directory: " . $workdir);
$phpVersion = $stdIo->ask('PHP Version [7.4]: ', '7.4');
$phpVersion = $stdIo->ask("PHP Version [$currentPhpVersion]: ", $currentPhpVersion);
$namespace = $stdIo->ask('Project namespace [MyRest]: ', 'MyRest');
$composerName = $stdIo->ask('Composer name [me/myrest]: ', 'me/myrest');
$mysqlConnection = $stdIo->ask('MySQL connection DEV [mysql://root:mysqlp455w0rd@mysql-container/mydb]: ', 'mysql://root:mysqlp455w0rd@mysql-container/mydb');
Expand Down
Loading