Download the PHP package locky42/leopard-core without Composer
On this page you can find all versions of the php package locky42/leopard-core. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download locky42/leopard-core
More information about locky42/leopard-core
Files in locky42/leopard-core
Package leopard-core
Short Description Lightweight PHP framework core for routing, controllers, and application base.
License MIT
Informations about the package leopard-core
Leopard Core
leopard-core is the core library for the Leopard Framework, providing essential features such as routing, attributes, dependency injection container, and other foundational components.
Table of Contents
- Installation
- Core Components
- Dependency Injection Container
- Routing
- Attributes
- ContractFactory
- View
- SEO
- Usage Examples
- Testing
Installation
Install leopard-core using Composer:
Core Components
Dependency Injection Container
The Container is a simple dependency injection container that allows you to register services and retrieve their instances.
Example:
Routing
The Router allows you to define routes using attributes, YAML configuration, and automatic route generation based on controller structure.
Routing Methods
- Attribute-based routing - Define routes using PHP attributes
- YAML configuration - Define routes and controller paths in YAML
- Auto-routing - Automatic route generation for methods ending with
Actionsuffix
Action Method Convention
For auto-routing (YAML controllers), only methods ending with Action suffix are processed as routes:
- HTTP method prefix:
get,post,put,delete,patch,options,head - Default method: If no prefix is specified,
GETis used by default - Action name: Formed by removing the HTTP method prefix (if any) and the
Actionsuffix
Examples:
Attribute-based Routing Example:
YAML Configuration Example:
YAML Controllers Behavior
controllers[].controllerregisters one конкретний контролер (relative toApp\\Controllers\\..., or absolute FQCN if it starts with\\).controllers[].namespacescans all*Controller.phpinsrc/Controllers/{Namespace}and registers them with the same base path.- Only methods ending with
Actionare auto-routed from YAML controller definitions. - HTTP method prefix is detected from method name:
get|post|put|delete|patch|options|head. - Special path handling for
path:path: /→/{controller}and/{controller}/{action}path: ""→/and/{action}path: /base→/base/{controller}and/base/{controller}/{action}
Dynamic Parameters in Paths
The router supports these placeholders in route paths:
{id}- one URI segment (no slash){id:\\d+}- custom regex constraint{path}- greedy capture including/
If a parameter type in controller method is int|float|bool and conversion fails, router returns 404.
HEAD requests are allowed to match GET routes.
Auto-routing with loadControllersFrom:
Attributes
leopard-core supports PHP attributes for defining routes and other metadata.
Example:
ContractFactory
The ContractFactory is a universal factory for creating instances through interface contracts. It enables flexible dependency management by allowing you to register and swap implementations without modifying existing code.
Key Benefits:
- Flexibility - Easily swap implementations
- Testability - Create mock objects for testing
- Extensibility - Add custom implementations
- Dependency Inversion - Depend on abstractions, not concrete classes
Basic Usage:
Swapping Implementations:
Available Methods:
register(string $interface, string $className, array $doctrineMapping = []): void- Register an implementation (and sync Doctrine mapping when available)create(string $interface): object- Create an instancegetMapping(string $interface): ?string- Get registered class namehasMapping(string $interface): bool- Check if interface is registeredgetMappings(): array- Get all registered mappingsunregister(string $interface): bool- Unregister an interfaceclear(): void- Clear all mappingsreset(): void- Reset to initial state
Integration Example:
Doctrine Integration (auto ResolveTargetEntity)
If locky42/leopard-doctrine is installed, ContractFactory::register(...) automatically forwards the mapping to ResolveTargetEntityRegistry::addResolveTargetEntity(...).
You can also pass Doctrine mapping options as the third argument:
In application projects, keep all contract mappings in one file (for example config/contract-mappings.php) and include it in bootstrap.
Best Practices:
-
Always use
::classsyntax: -
Register at application bootstrap:
-
Use type hints with interfaces:
- Clear mappings in tests:
Error Handling:
The factory throws InvalidArgumentException in these cases:
- Class doesn't exist
- Interface doesn't exist
- Class doesn't implement the interface
- No mapping found when creating instance
View
The View class is responsible for rendering templates and managing the presentation layer. It supports layouts, blocks, and integration with the SEO service.
Features:
- Template rendering with data passing
- Layout system for consistent page structure
- Block rendering for reusable components
- CSS and JavaScript management
- SEO metadata through integrated Seo service
Example:
Rendering Blocks:
SEO
The Seo service manages SEO metadata for your pages, including meta tags, Open Graph, Twitter Cards, and more.
Features:
- Meta tags management
- Open Graph tags for social media
- Twitter Cards support
- Canonical URLs
- Keywords management
- Robots directives
- Charset configuration
Example:
Rendering SEO Tags in Layout:
Usage Examples
Loading Configuration
Testing
To run tests, use the following command:
The tests are located in the vendor/locky42/leopard-core/tests directory.
License
This project is licensed under the MIT License.
All versions of leopard-core with dependencies
psr/container Version ^2.0
symfony/yaml Version ^7.3
nyholm/psr7 Version ^1.8
nyholm/psr7-server Version ^1.1
locky42/leopard-events Version ^1.1