-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_documentation_example.php
More file actions
99 lines (83 loc) · 3.01 KB
/
api_documentation_example.php
File metadata and controls
99 lines (83 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* 📚 PivotPHP v1.2.0 - API Documentation Example
*
* Demonstrates automatic API documentation generation using ApiDocumentationMiddleware.
* This replaces the deprecated OpenApiExporter with a more integrated approach.
*
* 🚀 Como executar:
* php -S localhost:8080 examples/api_documentation_example.php
*
* 🧪 Como testar:
* curl http://localhost:8080/ # API info
* curl http://localhost:8080/docs # OpenAPI JSON
* curl http://localhost:8080/swagger # Interactive Swagger UI
*
* NOTA: OpenApiExporter foi deprecado em favor do ApiDocumentationMiddleware
*/
require_once __DIR__ . '/../vendor/autoload.php';
use PivotPHP\Core\Core\Application;
use PivotPHP\Core\Middleware\Http\ApiDocumentationMiddleware;
// Create application
$app = new Application();
// Add automatic API documentation middleware
// NOTE: This replaces the deprecated OpenApiExporter class
$app->use(new ApiDocumentationMiddleware([
'docs_path' => '/docs', // JSON endpoint
'swagger_path' => '/swagger', // Swagger UI endpoint
'base_url' => 'http://localhost:8080',
'enabled' => true
]));
// Add some example routes with documentation
$app->get('/users', function($req, $res) {
// Documentation is handled by the middleware automatically
return $res->json([
['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com'],
['id' => 2, 'name' => 'Jane Smith', 'email' => 'jane@example.com']
]);
});
$app->get('/users/:id', function($req, $res) {
// Documentation is handled by the middleware automatically
$userId = $req->param('id');
if ($userId === '1') {
return $res->json(['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com']);
}
return $res->status(404)->json(['error' => 'User not found']);
});
$app->post('/users', function($req, $res) {
// Documentation is handled by the middleware automatically
$userData = $req->getBodyAsStdClass();
// Simulate user creation
$newUser = [
'id' => 3,
'name' => $userData->name ?? 'Unknown',
'email' => $userData->email ?? 'unknown@example.com'
];
return $res->status(201)->json($newUser);
});
$app->get('/products', function($req, $res) {
// Documentation is handled by the middleware automatically
return $res->json([
['id' => 1, 'name' => 'Laptop', 'price' => 999.99],
['id' => 2, 'name' => 'Mouse', 'price' => 29.99]
]);
});
$app->get('/health', function($req, $res) {
// Documentation is handled by the middleware automatically
return $res->json([
'status' => 'healthy',
'timestamp' => date('Y-m-d H:i:s'),
'version' => '1.2.0'
]);
});
// Add info route
$app->get('/', function($req, $res) {
return $res->json([
'message' => 'PivotPHP Core API Documentation Example',
'docs' => 'Visit /swagger for interactive documentation',
'json' => 'Visit /docs for OpenAPI JSON',
'version' => '1.2.0'
]);
});
// Run the application
$app->run();