{"id":51587899,"url":"https://github.com/denzyldick/php-config","last_synced_at":"2026-07-11T12:30:41.788Z","repository":{"id":364723498,"uuid":"1268984347","full_name":"denzyldick/php-config","owner":"denzyldick","description":"Load YAML, JSON, XML, or PHP config files and validate them against PHP 8.2 attribute-based DTOs.","archived":false,"fork":false,"pushed_at":"2026-06-14T08:24:48.000Z","size":11546,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-06-14T09:06:05.542Z","etag":null,"topics":["attributes","config","configuration","dto","json","php","schema","validation","xml","yaml"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/denzyldick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-14T06:57:39.000Z","updated_at":"2026-06-14T08:22:55.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/denzyldick/php-config","commit_stats":null,"previous_names":["denzyldick/php-config"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/denzyldick/php-config","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denzyldick%2Fphp-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denzyldick%2Fphp-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denzyldick%2Fphp-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denzyldick%2Fphp-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/denzyldick","download_url":"https://codeload.github.com/denzyldick/php-config/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denzyldick%2Fphp-config/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35362871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-11T02:00:05.354Z","response_time":104,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributes","config","configuration","dto","json","php","schema","validation","xml","yaml"],"created_at":"2026-07-11T12:30:41.072Z","updated_at":"2026-07-11T12:30:41.781Z","avatar_url":"https://github.com/denzyldick.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-config\n\nSchema-based config loader for PHP. Load YAML, JSON, XML, or PHP config files and validate them against PHP 8.2 attribute-based DTOs.\n\n## Installation\n\n```bash\ncomposer require denzyl/php-config\n```\n\n## Usage\n\nDefine a DTO with attributes:\n\n```php\nuse PhpConfig\\Attribute\\Required;\nuse PhpConfig\\Attribute\\Range;\nuse PhpConfig\\Attribute\\DefaultValue;\nuse PhpConfig\\Attribute\\Nested;\n\nclass DatabaseConfig\n{\n    #[Required]\n    public string $host;\n\n    #[Required]\n    #[Range(1, 65535)]\n    public int $port;\n\n    #[DefaultValue('root')]\n    public string $username;\n\n    #[Required]\n    public string $password;\n\n    #[DefaultValue(false)]\n    public bool $ssl;\n\n    #[Nested]\n    public LogConfig $log;\n}\n```\n\nLoad and validate — three ways to handle the result:\n\n```php\nuse PhpConfig\\Config;\nuse PhpConfig\\Exception\\ValidationException;\n\n$result = Config::load('config/database.yaml', DatabaseConfig::class);\n\n// 1. Unwrap — throws on failure (simplest)\n$config = $result-\u003eunwrap(); // ValidationException on error\n\n// 2. Guard — check and return early\nif ($result-\u003eisErr()) {\n    $errors = $result-\u003eexception()-\u003egetErrors();\n    // log, display, or rethrow\n}\n\n// 3. Match — handle both branches inline (functional style)\n$result-\u003ematch(\n    fn(DatabaseConfig $config) =\u003e bootstrap($config),\n    fn(ValidationException $e) =\u003e handleErrors($e-\u003egetErrors()),\n);\n```\n\n### Real-world example\n\n```php\n// public/index.php\nuse PhpConfig\\Config;\n\n$config = Config::load('../config/app.yaml', AppConfig::class)\n    -\u003eunwrap(); // fails hard — config errors at deploy time, not runtime\n\n$app = new App($config);\n$app-\u003erun();\n```\n\n```php\n// src/App.php\nuse PhpConfig\\Config;\nuse PhpConfig\\Exception\\ValidationException;\n\nclass App\n{\n    public function __construct(\n        private readonly AppConfig $config,\n    ) {}\n\n    public static function bootstrap(string $configPath): self\n    {\n        $result = Config::load($configPath, AppConfig::class);\n\n        if ($result-\u003eisErr()) {\n            $errors = $result-\u003eexception()-\u003egetErrors();\n            $messages = array_map(\n                fn($e) =\u003e \"{$e-\u003epath}: {$e-\u003emessage}\",\n                $errors,\n            );\n            throw new \\RuntimeException(\n                \"Config validation failed:\\n\" . implode(\"\\n\", $messages),\n            );\n        }\n\n        return new self($result-\u003eunwrap());\n    }\n}\n```\n\n```php\n// config/app.yaml\ndatabase:\n  host: localhost\n  port: 3306\n  username: root\n  password: secret\n  ssl: false\n```\n\n## Supported formats\n\n| Format | Extension | Reader |\n|--------|-----------|--------|\n| YAML | `.yaml`, `.yml` | `symfony/yaml` |\n| JSON | `.json` | `ext-json` |\n| XML | `.xml` | `ext-simplexml` |\n| PHP | `.php` | `require` + `return` |\n\n## Attributes\n\n| Attribute | Description |\n|-----------|-------------|\n| `#[Required]` | Field must be present and non-null |\n| `#[Range(min, max)]` | Numeric range constraint |\n| `#[Length(min, max)]` | String length constraint |\n| `#[Regex(pattern)]` | String pattern match |\n| `#[Email]` | Valid email format |\n| `#[Url]` | Valid URL format |\n| `#[DefaultValue(value)]` | Default value when absent |\n| `#[Nested]` | Nested DTO (class inferred from property type) |\n\n## Code generation\n\nGenerate DTO classes from an existing config file — useful when migrating or scaffolding.\n\n### CLI\n\n```bash\nvendor/bin/php-config generate:class config/database.yaml --class=DatabaseConfig\nvendor/bin/php-config generate:class config/app.json --output=src/Config/\n```\n\nOptions:\n\n| Option | Description |\n|--------|-------------|\n| `--class=\u003cname\u003e` | Class name (default: `AppConfig`) |\n| `--namespace=\u003cns\u003e` | Namespace (default: `App\\Config`) |\n| `--output=\u003cdir\u003e` | Write files to directory (default: stdout) |\n\n### Programmatic\n\n```php\nuse PhpConfig\\Generator\\SchemaGenerator;\n\n$generator = new SchemaGenerator();\n$files = $generator-\u003egenerateFromFile('config/database.yaml', 'DatabaseConfig');\n\n// Inspect or write\nforeach ($files as $filename =\u003e $code) {\n    echo $code;\n}\n```\n\nNested objects produce separate class files. Types are inferred from values: `string`, `int`, `float`, `bool`, nested `object`, or `array`. All scalar fields get `#[Required]` — remove it on fields that are optional.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenzyldick%2Fphp-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenzyldick%2Fphp-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenzyldick%2Fphp-config/lists"}