Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dminustin committed Jan 4, 2023
0 parents commit 2de6ef7
Show file tree
Hide file tree
Showing 34 changed files with 1,336 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
vendor
composer.lock
.php-cs-fixer.cache
4 changes: 4 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
preset: laravel

disabled:
- single_class_element_per_statement
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to `laravel-api-factory` will be documented in this file

## 1.0.0 - 04 Jan, 2023

- initial release
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Danila Minustin

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.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# All in one Laravel API Factory

[![Latest Version on Packagist](https://img.shields.io/packagist/v/dminustin/laravel-api-factory.svg?style=flat-square)](https://packagist.org/packages/dminustin/laravel-api-factory)

This package helps to fast generate APIs

It makes:
- Controllers
- Actions
- Routes
- Swagger Documentation

### The general philosophy is:

"Actions" contains all functionality, you may want to use it without HTTP requests, for example, in workers

"Controllers" must call "Actions" for any actions and must return an action result. No logic in controllers

Default Directory structure:
```
app
| http
| ApiFactory
| Actions
| Controllers
```

Controllers extends the ApiFactoryController

Actions extends the ApiFactoryAction

You can chane parent classes in stubs


## Todo
- implement middlewares in routes
- extend controller stub
- add postman collection writer

## Installation

You can install the package via composer:

```bash
composer require dminustin/laravel-api-factory
```

## Swagger documentation
```bash
composer require --dev DarkaOnLine/L5-Swagger
```

API Documentation will be available at /api/documentation
```
http://127.0.0.1:8000/api/documentation
```

## Configure
```bash
php artisan vendor:publish --tag=api-factory
```

Change the configuration file config/api-factory.php

You can change ./stubs files
- api_factory_action
- api_factory_controller
- api_factory_router

## Usage

```php
php artisan api:factory
```

### Testing

```bash
composer test
```

### Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
60 changes: 60 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "dminustin/laravel-api-factory",
"description": "All-in-one",
"version": "1.0.0.0",
"keywords": [
"dminustin",
"laravel-api-factory"
],
"homepage": "https://github.com/dminustin/laravel-api-factory",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Danila Minustin",
"email": "danila.minustin@gmail.com",
"role": "Developer"
}
],
"require": {
"php": "^7.4|^8.0",
"illuminate/support": ">=5.1",
"illuminate/database": ">=5.1",
"illuminate/contracts": ">=5.1",
"illuminate/filesystem": ">=5.1",
"illuminate/console": ">=5.1",
"symfony/yaml": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"friendsofphp/php-cs-fixer": "dev-master"
},
"autoload": {
"psr-4": {
"Dminustin\\ApiFactory\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Dminustin\\ApiFactory\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Dminustin\\ApiFactory\\LaravelApiFactoryServiceProvider"
],
"aliases": {
"ApiFactory": "Dminustin\\ApiFactory\\LaravelApiFactoryFacade"
}
}
}
}
51 changes: 51 additions & 0 deletions config/api-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

return [
'generateRoutes' => true,
'generateControllers' => true,
'generateActions' => true,
'generateSwaggerDoc' => true,
'overrideControllers' => false,
'overrideActions' => false,
'routesFile' => 'app/routes/example.yaml',

'outRouteFileName' => 'routes/web.php',

'uriPrefix' => '/api/v1/',

'generatedControllerPathPrefix' => 'app/Http/ApiFactory/Controllers',
'generatedActionPathPrefix' => 'app/Http/ApiFactory/Actions',

'generatedControllerNSPrefix' => 'App/Http/ApiFactory/Controllers',
'generatedActionNSPrefix' => 'App/Http/ApiFactory/Actions',

'generatedControllerNSSuffix' => 'Controller',
'generatedActionNSSuffix' => 'Action',


'middlewares' => [
'auth' => ['auth', 'registered'],
'guest' => ['guest'],
'admin' => ['toleAdmin']
],

//Swagger response definitions
'definitions' => [
'baseResponse' => [
'type' => 'object',
'required' => [
'result',
'data'
],
'properties' => [
'result' => [
'type' => 'boolean',
],
'data' => [
'type' => 'object'
]

]
]
]
];
4 changes: 4 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fix:
./vendor/bin/php-cs-fixer fix ./src
test:
./vendor/bin/phpunit tests
51 changes: 51 additions & 0 deletions routes/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
auth/:
-
path: login
method: post
description: Login into system
middlewares: [guest]
produces:
- application/json
tags: [auth, user]
params:
email: string|required|unique:users,email
password: string|required
responses:
200:
description: Base response
schema:
items:
"$ref": "#/definitions/baseResponse"
-
path: register
middlewares: [guest]
description: User registraction
tags: [auth, user]
produces:
- application/json
method: post
params:
email: string|required
password: string|required
confirm: boolean|required
-
path: reset
middlewares: [guest]
tags: [auth, user]
method: post
params:
email: string|required
user/avatar/:
-
tags: [avatar, user]
path: get
method: get
-
path: update
middlewares: [registered]
headers:
authorization: string|required
tags: [avatar, user]
method: post
params:
avatar: file|required
15 changes: 15 additions & 0 deletions src/Classes/AbstractClasses/AbstractDataConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Dminustin\ApiFactory\Classes\AbstractClasses;

abstract class AbstractDataConverter
{
protected $data;

public function __construct($data)
{
$this->data = $data;
}

abstract public function convert(): mixed;
}
50 changes: 50 additions & 0 deletions src/Classes/AbstractClasses/AbstractGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Dminustin\ApiFactory\Classes\AbstractClasses;

use Dminustin\ApiFactory\Classes\Config;
use Dminustin\ApiFactory\Classes\Logger;

abstract class AbstractGenerator
{
protected Config $config;
protected Logger $log;
protected array $renderingMap = [];

/**
* @var []EndPoint $routes
*/
protected $routes;

public function __construct(Config $config, array $routes, Logger $log)
{
$this->config = $config;
$this->routes = $routes;
$this->log = $log;
}

abstract public function generate();

protected function loadTemplate(string $templateName): string
{
return file_get_contents(base_path('stubs/' . $templateName));
}

protected function writeFile(string $rendered, string $outName)
{
$dir = dirname($outName);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}

file_put_contents($outName, $rendered);
}

protected function render(string $template): string
{
foreach ($this->renderingMap as $key => $value) {
$template = str_replace('%' . $key . '%', $value, $template);
}
return $template;
}
}
17 changes: 17 additions & 0 deletions src/Classes/ApiFactoryAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dminustin\ApiFactory\Classes;

use Illuminate\Http\Request;

class ApiFactoryAction
{
protected array $validationRules = [];
protected array $data = [];

public function checkAndFillRequestData(Request $request)
{
$request->validate($this->validationRules);
$this->data = $request->safe()->only($this->validationRules);
}
}
Loading

0 comments on commit 2de6ef7

Please sign in to comment.