Skip to content

HasPHP is a high-performance PHP framework built on OpenSwoole, designed for building scalable and efficient web applications and APIs. It comes with built-in support for routing, database operations, and template rendering using Twig.

License

Notifications You must be signed in to change notification settings

lutfi-haslab/HasPHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ HasPHP Framework

HasPHP Logo

A modern, high-performance PHP framework with OpenSwoole support

PHP Version OpenSwoole License Build Status

Clean, elegant, and built for speed

Features β€’ Quick Start β€’ Documentation β€’ CLI Tools β€’ Performance


🌟 Why HasPHP?

HasPHP combines the elegance of Laravel with the performance of OpenSwoole, delivering a framework that's both developer-friendly and production-ready. Whether you're building APIs, web applications, or microservices, HasPHP provides the tools you need to ship fast and scale efficiently.

// Clean, expressive routing
Router::get('/users/{id}', 'UserController@show');

// Powerful ORM
$users = User::where('active', 1)->with('posts')->get();

// Built-in API documentation
// Swagger UI automatically generated at /api/docs

πŸš€ Key Features

⚑ High Performance

  • OpenSwoole Integration: 10-50x faster than traditional PHP-FPM
  • Coroutine Support: Non-blocking I/O for maximum concurrency
  • Multi-Worker Architecture: Parallel request processing
  • Memory Persistence: Objects stay in memory between requests

πŸ› οΈ Developer Experience

  • Artisan CLI: Laravel-inspired command-line interface
  • Code Generation: Controllers, models, and migrations
  • Auto Migrations: Database schema versioning made easy
  • Hot Reload: Development server with automatic restarts

πŸ—οΈ Modern Architecture

  • Component-based SPA: React-like frontend architecture
  • RESTful APIs: Built-in API development tools
  • Auto Documentation: Swagger/OpenAPI integration
  • Dependency Injection: Clean, testable code structure

πŸ“¦ Production Ready

  • Docker Support: Container-ready deployment
  • Process Management: Daemon mode with PID management
  • Error Handling: Comprehensive exception handling
  • Logging: Structured logging with multiple drivers

πŸ“‹ Requirements

Component Minimum Recommended
PHP 8.0+ 8.4+
Composer 2.0+ Latest
OpenSwoole Optional 22.0+
Database Any MySQL 8.0+

System Requirements

  • Memory: 512MB+ (2GB+ recommended)
  • Disk: 100MB+ free space
  • OS: Linux, macOS, Windows (WSL2)

πŸš€ Quick Start

1. Installation

# Clone repository
git clone https://github.com/your-org/hasphp.git
cd hasphp

# Install dependencies
composer install

# Set up environment
cp .env.example .env

2. Database Setup

# Create SQLite database (default)
mkdir -p database
touch database/database.sqlite

# Or configure MySQL/PostgreSQL in .env
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_DATABASE=hasphp
# DB_USERNAME=root
# DB_PASSWORD=

3. Start Development Server

# Auto-detect best server (OpenSwoole or PHP built-in)
php artisan serve

# Or specify options
php artisan serve --port=8080 --host=0.0.0.0

4. Visit Your Application


πŸ”§ CLI Tools (Artisan)

HasPHP includes a powerful command-line interface inspired by Laravel's Artisan:

Code Generation

# Generate controllers
php artisan make:controller UserController
php artisan make:controller Api/PostController --api
php artisan make:controller ProductController --resource

# Generate models with relationships
php artisan make:model User --migration
php artisan make:model Post --migration --controller --api

# Generate database migrations
php artisan make:migration create_users_table --create=users
php artisan make:migration add_email_to_users --table=users

Database Management

# Run migrations
php artisan migrate

# Check migration status
php artisan migrate:status

# Rollback migrations
php artisan migrate:rollback
php artisan migrate:rollback --step=3

# Preview queries (dry run)
php artisan migrate --pretend

Server Management

# Development server
php artisan serve                     # Auto-detect best server
php artisan serve --port=9000         # Custom port
php artisan serve --openswoole        # Force OpenSwoole
php artisan serve --fallback          # Force PHP built-in

# Production server
php artisan server:start              # Start production server
php artisan server:start --daemon     # Background daemon mode
php artisan server:stop               # Stop all servers
php artisan server:status             # Show server status

Help & Discovery

# List all commands
php artisan list

# Get help for specific command
php artisan help make:controller
php artisan migrate --help

πŸ—οΈ Architecture

Project Structure

hasphp/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ Controllers/           # HTTP controllers
β”‚   β”œβ”€β”€ Models/               # Eloquent models
β”‚   β”œβ”€β”€ Core/                 # Framework core
β”‚   β”‚   β”œβ”€β”€ CLI/              # Command-line interface
β”‚   β”‚   β”œβ”€β”€ DB/               # Database abstraction
β”‚   β”‚   └── Internal/         # Internal framework classes
β”œβ”€β”€ database/
β”‚   └── migrations/           # Database migrations
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ web.php               # Web routes
β”‚   └── api.php               # API routes
β”œβ”€β”€ views/
β”‚   β”œβ”€β”€ components/           # Reusable components
β”‚   └── layouts/              # Layout templates
β”œβ”€β”€ public/                   # Public assets
β”œβ”€β”€ storage/                  # Logs and cache
β”œβ”€β”€ artisan                   # CLI entry point
└── composer.json             # Dependencies

Request Lifecycle

  1. Router matches incoming requests
  2. Middleware processes requests (CORS, auth, etc.)
  3. Controllers handle business logic
  4. Models interact with database
  5. Views render responses
  6. Response sent back to client

πŸ’Ύ Database

Query Builder

// Fluent query builder
$users = DB::table('users')
    ->where('active', 1)
    ->orderBy('created_at', 'desc')
    ->limit(10)
    ->get();

// Raw queries with bindings
$users = DB::query('SELECT * FROM users WHERE active = ?', [1]);

Eloquent ORM

// Model definition
class User extends Model
{
    protected string $table = 'users';
    protected array $fillable = ['name', 'email'];
    
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

// Usage
$user = User::create(['name' => 'John', 'email' => '[email protected]']);
$posts = User::with('posts')->where('active', 1)->get();

Migrations

// Create migration
php artisan make:migration create_users_table --create=users

// Migration file
class CreateUsersTable
{
    public function up()
    {
        $sql = "CREATE TABLE users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name VARCHAR(255) NOT NULL,
            email VARCHAR(255) UNIQUE NOT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP
        )";
        
        $this->db->query($sql);
    }
}

🌐 Routing & Controllers

Route Definition

// routes/web.php
Router::get('/', 'HomeController@index');
Router::get('/users/{id}', 'UserController@show');
Router::post('/users', 'UserController@store');

// Route groups
Router::group(['prefix' => 'api'], function() {
    Router::get('/posts', 'Api\\PostController@index');
    Router::post('/posts', 'Api\\PostController@store');
});

Controllers

class UserController
{
    public function index($request, $response)
    {
        $users = User::all();
        
        $response->json([
            'success' => true,
            'data' => $users
        ]);
    }
    
    public function show($request, $response, $id)
    {
        $user = User::find($id);
        
        if (!$user) {
            return $response->status(404)->json([
                'success' => false,
                'message' => 'User not found'
            ]);
        }
        
        $response->json([
            'success' => true,
            'data' => $user
        ]);
    }
}

🎨 Frontend Integration

Component-Based SPA

<!-- views/spa.twig -->
<div x-data="spaApp()" x-init="init()">
    <!-- Navigation -->
    <nav>
        <a href="#" @click="navigateTo('home')">Home</a>
        <a href="#" @click="navigateTo('posts')">Posts</a>
        <a href="#" @click="navigateTo('users')">Users</a>
    </nav>
    
    <!-- Dynamic components -->
    <div x-show="currentRoute === 'home'">
        {% include 'components/home.twig' %}
    </div>
    
    <div x-show="currentRoute === 'posts'">
        {% include 'components/posts.twig' %}
    </div>
</div>

API Integration

// Built-in HasJS framework
const api = new HasAPI('/api');

// Fetch data with coroutines
async function loadUsers() {
    try {
        const users = await api.get('/users');
        updateUI(users);
    } catch (error) {
        showError('Failed to load users');
    }
}

πŸ“Š Performance

Benchmarks

Server Type Requests/sec Memory Usage Response Time
OpenSwoole 15,000-50,000 ~50MB 2-5ms
PHP-FPM 1,000-3,000 ~100MB 20-50ms
Built-in Server 500-1,000 ~80MB 50-100ms

Optimization Features

  • Memory Persistence: Objects stay in memory between requests
  • Connection Pooling: Database connections reused across requests
  • Coroutine Support: Non-blocking I/O operations
  • Static File Serving: Direct file serving without PHP overhead

Production Deployment

# Start production server
php artisan server:start --daemon --port=8080 --workers=4

# With process manager (PM2, Supervisor)
pm2 start ecosystem.config.js

# Docker deployment
docker build -t hasphp-app .
docker run -d -p 8080:8080 hasphp-app

πŸ”§ Configuration

Environment Variables

# .env file
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourapp.com

# Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hasphp
DB_USERNAME=root
DB_PASSWORD=

# OpenSwoole Settings
SWOOLE_WORKERS=4
SWOOLE_TASK_WORKERS=2
SWOOLE_MAX_CONNECTIONS=1000

Server Configuration

// openswoole-server.php
$server->set([
    'worker_num' => env('SWOOLE_WORKERS', 4),
    'task_worker_num' => env('SWOOLE_TASK_WORKERS', 2),
    'max_request' => 10000,
    'max_conn' => env('SWOOLE_MAX_CONNECTIONS', 1000),
    'daemonize' => env('SWOOLE_DAEMON', false),
    'enable_coroutine' => true,
    'hook_flags' => SWOOLE_HOOK_ALL,
]);

πŸ“š API Documentation

HasPHP automatically generates OpenAPI 3.0 documentation for your APIs:

Features

  • πŸ”„ Auto-generated: Scans routes and generates docs
  • πŸ§ͺ Interactive: Test APIs directly in browser
  • πŸ“ Schema validation: Request/response validation
  • 🎨 Beautiful UI: Clean Swagger interface

Access Documentation

  • Main docs: /api/docs
  • OpenAPI spec: /api/docs/openapi.json
  • Alternative: /docs or /swagger

Example API Response

{
  "success": true,
  "data": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "[email protected]",
      "created_at": "2024-01-15T10:30:00Z"
    }
  ],
  "pagination": {
    "total": 100,
    "limit": 10,
    "offset": 0,
    "has_more": true
  }
}

πŸš€ Production Deployment

Docker Deployment

FROM openswoole/swoole:php8.2-alpine

WORKDIR /var/www

COPY . .
RUN composer install --no-dev --optimize-autoloader

EXPOSE 8080

CMD ["php", "artisan", "server:start", "--daemon"]

Process Management

# docker-compose.yml
version: '3.8'
services:
  hasphp:
    build: .
    ports:
      - "8080:8080"
    environment:
      - APP_ENV=production
      - DB_HOST=db
    depends_on:
      - db
      
  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: hasphp
      MYSQL_ROOT_PASSWORD: secret

Load Balancing

# nginx configuration
upstream hasphp_backend {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
    server 127.0.0.1:8083;
}

server {
    listen 80;
    server_name yourapp.com;
    
    location / {
        proxy_pass http://hasphp_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

# Clone repository
git clone https://github.com/your-org/hasphp.git
cd hasphp

# Install dependencies
composer install

# Run tests
php artisan test

# Start development server
php artisan serve --port=8080

Code Standards

  • PSR-4 autoloading
  • PSR-12 coding style
  • PHPUnit for testing
  • PHPStan for static analysis

πŸ“ License

HasPHP is open-sourced software licensed under the MIT license.


πŸ™ Acknowledgments

  • OpenSwoole - High-performance async PHP runtime
  • Twig - Flexible template engine
  • Alpine.js - Lightweight reactive framework
  • Swagger UI - API documentation interface

πŸ“ž Support


Built with ❀️ by the HasPHP Team

⭐ Star us on GitHub β€’ 🐦 Follow on Twitter β€’ πŸ“š Read the Docs

About

HasPHP is a high-performance PHP framework built on OpenSwoole, designed for building scalable and efficient web applications and APIs. It comes with built-in support for routing, database operations, and template rendering using Twig.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •