Download the PHP package hudsxn/canvas without Composer
On this page you can find all versions of the php package hudsxn/canvas. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hudsxn/canvas
More information about hudsxn/canvas
Files in hudsxn/canvas
Package canvas
Short Description A simple page builder API that is agnostic of frameworks, and can be used for CSR or SSR
License MIT
Informations about the package canvas
Canvas Framework
A lightweight, performant PHP framework for building server-rendered and hybrid web applications with a focus on flexibility, security, and developer experience.
Overview
Canvas provides a structured approach to building web pages through a tree-based component system, decoupled rendering architecture, and comprehensive response handling. It solves common problems in modern PHP web development while maintaining simplicity and performance.
Core Problems Solved
1. Rendering Flexibility
Traditional PHP frameworks often tightly couple your application logic to a specific templating engine. Canvas solves this by providing a renderer contract that allows you to choose your preferred rendering approach:
- Server-side templating: Use Blade, Twig, or plain PHP
- Single Page Applications: Generate JSON for React, Vue, or Angular
- Hybrid approaches: Server-side rendering with client-side hydration
- Static site generation: Pre-render to HTML files
Switch between rendering strategies without changing your application code. Simply swap the renderer implementation.
2. Performance Optimization
Canvas includes several performance-critical features:
Array-Based String Building Instead of using string concatenation (which creates new string buffers on each operation), Canvas uses numerically-indexed arrays for HTML generation. This approach is significantly faster for large HTML outputs:
Automatic Gzip Compression Built-in content negotiation and gzip compression can reduce HTML payload sizes by 70-90%, significantly improving page load times. Compression level is configurable from 1 (fastest) to 9 (maximum compression).
Bitwise Flags Page configuration flags (caching, indexing, security) use bitwise operations instead of individual boolean properties, reducing memory usage and providing faster flag checks.
3. Security by Default
Canvas makes it easy to implement security best practices:
- Content Security Policy: Fluent API for configuring CSP directives
- Automatic Security Headers: X-Frame-Options, X-Content-Type-Options, HSTS
- HTTPS Enforcement: Built-in HTTPS redirect support
- XSS Protection: CSP helpers with sensible defaults
Configure comprehensive security policies in a few lines:
4. SEO and Metadata Management
Centralized management of document metadata, Open Graph tags, Twitter Cards, and search engine directives:
5. Separation of Concerns
Canvas enforces clean architecture through separation:
- Node: Structure and hierarchy (the "what")
- Canvas: Root container with template information (the "where")
- Page: Document metadata and policies (the "metadata")
- Renderer: Presentation logic (the "how")
- Response: HTTP delivery (the "transport")
This separation makes testing easier, code more maintainable, and allows different parts of your application to evolve independently.
Architecture
Node System
The Node class provides a flexible tree structure for representing UI components or document elements. Each node contains:
- Type identifier (component name)
- Arbitrary properties (JSON-serializable)
- Child nodes
- Parent reference
Nodes can be searched, traversed, and manipulated programmatically:
Canvas (Root Container)
Canvas extends Node to represent the complete document, adding template information:
Page (Document Configuration)
Page combines a Canvas with complete document metadata and behavioral rules:
Renderer (Pluggable Rendering)
Implement the CanvasPageRenderer interface to define how pages are rendered:
Response (HTTP Delivery)
Response handles the complete HTTP response lifecycle:
Performance Features
Efficient String Building
Using array-based string building avoids the performance penalty of repeated string concatenation. For large HTML documents, this can provide significant performance improvements.
Configurable Compression
Gzip compression with configurable levels (1-9) allows you to balance compression ratio against CPU usage based on your needs:
- Level 1-3: Fast compression for dynamic content
- Level 6: Balanced (default)
- Level 9: Maximum compression for static assets
Bitwise Flag Operations
Page flags use bitwise operations for efficient storage and fast checks:
Security Features
Content Security Policy
Fluent API for building CSP directives:
Automatic Security Headers
Response automatically generates security headers:
X-Content-Type-Options: nosniffX-Frame-Options: SAMEORIGINX-XSS-Protection: 1; mode=blockReferrer-Policy: strict-origin-when-cross-originStrict-Transport-Security(when HTTPS is enforced)
HTTPS Enforcement
Built-in support for forcing HTTPS connections:
Use Cases
Traditional Server-Rendered Applications
Use Canvas with your preferred template engine (Blade, Twig, plain PHP) for traditional server-side rendering with enhanced performance and security features.
Single Page Applications
Canvas includes a built-in SinglePageApplicationRenderer for SPAs with full control over assets and state injection:
The SPA renderer generates a minimal HTML shell with:
- Automatic viewport configuration for mobile optimization
- CSP-compliant nonce-based inline script injection
- Configurable mount points and state variables
- Support for single or multiple JS/CSS bundles (vendor splitting)
- Complete page state serialized to JSON for client-side hydration
Multiple Asset Support:
Hybrid Applications
Combine server-side rendering with client-side hydration for optimal performance and SEO:
- Server renders initial HTML
- Canvas state serialized to JSON
- Client-side framework hydrates from JSON
Static Site Generation
Pre-render pages to static HTML files:
Getting Started
Installation
Basic Example
Quick Start: Single Page Application
Canvas includes a production-ready SPA renderer:
The client-side app can access the state:
Implementing a Renderer
Design Philosophy
Canvas is built on several key principles:
Flexibility Over Convention: Choose your own rendering approach, template engine, and architecture. Canvas provides structure without imposing rigid conventions.
Performance by Default: Built-in optimizations like array-based string building and gzip compression mean you get good performance without extra effort.
Security by Design: Making security features easy to use encourages their adoption. CSP configuration should be as simple as method chaining.
Separation of Concerns: Clear boundaries between structure (Node), metadata (Page), rendering (Renderer), and transport (Response) make code more maintainable and testable.
Progressive Enhancement: Start simple and add features as needed. Use basic rendering or add compression, security headers, and advanced CSP as your application grows.
Batteries Included: While remaining flexible, Canvas includes a production-ready SinglePageApplicationRenderer so you can start building immediately without boilerplate.
Bundled Renderer
SinglePageApplicationRenderer
A production-ready renderer for React, Vue, Angular, and other client-side frameworks.
Features:
- CSP-compliant nonce-based script injection
- Automatic viewport configuration
- Support for single or multiple JS/CSS bundles
- Configurable mount points and state variables
- SEO-friendly meta tag generation
- Automatic state serialization
Configuration Options:
Generated HTML Structure:
Advanced Usage:
License
MIT License - see LICENSE file for details.