Skip to content

Commit cd8e2e2

Browse files
Initial commit
0 parents  commit cd8e2e2

15 files changed

+632
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.phpunit.cache
2+
/vendor
3+
composer.phar
4+
composer.lock
5+
.DS_Store
6+
Thumbs.db
7+
/phpunit.xml
8+
/.idea
9+
/.fleet
10+
/.vscode
11+
.phpunit.result.cache

.styleci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
php:
2+
preset: laravel
3+
version: 0.1
4+

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-otp` will be documented in this file
4+
5+
## 0.0.1 - 201X-XX-XX
6+
7+
- initial release

CONTRIBUTING.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Please read and understand the contribution guide before creating an issue or pull request.
6+
7+
## Etiquette
8+
9+
This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10+
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11+
extremely unfair for them to suffer abuse or anger for their hard work.
12+
13+
Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14+
world that developers are civilized and selfless people.
15+
16+
It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17+
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18+
19+
## Viability
20+
21+
When requesting or submitting new features, first consider whether it might be useful to others. Open
22+
source projects are used by many developers, who may have entirely different needs to your own. Think about
23+
whether or not your feature is likely to be used by other users of the project.
24+
25+
## Procedure
26+
27+
Before filing an issue:
28+
29+
- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30+
- Check to make sure your feature suggestion isn't already present within the project.
31+
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32+
- Check the pull requests tab to ensure that the feature isn't already in progress.
33+
34+
Before submitting a pull request:
35+
36+
- Check the codebase to ensure that your feature doesn't already exist.
37+
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38+
39+
## Requirements
40+
41+
If the project maintainer has any additional requirements, you will find them listed here.
42+
43+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44+
45+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46+
47+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48+
49+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50+
51+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52+
53+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54+
55+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Mustafa Khaled
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Laravel Fast-API
2+
3+
[![Latest Version](https://img.shields.io/github/v/release/mustafakhaleddev/laravel-fast-api.svg?style=flat-square)](https://github.com/mustafakhaleddev/laravel-fast-api/releases)
4+
[![Issues](https://img.shields.io/github/issues/mustafakhaleddev/laravel-fast-api.svg?style=flat-square)](https://github.com/mustafakhaleddev/laravel-fast-api/issues)
5+
[![License](https://img.shields.io/github/license/mustafakhaleddev/laravel-fast-api.svg?style=flat-square)](https://github.com/mustafakhaleddev/laravel-fast-api/blob/main/LICENSE)
6+
7+
Laravel FastAPI is a PHP attribute-based routing solution for building APIs quickly and efficiently. With FastAPI attributes, you can define routes, methods, and middlewares directly in your controller classes, reducing the need for complex route files and enabling better organization and clarity.
8+
9+
This package also integrates seamlessly with Laravel's `route:cache` for enhanced performance, ensuring your APIs are as fast as possible.
10+
11+
## Features
12+
13+
- **Attribute-Based Routing**: Define your API routes using PHP attributes.
14+
- **Support for Advanced Routing Options**: Middleware, where clauses, route options, and more!
15+
- **Enum-Based HTTP Methods**: Use the predefined `FastApiMethod` for your HTTP methods.
16+
- **API Caching**: Leverage Laravel's `route:cache` for optimal performance.
17+
- **Clear API Cache**: Quickly clear cached API routes with simple Artisan commands.
18+
19+
## Installation
20+
21+
You can install the package via Composer:
22+
23+
```bash
24+
composer require mkd/laravel-fast-api
25+
```
26+
27+
## Usage
28+
29+
### Define FastAPI Routes
30+
31+
Use the `#[FastAPIGroup]` and `#[FastAPI]` attributes to define routes inside your controller classes.
32+
33+
```php
34+
use MKD\FastAPI\Attributes\FastAPI;
35+
use MKD\FastAPI\Attributes\FastAPIGroup;
36+
37+
#[FastAPIGroup(prefix: '/items', options: ['name' => 'items'], middlewares: ['auth'])]
38+
class ItemsController extends Controller
39+
{
40+
#[FastAPI(method: FastApiMethod::GET, path: '/data/{id}', options: ['functions' => [
41+
'whereIn' => ['id', ['2']],
42+
], 'name' => 'item_id'])]
43+
public function getItem($id)
44+
{
45+
return response()->json(['item' => $id]);
46+
}
47+
}
48+
```
49+
50+
This simple attribute-based approach automatically handles routing logic, allowing you to focus on building your API logic.
51+
52+
### Supported Methods and Functions
53+
54+
You can define routes with the following HTTP methods:
55+
56+
```php
57+
enum FastApiMethod
58+
{
59+
case GET;
60+
case POST;
61+
case PUT;
62+
case PATCH;
63+
case OPTION;
64+
case DELETE;
65+
case ANY;
66+
case REDIRECT;
67+
case MATCH;
68+
}
69+
```
70+
71+
In addition, you can leverage these functions to customize your routes:
72+
73+
```php
74+
private array $supportedFunctions = [
75+
'middleware',
76+
'where',
77+
'whereNumber',
78+
'whereAlpha',
79+
'whereAlphaNumeric',
80+
'whereUuid',
81+
'whereUlid',
82+
'whereIn',
83+
'name',
84+
'withTrashed',
85+
'scopeBindings',
86+
'withoutScopedBindings',
87+
];
88+
```
89+
90+
### Configurations
91+
92+
In your configuration file, you can specify paths and controllers to be scanned for FastAPI attributes.
93+
94+
```php
95+
return [
96+
//Paths to check for controllers that use fast-api
97+
'paths' => [
98+
app_path('Http/Controllers'),
99+
],
100+
101+
//Specify controllers that are not included in the paths
102+
'controllers' => [
103+
\App\Http\Controllers\CustomController::class
104+
],
105+
];
106+
```
107+
108+
### Artisan Commands
109+
110+
FastAPI provides useful commands for caching and clearing controllers:
111+
112+
- Cache the controllers for better scanning performance:
113+
114+
```bash
115+
php artisan fast-api:cache
116+
```
117+
118+
- Clear the cached controllers:
119+
120+
```bash
121+
php artisan fast-api:clear-cache
122+
```
123+
124+
### Performance
125+
126+
By using Laravel's `route:cache`, the FastAPI routes are cached to ensure high performance. It is recommended to always cache your routes in production environments for faster API responses.
127+
128+
```bash
129+
php artisan route:cache
130+
```
131+
132+
## Contributing
133+
134+
Feel free to submit issues and pull requests. Contributions are welcome!
135+
136+
## License
137+
138+
The MIT License (MIT). Please see [License File](LICENSE) for more information.
139+
140+
---
141+
142+
This `README.md` gives a clear overview of your package's purpose, usage, features, and installation steps, ensuring potential users and contributors understand it quickly.

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "mkd/laravel-fast-api",
3+
"description": "Laravel package for generating and verifying One-Time Passwords (OTPs) using TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) algorithms.",
4+
"keywords": [
5+
"laravel"
6+
7+
],
8+
"homepage": "https://github.com/mustafakhaleddev/laravel-fast-api",
9+
"license": "MIT",
10+
"type": "library",
11+
"authors": [
12+
{
13+
"name": "Mustafa Khaled",
14+
"email": "mustafakhaled.dev@gmail.com",
15+
"role": "Developer"
16+
}
17+
],
18+
"require": {
19+
"php": "^8.0",
20+
"laravel/framework": "^7.0|^8.0|^9.0|^10.0|^11.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^9.5"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"MKD\\FastAPI\\": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"MKD\\FastAPI\\": "tests/"
33+
}
34+
},
35+
"config": {
36+
"sort-packages": true
37+
},
38+
"extra": {
39+
"laravel": {
40+
"providers": [
41+
"MKD\\FastAPI\\LaravelFastAPIServiceProvider"
42+
],
43+
"aliases": {
44+
45+
}
46+
}
47+
}
48+
}

config/config.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* Fast API Config
5+
*/
6+
7+
8+
return [
9+
10+
//Paths to check for controllers that use fast-api
11+
'paths' => [
12+
app_path('Http/Controllers'),
13+
],
14+
15+
//specify controllers that not included on the paths
16+
'controllers' => [
17+
\App\Http\Controllers\CustomController::class
18+
],
19+
];

src/Attributes/FastAPI.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace MKD\FastAPI\Attributes;
4+
5+
use Attribute;
6+
use MKD\FastAPI\Enums\FastApiMethod;
7+
8+
#[Attribute(Attribute::TARGET_METHOD)]
9+
class FastAPI
10+
{
11+
public FastApiMethod $method;
12+
public string $path;
13+
public ?array $options;
14+
public FastAPIGroup $group;
15+
16+
public function __construct(FastApiMethod $method, string $path, ?array $options = [])
17+
{
18+
$this->method = $method;
19+
$this->path = $path;
20+
$this->options = $options;
21+
}
22+
23+
public function setGroup(FastAPIGroup $group){
24+
$this->group = $group;
25+
}
26+
27+
}

src/Attributes/FastAPIGroup.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace MKD\FastAPI\Attributes;
4+
5+
use Attribute;
6+
use MKD\FastAPI\Enums\FastApiMethod;
7+
8+
#[Attribute(Attribute::TARGET_CLASS)]
9+
class FastAPIGroup
10+
{
11+
public string $prefix;
12+
public array $middlewares;
13+
public array $options;
14+
15+
public function __construct(string $prefix = '', array $middlewares = [], array $options = [])
16+
{
17+
$this->prefix = $prefix;
18+
$this->middlewares = $middlewares;
19+
$this->options = $options;
20+
}
21+
22+
}

0 commit comments

Comments
 (0)