11<?php
22
33/**
4- * 📁 PivotPHP v1.1.3 - Static Files and Routes
5- *
6- * Demonstrates two distinct approaches:
7- * 1. $app->staticFiles() - Serves actual files from disk using StaticFileManager
8- * 2. $app->static() - Pre-compiled static responses using StaticRouteManager
9- *
4+ * 📁 PivotPHP v2.0.0 - Static Files Serving
5+ *
6+ * Demonstrates static file serving using StaticFileManager from core-routing.
7+ *
108 * 🚀 How to run:
119 * php -S localhost:8000 examples/02-routing/static-files.php
12- *
10+ *
1311 * 🧪 How to test:
1412 * # Static files (served from disk)
1513 * curl http://localhost:8000/public/test.json # File serving
1614 * curl http://localhost:8000/assets/app.css # CSS file
1715 * curl http://localhost:8000/docs/readme.txt # Text file
18- *
19- * # Static routes (pre-compiled responses)
20- * curl http://localhost:8000/api/static/health # Optimized response
21- * curl http://localhost:8000/api/static/version # Static data
22- * curl http://localhost:8000/api/static/metrics # Pre-computed metrics
16+ *
17+ * 📝 Note: $app->static() was removed in v2.0.0
18+ * Use regular routes with HTTP caching headers for static responses instead.
2319 */
2420
2521require_once dirname (__DIR__ , 2 ) . '/vendor/autoload.php ' ;
2622
2723use PivotPHP \Core \Core \Application ;
28- use PivotPHP \Core \Routing \SimpleStaticFileManager ;
2924
3025// 🎯 Create Application
3126$ app = new Application ();
3732 mkdir ($ staticDir . '/css ' , 0755 , true );
3833 mkdir ($ staticDir . '/js ' , 0755 , true );
3934 mkdir ($ staticDir . '/docs ' , 0755 , true );
40-
35+
4136 // Create demo files
4237 file_put_contents ($ staticDir . '/test.json ' , json_encode ([
4338 'message ' => 'This is a static JSON file ' ,
44- 'served_by ' => 'SimpleStaticFileManager ' ,
45- 'framework ' => 'PivotPHP v1.1.3 '
39+ 'served_by ' => 'StaticFileManager (core-routing) ' ,
40+ 'framework ' => 'PivotPHP v2.0.0 '
4641 ], JSON_PRETTY_PRINT ));
47-
42+
4843 file_put_contents ($ staticDir . '/css/app.css ' , "
4944/* Demo CSS file served by PivotPHP */
5045body {
6055 background: white;
6156}
6257 " );
63-
58+
6459 file_put_contents ($ staticDir . '/js/app.js ' , "
6560// Demo JavaScript file served by PivotPHP
66- console.log('PivotPHP v1.1.3 - Static file serving working !');
61+ console.log('PivotPHP v2.0.0 - Modular routing system !');
6762
6863function showFrameworkInfo() {
6964 return {
7065 framework: 'PivotPHP',
71- version: '1.1.3',
72- feature: 'Static file serving',
73- performance: '+116% improvement'
66+ version: '2.0.0',
67+ feature: 'Modular routing with core-routing package'
7468 };
7569}
7670 " );
77-
71+
7872 file_put_contents ($ staticDir . '/docs/readme.txt ' , "
79- PivotPHP v1.1.3 - Static Files Demo
73+ PivotPHP v2.0.0 - Modular Routing
8074
81- This file is served directly by SimpleStaticFileManager .
75+ This file is served directly by StaticFileManager from core-routing package .
8276
83- Features demonstrated :
77+ Features:
8478- Static file serving with proper MIME types
85- - Directory organization
86- - Security (path traversal prevention)
87- - Performance optimization
88- - Integration with main application
89-
90- Static files are served efficiently while maintaining
91- the framework's principle of simplicity over premature optimization.
79+ - Directory organization and security
80+ - Path traversal prevention
81+ - Integration with modular routing system
82+
83+ v2.0.0 Breaking Changes:
84+ - Routing system now uses pivotphp/core-routing
85+ - $ app ->static () method removed (use HTTP caching instead)
86+ - StaticFileManager now from core-routing package
9287 " );
9388}
9489
95- // 🏠 Home route - Static files overview
90+ // 🏠 Home route - Overview
9691$ app ->get ('/ ' , function ($ req , $ res ) {
9792 return $ res ->json ([
98- 'title ' => 'PivotPHP v1.1.3 - Static Files & Routes Demo ' ,
99- 'two_approaches ' => [
100- 'staticFiles() ' => 'Serves actual files from disk using StaticFileManager ' ,
101- 'static() ' => 'Pre-compiled responses using StaticRouteManager for maximum performance '
102- ],
93+ 'title ' => 'PivotPHP v2.0.0 - Static Files Demo ' ,
94+ 'version ' => '2.0.0 ' ,
95+ 'routing ' => 'Modular (pivotphp/core-routing) ' ,
10396 'static_file_serving ' => [
10497 'method ' => '$app->staticFiles() ' ,
10598 'purpose ' => 'Serve actual files from filesystem ' ,
@@ -116,26 +109,16 @@ function showFrameworkInfo() {
116109 'directory_traversal_protection ' => true
117110 ]
118111 ],
119- 'static_routes ' => [
120- 'method ' => '$app->static() ' ,
121- 'purpose ' => 'Pre-computed responses for maximum performance ' ,
122- 'examples ' => [
123- 'GET /api/static/health ' => 'Static health check (pre-compiled) ' ,
124- 'GET /api/static/version ' => 'Static version info (optimized) ' ,
125- 'GET /api/static/metrics ' => 'Static metrics endpoint (cached) '
126- ],
127- 'benefits ' => [
128- 'zero_processing_time ' => true ,
129- 'pre_computed_responses ' => true ,
130- 'maximum_throughput ' => true ,
131- 'minimal_memory_usage ' => true
132- ]
112+ 'breaking_changes ' => [
113+ 'removed ' => '$app->static() method ' ,
114+ 'alternative ' => 'Use regular routes with Cache-Control headers ' ,
115+ 'example ' => '$res->header("Cache-Control", "public, max-age=3600") '
133116 ]
134117 ]);
135118});
136119
137120// 📁 Register static file directories using $app->staticFiles()
138- // This uses StaticFileManager to serve actual files from disk
121+ // This uses StaticFileManager from core-routing to serve actual files from disk
139122try {
140123 // Register /public route to serve files from static-demo directory
141124 $ app ->staticFiles ('/public ' , $ staticDir , [
@@ -145,16 +128,16 @@ function showFrameworkInfo() {
145128 'fallthrough ' => true ,
146129 'redirect ' => true
147130 ]);
148-
131+
149132 // Register /assets route for CSS/JS files
150133 $ app ->staticFiles ('/assets ' , $ staticDir , [
151134 'index ' => false ,
152135 'dotfiles ' => 'deny '
153136 ]);
154-
155- // Register /docs route for documentation files
137+
138+ // Register /docs route for documentation files
156139 $ app ->staticFiles ('/docs ' , $ staticDir . '/docs ' );
157-
140+
158141} catch (Exception $ e ) {
159142 // Handle directory registration errors gracefully
160143 $ app ->get ('/static-error ' , function ($ req , $ res ) use ($ e ) {
@@ -166,70 +149,13 @@ function showFrameworkInfo() {
166149 });
167150}
168151
169- // 🚀 Static Routes using $app->static() - Pre-compiled responses
170- // These use StaticRouteManager for maximum performance optimization
171-
172- // Health check with static response
173- $ app ->static ('/api/static/health ' , function ($ req , $ res ) {
174- return $ res ->json ([
175- 'status ' => 'healthy ' ,
176- 'framework ' => 'PivotPHP ' ,
177- 'version ' => '1.1.3 ' ,
178- 'uptime ' => 'demo ' ,
179- 'static_route ' => true ,
180- 'optimized ' => 'StaticRouteManager '
181- ]);
182- }, [
183- 'cache_control ' => 'public, max-age=300 ' // 5 minutes cache
184- ]);
185-
186- // Version info with static response
187- $ app ->static ('/api/static/version ' , function ($ req , $ res ) {
188- return $ res ->json ([
189- 'framework ' => 'PivotPHP Core ' ,
190- 'version ' => '1.1.3 ' ,
191- 'edition ' => 'Performance Optimization & Array Callables ' ,
192- 'php_version ' => PHP_VERSION ,
193- 'features ' => [
194- 'array_callables ' => true ,
195- 'performance_boost ' => '+116% ' ,
196- 'object_pooling ' => true ,
197- 'json_optimization ' => true
198- ],
199- 'static_route ' => true
200- ]);
201- }, [
202- 'cache_control ' => 'public, max-age=3600 ' // 1 hour cache
203- ]);
204-
205- // Metrics with static response
206- $ app ->static ('/api/static/metrics ' , function ($ req , $ res ) {
207- return $ res ->json ([
208- 'framework_metrics ' => [
209- 'throughput_improvement ' => '+116% ' ,
210- 'object_pool_reuse ' => '100% ' ,
211- 'json_operations ' => '505K ops/sec ' ,
212- 'memory_efficiency ' => 'optimized '
213- ],
214- 'static_route_benefits ' => [
215- 'no_dynamic_processing ' => true ,
216- 'pre_computed_response ' => true ,
217- 'minimal_memory_usage ' => true ,
218- 'maximum_throughput ' => true
219- ],
220- 'generated_at ' => date ('c ' ),
221- 'static_route ' => true
222- ]);
223- }, [
224- 'cache_control ' => 'public, max-age=60 ' // 1 minute cache
225- ]);
226-
227- // 📊 Static implementation details
152+ // 📊 Static files information
228153$ app ->get ('/static-info ' , function ($ req , $ res ) {
229154 return $ res ->json ([
230- 'app_staticFiles_method ' => [
155+ 'staticFiles_method ' => [
231156 'purpose ' => 'Serve actual files from disk ' ,
232- 'uses ' => 'StaticFileManager class ' ,
157+ 'package ' => 'pivotphp/core-routing ' ,
158+ 'class ' => 'StaticFileManager ' ,
233159 'api_call ' => '$app->staticFiles( \'/assets \', \'./public/assets \') ' ,
234160 'features ' => [
235161 'mime_type_detection ' => 'Automatic based on file extension ' ,
@@ -244,36 +170,49 @@ function showFrameworkInfo() {
244170 'downloads ' => 'Static downloads '
245171 ]
246172 ],
247- 'app_static_method ' => [
248- 'purpose ' => 'Pre-compiled responses for maximum performance ' ,
249- 'uses ' => 'StaticRouteManager class ' ,
250- 'api_call ' => '$app->static( \'/api/health \', function($req, $res) { ... }) ' ,
251- 'optimization ' => 'Pre-computed responses for maximum performance ' ,
252- 'use_cases ' => [
253- 'health_checks ' => 'Service monitoring endpoints ' ,
254- 'version_info ' => 'Application metadata ' ,
255- 'metrics ' => 'Performance indicators ' ,
256- 'api_status ' => 'Service availability '
257- ],
258- 'benefits ' => [
259- 'zero_processing_time ' => 'Response pre-computed ' ,
260- 'maximum_throughput ' => 'No dynamic processing ' ,
261- 'minimal_memory ' => 'Optimized memory usage ' ,
262- 'cache_friendly ' => 'HTTP cache headers '
173+ 'migration_from_v1 ' => [
174+ 'removed_feature ' => '$app->static() method ' ,
175+ 'reason ' => 'Simplification and modular architecture ' ,
176+ 'alternative ' => 'Use HTTP caching headers with regular routes ' ,
177+ 'example ' => [
178+ 'old ' => '$app->static( \'/health \', fn() => $res->json([...])) ' ,
179+ 'new ' => '$app->get( \'/health \', fn() => $res->header("Cache-Control", "max-age=300")->json([...])) '
263180 ]
264- ],
265- 'when_to_use ' => [
266- 'staticFiles ' => 'When you need to serve actual files (CSS, JS, images, documents) ' ,
267- 'static ' => 'When you have fixed data that never changes (health, version, status) '
268- ],
269- 'performance_comparison ' => [
270- 'staticFiles ' => 'Fast file serving with MIME detection and security ' ,
271- 'static ' => 'Ultra-fast pre-computed responses with zero processing ' ,
272- 'recommendation ' => 'Use static() for data, staticFiles() for files '
273181 ]
274182 ]);
275183});
276184
185+ // Example: Static-like response using HTTP caching (replacement for $app->static())
186+ $ app ->get ('/api/health ' , function ($ req , $ res ) {
187+ return $ res
188+ ->header ('Cache-Control ' , 'public, max-age=300 ' ) // 5 minutes cache
189+ ->json ([
190+ 'status ' => 'healthy ' ,
191+ 'framework ' => 'PivotPHP ' ,
192+ 'version ' => '2.0.0 ' ,
193+ 'routing ' => 'modular (core-routing) ' ,
194+ 'note ' => 'Uses HTTP caching instead of $app->static() '
195+ ]);
196+ });
197+
198+ // Example: Version endpoint with caching
199+ $ app ->get ('/api/version ' , function ($ req , $ res ) {
200+ return $ res
201+ ->header ('Cache-Control ' , 'public, max-age=3600 ' ) // 1 hour cache
202+ ->json ([
203+ 'framework ' => 'PivotPHP Core ' ,
204+ 'version ' => '2.0.0 ' ,
205+ 'edition ' => 'Modular Routing ' ,
206+ 'php_version ' => PHP_VERSION ,
207+ 'routing_package ' => 'pivotphp/core-routing ^1.0 ' ,
208+ 'breaking_changes ' => [
209+ 'modular_routing ' => true ,
210+ 'removed_static_method ' => true ,
211+ 'removed_classes ' => ['StaticRouteManager ' , 'MockRequest ' , 'MockResponse ' ]
212+ ]
213+ ]);
214+ });
215+
277216// 🔧 Static files listing endpoint
278217$ app ->get ('/files/list ' , function ($ req , $ res ) {
279218 return $ res ->json ([
@@ -282,45 +221,11 @@ function showFrameworkInfo() {
282221 '/assets ' => 'CSS and JS files ' ,
283222 '/docs ' => 'Documentation files '
284223 ],
285- 'static_route_examples ' => [
286- '/api/static/health ' ,
287- '/api/static/version ' ,
288- '/api/static/metrics '
224+ 'cached_api_endpoints ' => [
225+ '/api/health ' => 'Health check with 5min cache ' ,
226+ '/api/version ' => 'Version info with 1hr cache '
289227 ],
290- 'note ' => 'Static files are served directly without PHP processing for maximum performance '
291- ]);
292- });
293-
294- // 🧪 Performance comparison endpoint
295- $ app ->get ('/performance/static-vs-dynamic ' , function ($ req , $ res ) {
296- // Simulate dynamic route processing time
297- $ dynamicStart = microtime (true );
298- usleep (100 ); // Simulate some processing time
299- $ dynamicEnd = microtime (true );
300- $ dynamicTime = ($ dynamicEnd - $ dynamicStart ) * 1000 ;
301-
302- // Static route would have ~0ms processing time (pre-computed)
303- $ staticTime = 0.001 ; // Essentially instantaneous
304-
305- return $ res ->json ([
306- 'performance_comparison ' => [
307- 'dynamic_route ' => [
308- 'processing_time_ms ' => round ($ dynamicTime , 3 ),
309- 'benefits ' => ['flexible ' , 'real-time data ' , 'customizable ' ],
310- 'use_cases ' => ['user-specific data ' , 'real-time calculations ' , 'database queries ' ]
311- ],
312- 'static_route ' => [
313- 'processing_time_ms ' => $ staticTime ,
314- 'benefits ' => ['maximum_speed ' , 'minimal_cpu ' , 'cacheable ' ],
315- 'use_cases ' => ['health checks ' , 'version info ' , 'fixed responses ' ]
316- ],
317- 'recommendation ' => 'Use static routes for fixed responses, dynamic for real-time data '
318- ],
319- 'framework_philosophy ' => [
320- 'principle ' => 'Right tool for the right job ' ,
321- 'approach ' => 'Simple, practical solutions over complex optimizations ' ,
322- 'result ' => 'High performance with maintainable code '
323- ]
228+ 'note ' => 'Static files served by StaticFileManager from core-routing package '
324229 ]);
325230});
326231
@@ -332,7 +237,7 @@ function showFrameworkInfo() {
332237 new RecursiveDirectoryIterator ($ staticDir , RecursiveDirectoryIterator::SKIP_DOTS ),
333238 RecursiveIteratorIterator::CHILD_FIRST
334239 );
335-
240+
336241 foreach ($ files as $ fileinfo ) {
337242 $ todo = ($ fileinfo ->isDir () ? 'rmdir ' : 'unlink ' );
338243 $ todo ($ fileinfo ->getRealPath ());
@@ -342,4 +247,4 @@ function showFrameworkInfo() {
342247});
343248
344249// 🚀 Run the application
345- $ app ->run ();
250+ $ app ->run ();
0 commit comments