concept-labs / http
(C)oncept-Labs HTTP application
Installs: 39
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/concept-labs/http
Requires
- php: >=8.2
- concept-labs/event-dispatcher: ^1
- concept-labs/http-message: ^1
- concept-labs/singularity: ^1
- psr/http-server-middleware: ^1
README
A low-level, PSR-compliant HTTP application foundation built on configuration-driven architecture and integrated with the Singularity DI container. Part of the Concept-Labs ecosystem.
๐ฏ Philosophy
The Concept-Labs HTTP ecosystem embraces the principles of:
- Configuration-Driven Architecture: Build entirely different applications (REST APIs, web apps, CLI runners) through configuration alone. See Configuration Package for the powerful configuration system.
- Dependency Injection First: Fully integrated with Singularity DI Container
- PSR Standards: Built on PSR-7 (HTTP Messages), PSR-11 (Container), and PSR-15 (HTTP Handlers)
- Lazy Initialization: Middleware wrappers ensure components are only instantiated when actually needed
- Low-Level Foundation: Provides core HTTP handling that higher-level packages build upon (like simple-http)
- SOLID Principles: Clean, maintainable, and testable code architecture
Concept-Labs Ecosystem
This package is part of the Concept-Labs ecosystem and leverages:
- Singularity: Advanced PSR-11 DI container with lifecycle management
- Config: Powerful configuration system enabling configuration-driven application building
- HTTP Message: PSR-7 compliant HTTP message implementation
- Event Dispatcher: PSR-14 event dispatcher for application events
This is a low-level HTTP application foundation. For higher-level applications, see:
- simple-http: Higher-level HTTP application with additional features
โจ Key Features
- ๐ Bootstrap System: Simple application bootstrapping with minimal configuration
- ๐ญ Middleware Pipeline: PSR-15 middleware with priority-based ordering and lazy initialization
- ๐๏ธ Dependency Injection: Full integration with Singularity DI container
- โ๏ธ Configuration System: Build different applications through configuration (see Config Package)
- ๐ Extensible: Low-level foundation for higher-level packages
- ๐งช Fully Tested: Comprehensive test coverage with PEST
- ๐ฆ PSR Compliant: Follows PHP-FIG standards
๐ Requirements
- PHP: >= 8.2
- Extensions: Standard PHP extensions
- Dependencies:
concept-labs/singularity
: ^1concept-labs/http-message
: ^1concept-labs/event-dispatcher
: ^1psr/http-server-middleware
: ^1
๐ฆ Installation
Install via Composer:
composer require concept-labs/http
๐ Quick Start
Basic Application
<?php require_once 'vendor/autoload.php'; use Concept\Http\Bootstrap; // Create bootstrap instance with configuration $bootstrap = new Bootstrap( base: __DIR__, configSource: 'config/*.json' ); // Create and run application $app = $bootstrap->app(); $app->run();
Configuration-Driven Setup
The power of this ecosystem lies in its configuration-driven architecture. Your entire application is defined through configuration:
Create config/app.json
:
{ "app": { "name": "My Application", "debug": true }, "middleware": { "custom": { "preference": "App\\Middleware\\CustomMiddleware", "priority": 100 } } }
Note: All middleware is provided by your application - this package only provides the foundation and middleware pipeline infrastructure.
For routing functionality, see the Router Documentation (optional feature).
Custom Middleware
Create your own middleware to handle requests:
<?php namespace App\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class CustomMiddleware implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { // Process request return $handler->handle($request); } }
Important: Define middleware in configuration (preferred) rather than using addMiddleware()
method. Configuration-based approach allows for better DI integration and lazy initialization.
๐ Documentation
- Installation Guide - Detailed installation and setup
- Configuration - Configuration system reference (see also Config Package)
- Middleware - Middleware pipeline and lazy initialization
- Dependency Injection - DI container integration
- Router (Optional) - Built-in routing middleware (optional feature)
- Examples - Real-world usage examples
- API Reference - Complete API documentation
๐๏ธ Architecture
Core Components
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Bootstrap โ
โ โข Initializes container and configuration โ
โ โข Creates application instance โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HttpApp โ
โ โข Manages middleware stack with lazy wrappers โ
โ โข Processes HTTP requests through pipeline โ
โ โข Handles response generation โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Middleware Pipeline โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ Your Custom โโ โ Your Handler โ โ
โ โ Middleware โ โ (Optional) โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ
โ Note: All middleware is developer-provided โ
โ This package provides infrastructure only โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Lazy Middleware Initialization
Middleware uses a wrapper pattern for lazy initialization:
- Middleware is not instantiated until actually needed in the request chain
- If a middleware returns a response early, subsequent middleware is never created
- Improves performance by avoiding unnecessary object creation
- Automatic through the MiddlewareWrapper system
See Middleware Guide for details.
๐งช Testing
Run the test suite:
# Run all tests ./vendor/bin/pest # Run with coverage ./vendor/bin/pest --coverage # Run specific test file ./vendor/bin/pest tests/Unit/Router/RouteTest.php
Test Coverage
- โ Bootstrap initialization and configuration loading
- โ HTTP application middleware management
- โ Routing with dynamic parameters
- โ Middleware aggregation and priority ordering
- โ Request handler stack processing
๐ง Conventions
Code Style
- PSR-12: Extended coding style guide
- PSR-4: Autoloading standard
- Named Arguments: Leverage PHP 8.2+ features
- Type Declarations: Strict types enabled
Configuration
- Use JSON format for all configuration files
- Use package-level configuration in
concept.json
for DI bindings - Use application configuration in
config/*.json
for app settings - Leverage configuration directives (@import, @include)
- Use context variables with
${VAR}
syntax and environment variables with@env()
directive
Dependency Injection
- Use constructor injection for dependencies
- Implement Injectable interface for automatic DI
- Define preferences in
concept.json
for interface bindings - Use shared: true for singleton services
- Avoid service locator anti-pattern
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
# Clone repository git clone https://github.com/Concept-Labs/http.git cd http # Install dependencies composer install # Run tests ./vendor/bin/pest
๐ Examples
Full Application Example
<?php require_once 'vendor/autoload.php'; use Concept\Http\Bootstrap; // Bootstrap with configuration $bootstrap = new Bootstrap( base: __DIR__, configSource: 'config/*.json' ); // Get application $app = $bootstrap->app(); // Add runtime middleware (optional) $app->addMiddleware( new App\Middleware\LoggingMiddleware(), priority: 200 ); // Run application $app->run();
See docs/examples.md for more comprehensive examples.
๐ Quick Links
- GitHub: Concept-Labs/http
- Singularity DI: Concept-Labs/singularity
- Issues: Report Bug
- Discussions: Community Forum
โ FAQs
Q: What makes this different from other PHP frameworks?
A: This is not a framework - it's a low-level HTTP application ecosystem. It provides the foundation (middleware pipeline, DI integration, configuration system) that higher-level packages build upon. Your application is entirely configuration-driven.
Q: Do I need to use the Router?
A: No! The router is an optional middleware component included in this package. You can build applications without it or use your own routing solution.
Q: Where's the Auth middleware, CORS, etc.?
A: This package provides only the infrastructure. All middleware (auth, CORS, validation, etc.) is developer-provided. This keeps the package lean and flexible.
Q: Configuration vs addMiddleware()?
A: Configuration is preferred. It enables:
- Lazy initialization through middleware wrappers
- Better DI integration (can use interface IDs as preferences)
- Configuration-driven application building
- The
addMiddleware()
method is available for runtime scenarios
Q: Is it production-ready?
A: Yes! The package is fully tested, follows PSR standards, and SOLID principles. It's designed as a foundation for production applications.
๐ License
Copyright (c) 2025 Concept-Labs
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
๐ Support
If you find this project useful, please consider:
- โญ Starring the repository
- ๐ Reporting bugs and issues
- ๐ก Suggesting new features
- ๐ค Contributing code or documentation
Built with โค๏ธ by Concept-Labs