Download the PHP package dev-kraken/env-validator without Composer
On this page you can find all versions of the php package dev-kraken/env-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dev-kraken/env-validator
More information about dev-kraken/env-validator
Files in dev-kraken/env-validator
Package env-validator
Short Description Type-safe environment variable validation for Laravel and PHP applications with intelligent presets and extensible rule objects
License MIT
Homepage https://github.com/dev-kraken/env-validator
Informations about the package env-validator
EnvValidator
Type-safe environment variable validation for Laravel and PHP applications
EnvValidator is a modern, type-safe PHP package for validating environment variables in Laravel and standalone PHP applications. It provides robust validation with clear error messages, intelligent presets, extensible rule objects, and automatic environment file synchronization.
:sparkles: Features
- :lock: Type-safe validation with PHP 8.2+ and PHPStan level max
- :dart: Smart presets for common scenarios (Laravel, microservices, production, etc.)
- :jigsaw: Extensible rule system with custom Rule objects
- :arrows_counterclockwise: Environment sync to keep
.env
and.env.example
synchronized - :rocket: Laravel integration with auto-discovery and Artisan commands
- :package: Standalone support for non-Laravel PHP applications
- :shield: Production-ready with comprehensive test coverage
- :memo: Clear error messages with detailed validation feedback
:rocket: Quick Start
Installation
You can publish the configuration file with:
Laravel Usage
Standalone PHP Usage
Basic Example (String Rules)
Advanced Example (Rule Objects)
Required Field Validation
EnvValidator properly handles required field validation in both Laravel and standalone PHP environments:
Required fields fail validation when they are:
- ❌ Missing from the environment
- ❌ Empty strings (
''
) - ❌ Null values
- ✅ Present with valid values
:clipboard: Built-in Rule Presets
Preset | Description | Use Case |
---|---|---|
laravel |
Complete Laravel application | Full-featured web applications |
minimal |
Essential variables only | Microservices, lightweight apps |
production |
Production-ready settings | Deployment environments |
api |
API-focused applications | REST APIs, headless applications |
microservice |
Microservice-specific | Containerized services |
docker |
Docker/containerized apps | Container deployments |
Preset Usage Examples
:dart: Rule Objects vs String Rules
EnvValidator uses Rule objects for better type safety and maintainability:
:white_check_mark: Rule Objects (Recommended)
Benefits:
- :mag: IDE autocompletion and type hints
- :test_tube: Easy unit testing of individual rules
- :recycle: Reusable across multiple fields
- :art: Custom error messages with context
- :wrench: Better debugging and inspection
:books: Available Rule Objects
String Rules
BooleanRule
- Validates boolean values (true, false, 1, 0, yes, no, etc.)InRule
- Validates value is in a list of allowed valuesKeyRule
- Validates Laravel application keys (base64: format)PatternRule
- Validates against regex patternsEmailRule
- Validates email addressesJsonRule
- Validates JSON strings
Numeric Rules
NumericRule
- Validates numeric valuesIntegerRule
- Validates integer valuesPortRule
- Validates port numbers (1-65535)
Network Rules
UrlRule
- Validates URLsIpRule
- Validates IP addresses
:hammer_and_wrench: Advanced Usage
Custom Rules
Environment-Specific Validation
Conditional Validation
:art: Laravel Integration
Artisan Commands
:arrows_counterclockwise: Environment File Synchronization
Keep your .env
and .env.example
files synchronized automatically! This feature helps prevent deployment issues when developers add new environment variables but forget to update the example file.
:mag: Check Synchronization Status
Sample Output:
:gear: Synchronization Commands
:shield: Security Features
The sync command automatically handles sensitive data:
- Sensitive keys (PASSWORD, SECRET, KEY, TOKEN) get empty values
- URLs become
https://example.com
- Email addresses become
[email protected]
- Boolean values are standardized to
true
- Numeric values (ports, timeouts) are preserved
- Environment names get appropriate defaults (
production
)
:building_construction: Integration with CI/CD
Add environment sync checks to your deployment pipeline:
:computer: Programmatic Usage
:computer: Standalone PHP Usage
The environment sync feature works perfectly in standalone PHP applications (without Laravel):
Different Project Structures
Deployment Script Integration
:bulb: Best Practices
Laravel Projects
- Before Deployments: Always run
php artisan env:sync --check
- In Development: Use
php artisan env:sync
when adding new variables - Team Workflow:
- Add new variables to
.env
- Run
php artisan env:sync
- Commit both
.env.example
and updated validation rules
- Add new variables to
- CI/CD Integration: Fail builds if files are out of sync
- Security: Use
--no-values
flag for highly sensitive projects
Standalone PHP Projects
- Always specify absolute paths: Use
__DIR__
orgetcwd()
for reliable paths - Check file existence: Use
$syncService->envFileExists()
before operations - Handle errors gracefully: Always check
$result['success']
before proceeding - Integrate with deployment: Add sync checks to your deployment scripts
- Combine with validation: Use sync + validation for complete environment safety
Service Provider Configuration
Facade Usage
:test_tube: Testing
Running Tests
Code Quality
:book: Examples
The examples/
directory contains comprehensive examples demonstrating various use cases:
comprehensive_examples.php
- Complete feature showcase with presets and rule objectsrequired_field_examples.php
- Detailed required field validation examplesenv_sync_examples.php
- Environment file synchronization demonstrations (Laravel)standalone_env_sync_examples.php
- Environment sync for standalone PHP applicationspreset_examples.php
- Preset system demonstrationsrule_objects_demo.php
- Rule object usage and benefits
Real-World Scenarios
Running Examples
:handshake: Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
Development Setup
:page_facing_up: License
The MIT License (MIT). Please see License File for more information.
:lock: Security
If you discover any security vulnerabilities, please send an email to [email protected] instead of using the issue tracker.
:telephone_receiver: Support
- :email: Email: [email protected]
- :bug: Issues: GitHub Issues
- :speech_balloon: Discussions: GitHub Discussions
Made with :heart: by Dev Kraken