achinon/yaml_classer
Symfony bundle that converts YAML config files into generated PHP classes, enabling IDE-friendly, callable access to YAML values. Install via Composer, run a console command to generate a class, then use via DI or instantiate directly.
Installation
Run composer require achinon/yaml_classer in your Laravel project.
Ensure the package is autoloaded (check composer.json and run composer dump-autoload if needed).
First Command Generate a class from a YAML file:
php artisan achinon:yaml_classer path/to/config.yml ConfigClassName
Example:
php artisan achinon:yaml_classer config/services.yml App\Config\ServicesConfig
First Use Case Use the generated class in your Laravel app:
// Via DI (if registered as a service)
public function __construct(App\Config\ServicesConfig $servicesConfig) {
$this->mailer = $servicesConfig->get('mail.mailers.smtp.host');
}
// Or instantiate directly
$config = new App\Config\ServicesConfig();
$value = $config->example; // Access nested values like YAML keys
Config Management
config('services.mail.host') to $mailConfig->mail->host.Dependency Injection
// In AppServiceProvider@boot()
$this->app->singleton(App\Config\ServicesConfig::class, function ($app) {
return new App\Config\ServicesConfig();
});
public function __construct(App\Config\ServicesConfig $config) {
$this->config = $config;
}
Dynamic Configuration
$userConfig = new App\Config\UserSettings();
if (!$userConfig->get('feature.enabled')) {
// Disable feature logic
}
IDE Integration
$config->database-> shows host, port).Development Workflow
config/custom.yml).Deployment Workflow
Laravel Config Integration
Extend the package to load YAML from config/ and generate classes automatically:
// In a custom Artisan command
$yamlFiles = File::allFiles(config_path());
foreach ($yamlFiles as $file) {
$className = 'App\Config\\' . basename($file, '.yml');
$this->call('achinon:yaml_classer', [
'file' => $file->getPathname(),
'class' => $className,
]);
}
Caching
Cache generated classes in bootstrap/cache/ to avoid regeneration on every request.
File Overwrites
--dry-run (if available) or check for files manually before running.Namespace Collisions
ConfigClassName) don’t conflict with existing classes. Prefix with your app namespace (e.g., App\Config\).YAML Parsing Quirks
# May fail
defaults: &defaults
adapter: mysql
host: localhost
production:
<<: *defaults
host: db.prod.example.com
IDE Autocompletion Lag
.gitignore to exclude generated files if they’re not critical.Class Not Found?
Verify the generated file is in the autoloaded namespace (check composer.json under autoload->psr-4).
Property Access Errors Debug with:
var_dump((new App\Config\ServicesConfig())->toArray());
Compare with the original YAML to spot parsing issues.
Command Not Found
Ensure the package is registered in config/console.php:
'commands' => [
\Achinon\YamlClasserBundle\Command\GenerateClassCommand::class,
],
Custom Class Templates Extend the package to generate classes with:
public string $host).public function getHost(): string { return $this->host; }
Validation Layer Add validation to generated classes:
public function __construct() {
if (empty($this->example)) {
throw new \RuntimeException("Config 'example' is required.");
}
}
Environment-Specific Configs
Generate separate classes for config.yml and config.prod.yml:
php artisan achinon:yaml_classer config.yml App\Config\Config
php artisan achinon:yaml_classer config.prod.yml App\Config\ProdConfig
Performance For large YAML files, optimize by:
__get() magic method).Yaml::parseFile() directly if the package becomes a bottleneck.Testing Mock generated classes in tests:
$mockConfig = $this->createMock(App\Config\ServicesConfig::class);
$mockConfig->method('get')->willReturn('test-value');
How can I help you explore Laravel packages today?