|
1 | | -# kariri-cache |
2 | | -Camada de cache que proporciona mecanismos de armazenamento temporário de dados, melhorando o desempenho por evitar a repetição de operações custosas. |
| 1 | +# KaririCode Framework: Dotenv Component |
| 2 | + |
| 3 | +[](README.md) |
| 4 | +[](README.pt-br.md) |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +A robust and flexible environment variable management component for the KaririCode Framework, providing advanced features for handling .env files in PHP applications. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- Parse and load environment variables from .env files |
| 15 | +- Support for variable interpolation |
| 16 | +- **Automatic type detection and casting** |
| 17 | + - Detects and converts common types (string, integer, float, boolean, array, JSON) |
| 18 | + - Preserves data types for more accurate usage in your application |
| 19 | +- **Customizable type system** |
| 20 | + - Extensible with custom type detectors and casters |
| 21 | + - Fine-grained control over how your environment variables are processed |
| 22 | +- Strict mode for variable name validation |
| 23 | +- Easy access to environment variables through a global helper function |
| 24 | +- Support for complex data structures (arrays and JSON) in environment variables |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +To install the KaririCode Dotenv component in your project, run the following command: |
| 29 | + |
| 30 | +```bash |
| 31 | +composer require kariricode/dotenv |
| 32 | +``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +### Basic Usage |
| 37 | + |
| 38 | +1. Create a `.env` file in your project's root directory: |
| 39 | + |
| 40 | +```env |
| 41 | +KARIRI_APP_ENV=develop |
| 42 | +KARIRI_APP_NAME=KaririCode |
| 43 | +KARIRI_PHP_VERSION=8.3 |
| 44 | +KARIRI_PHP_PORT=9003 |
| 45 | +KARIRI_APP_DEBUG=true |
| 46 | +KARIRI_APP_URL=https://kariricode.com |
| 47 | +KARIRI_MAIL_FROM_NAME="${KARIRI_APP_NAME}" |
| 48 | +KARIRI_JSON_CONFIG={"key": "value", "nested": {"subkey": "subvalue"}} |
| 49 | +KARIRI_ARRAY_CONFIG=["item1", "item2", "item with spaces"] |
| 50 | +``` |
| 51 | + |
| 52 | +2. In your application's bootstrap file: |
| 53 | + |
| 54 | +```php |
| 55 | +<?php |
| 56 | + |
| 57 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 58 | + |
| 59 | +use KaririCode\Dotenv\DotenvFactory; |
| 60 | +use function KaririCode\Dotenv\env; |
| 61 | + |
| 62 | +$dotenv = DotenvFactory::create(__DIR__ . '/../.env'); |
| 63 | +$dotenv->load(); |
| 64 | + |
| 65 | +// Now you can use the env() function to access your environment variables |
| 66 | +$appName = env('KARIRI_APP_NAME'); |
| 67 | +$debug = env('KARIRI_APP_DEBUG'); |
| 68 | +$jsonConfig = env('KARIRI_JSON_CONFIG'); |
| 69 | +$arrayConfig = env('KARIRI_ARRAY_CONFIG'); |
| 70 | +``` |
| 71 | + |
| 72 | +### Type Detection and Casting |
| 73 | + |
| 74 | +The KaririCode Dotenv component automatically detects and casts the following types: |
| 75 | + |
| 76 | +- Strings |
| 77 | +- Integers |
| 78 | +- Floats |
| 79 | +- Booleans |
| 80 | +- Null values |
| 81 | +- Arrays |
| 82 | +- JSON objects |
| 83 | + |
| 84 | +Example: |
| 85 | + |
| 86 | +```env |
| 87 | +STRING_VAR=Hello World |
| 88 | +INT_VAR=42 |
| 89 | +FLOAT_VAR=3.14 |
| 90 | +BOOL_VAR=true |
| 91 | +NULL_VAR=null |
| 92 | +ARRAY_VAR=["item1", "item2", "item3"] |
| 93 | +JSON_VAR={"key": "value", "nested": {"subkey": "subvalue"}} |
| 94 | +``` |
| 95 | + |
| 96 | +When accessed using the `env()` function, these variables will be automatically cast to their appropriate PHP types: |
| 97 | + |
| 98 | +```php |
| 99 | +$stringVar = env('STRING_VAR'); // string: "Hello World" |
| 100 | +$intVar = env('INT_VAR'); // integer: 42 |
| 101 | +$floatVar = env('FLOAT_VAR'); // float: 3.14 |
| 102 | +$boolVar = env('BOOL_VAR'); // boolean: true |
| 103 | +$nullVar = env('NULL_VAR'); // null |
| 104 | +$arrayVar = env('ARRAY_VAR'); // array: ["item1", "item2", "item3"] |
| 105 | +$jsonVar = env('JSON_VAR'); // array: ["key" => "value", "nested" => ["subkey" => "subvalue"]] |
| 106 | +``` |
| 107 | + |
| 108 | +This automatic typing ensures that you're working with the correct data types in your application, reducing type-related errors and improving overall code reliability. |
| 109 | + |
| 110 | +### Advanced Usage |
| 111 | + |
| 112 | +#### Custom Type Detectors |
| 113 | + |
| 114 | +Create custom type detectors to handle specific formats: |
| 115 | + |
| 116 | +```php |
| 117 | +use KaririCode\Dotenv\Type\Detector\AbstractTypeDetector; |
| 118 | + |
| 119 | +class CustomDetector extends AbstractTypeDetector |
| 120 | +{ |
| 121 | + public const PRIORITY = 100; |
| 122 | + |
| 123 | + public function detect(mixed $value): ?string |
| 124 | + { |
| 125 | + // Your detection logic here |
| 126 | + // Return the detected type as a string, or null if not detected |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +$dotenv->addTypeDetector(new CustomDetector()); |
| 131 | +``` |
| 132 | + |
| 133 | +#### Custom Type Casters |
| 134 | + |
| 135 | +Create custom type casters to handle specific data types: |
| 136 | + |
| 137 | +```php |
| 138 | +use KaririCode\Dotenv\Contract\TypeCaster; |
| 139 | + |
| 140 | +class CustomCaster implements TypeCaster |
| 141 | +{ |
| 142 | + public function cast(mixed $value): mixed |
| 143 | + { |
| 144 | + // Your casting logic here |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +$dotenv->addTypeCaster('custom_type', new CustomCaster()); |
| 149 | +``` |
| 150 | + |
| 151 | +## Development and Testing |
| 152 | + |
| 153 | +For development and testing purposes, this package uses Docker and Docker Compose to ensure consistency across different environments. A Makefile is provided for convenience. |
| 154 | + |
| 155 | +### Prerequisites |
| 156 | + |
| 157 | +- Docker |
| 158 | +- Docker Compose |
| 159 | +- Make (optional, but recommended for easier command execution) |
| 160 | + |
| 161 | +### Setup for Development |
| 162 | + |
| 163 | +1. Clone the repository: |
| 164 | + |
| 165 | + ```bash |
| 166 | + git clone https://github.com/KaririCode-Framework/kariricode-dotenv.git |
| 167 | + cd kariricode-dotenv |
| 168 | + ``` |
| 169 | + |
| 170 | +2. Set up the environment: |
| 171 | + |
| 172 | + ```bash |
| 173 | + make setup-env |
| 174 | + ``` |
| 175 | + |
| 176 | +3. Start the Docker containers: |
| 177 | + |
| 178 | + ```bash |
| 179 | + make up |
| 180 | + ``` |
| 181 | + |
| 182 | +4. Install dependencies: |
| 183 | + ```bash |
| 184 | + make composer-install |
| 185 | + ``` |
| 186 | + |
| 187 | +### Available Make Commands |
| 188 | + |
| 189 | +- `make up`: Start all services in the background |
| 190 | +- `make down`: Stop and remove all containers |
| 191 | +- `make build`: Build Docker images |
| 192 | +- `make shell`: Access the shell of the PHP container |
| 193 | +- `make test`: Run tests |
| 194 | +- `make coverage`: Run test coverage with visual formatting |
| 195 | +- `make cs-fix`: Run PHP CS Fixer to fix code style |
| 196 | +- `make quality`: Run all quality commands (cs-check, test, security-check) |
| 197 | + |
| 198 | +For a full list of available commands, run: |
| 199 | + |
| 200 | +```bash |
| 201 | +make help |
| 202 | +``` |
| 203 | + |
| 204 | +## License |
| 205 | + |
| 206 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 207 | + |
| 208 | +## Support and Community |
| 209 | + |
| 210 | +- **Documentation**: [https://kariricode.org/docs/dotenv](https://kariricode.org/docs/dotenv) |
| 211 | +- **Issue Tracker**: [GitHub Issues](https://github.com/KaririCode-Framework/kariricode-dotenv/issues) |
| 212 | +- **Community**: [KaririCode Club Community](https://kariricode.club) |
| 213 | + |
| 214 | +## Acknowledgments |
| 215 | + |
| 216 | +- The KaririCode Framework team and contributors. |
| 217 | +- Inspired by other popular PHP Dotenv libraries. |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +Built with ❤️ by the KaririCode team. Empowering developers to build more robust and flexible PHP applications. |
0 commit comments