nette / php-generator

🐘 Generates neat PHP code for you. Supports new PHP 8.3 features.
https://doc.nette.org/php-generator
Other
2.11k stars 138 forks source link

Feature Request: Files that return directly #29

Closed hparadiz closed 6 years ago

hparadiz commented 6 years ago

I'm looking to be able to build files that return directly.

<?php
return [
    'mysql' => [
        'host'      =>  'localhost',
        'database' =>  'database',
        'username' =>  'username',
        'password' =>  'password',
    ]
];

This is a common way to configure things in various frameworks.

You can use a simple config getting function like so:

    function config($Label)
    {
        $Config = static::$ApplicationPath . '/config/' . $Label . '.php';
        if (!file_exists($Config)) {
            throw new \Exception($Config . ' not found in '.static::class.'::config()');
        }
        return require $Config;
    }
dg commented 6 years ago
file_put_contents('config.php', '<?php return ' . Nette\PhpGenerator\Helpers::dump($cfg) . ';');
hparadiz commented 6 years ago

Thank you @dg! This will be excellent for adding new configurations programatically.

Is there a way to preserve comments and order for existing code?