- How do I include a php.ini file in another php.ini file?
- 6 Answers 6
- Saved searches
- Use saved searches to filter your results more quickly
- License
- requtize/config
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- License
- awurth/PHP-Config
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
How do I include a php.ini file in another php.ini file?
I’m interested how RHEL/Fedora accomplishes the /etc/php.d/* include. Did they patch PHP to make this possible?
@gahooa that’s the question that I had leading me here. Did you ever learn how they accomplish the /etc/php.d/* include?
see the accepted answer below, this answers your question too. U can also do php -i (phpinfo() ) and see how the php was built
6 Answers 6
I don’t think you can «include» .ini files from the main php.ini file.
One possible solution, though, might be to use this option on the configure line, when compiling PHP:
--with-config-file-scan-dir=PATH Set the path where to scan for configuration files
If this option is used at compile-time, PHP will look for every .ini file in this directory, in addition to the «normal» php.ini file.
I suppose this is what is used by Ubuntu, for instance, which uses a different .ini file for each downloaded extension, instead of modifying php.ini.
The path to the php.ini file is being defined with this option, on the configure line:
--with-config-file-path=PATH Set the path in which to look for php.ini [PREFIX/lib]
Still, it probably means you’ll have to re-compile PHP — which is not that hard, btw — the hardest part being to get the dependencies you need.
And, here is a post on the internals@ mailling-list that says the same thing as I do: config files and PHP_CONFIG_FILE_SCAN_DIR
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Simple Config library, that handle PHP arrays, YAML and INI files as configs and can dump all configs into one PHP file as Cache to speed-up usage without parsing files every time.
License
requtize/config
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This library provides configurational files system, that alows You to combine multiple files and multiple formats into one object and accessing to it from simple array DOT notation.
- Merging multiple files into one object
- Imports files in multiple formats in the same way: PHP, INI, YAML
- Import files from another files
- Simple dot notation for accessing arrays
- Cache system, for save parsed files into one
< "require": < "requtize/config": "^1.2.0" > >
composer require requtize/config:^1.2.0
Dot notation is created for accessing multidimentional arrays (PHP) in simple way. If we want to access to some value, we need separate each index by dot. Following code should to explain that:
$array = [ 'one' => [ 'two' => [ 'three' => 'value' ] ] ]; // PHP access $array['one']['two']['three']; // Dot notation access $config->get('one.two.three');
// Without cache system $config = new Config(); // With cache system $config = new Config('config-filepath.php'); // Import files - multiple formats in the same way $config->import('filename.php'); $config->import('filename.ini'); $config->import('filename.yaml'); // Or import as array $config->import([ 'filename.php', 'filename.ini', 'filename.yaml' ]); // Get value - each index in multidimensional array separate by dot // Default value will be returned if given key(s) will not be existed $value = $config->get('some.deep.index.in-config.file', 'default value'); // Set value in current request live (and in cache). $config->set('key', 'some value to save'); // Key exists? $config->has('key') ? 'yes' : 'no'; // Get all data in configs (in all files) $config->all();
Importing other files in config file
If you want to import other files, You musn’t write any PHP to do this. Just use imports.files index in any file, and type files names in each index You want to import. Importing can be used for imports other formats in the same way. Remember that the files are searched relative to file you place importing rules!
return [ // Other data. 'imports' => [ 'filepath.php', '../../global-config/main.ini', './some-yaml.file.yaml' ] // Other data. ];
# Other data. imports: "filepath.php" "../../global-config/main.ini" "./some-yaml.file.yaml" # Other data.
; Other data. [imports] 0 = "filepath.php" 1 = "../../global-config/main.ini" 2 = "./some-yaml.file.yaml" ; Other data.
- Prefixes for imported files to allow import files from different place: ‘%root%/config.file.php’, ‘%server-config%/some.file.ini’
- XML files support
- You want some more.
This code is licensed under MIT License.
About
Simple Config library, that handle PHP arrays, YAML and INI files as configs and can dump all configs into one PHP file as Cache to speed-up usage without parsing files every time.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
awurth / PHP-Config Public archive
A configuration file loader for PHP
License
awurth/PHP-Config
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Loads your application’s configuration from PHP, YAML or JSON files, and stores it in a cache file for performance.
Uses the Symfony Config component.
$ composer require awurth/config
If you want to be able to load YAML files, you have to install the Symfony YAML component too:
$ composer require symfony/yaml
// config.php return [ 'database' => [ 'name' => 'database_name', 'user' => 'root', 'password' => 'pass' ] ];
# config.yml database: name: database_name user: root password: pass
// config.json < "database": < "name": "database_name", "user": "root", "password": "pass" > >
$loader = new AWurth\Config\ConfigurationLoader(); $phpConfig = $loader->load('path/to/config.php'); $yamlConfig = $loader->load('path/to/config.yml'); $jsonConfig = $loader->load('path/to/config.json'); // Result: $phpConfig = $yamlConfig = $jsonConfig = [ 'database' => [ 'name' => 'database_name', 'user' => 'root', 'password' => 'pass' ] ];
Using the cache (HIGHLY RECOMMENDED!)
$debug = 'prod' !== $environment; $loader = new AWurth\Config\ConfigurationLoader('path/to/cache.php', $debug); $config = $loader->load('path/to/config');
The cache file should not be versioned, especially if you store your database credentials in it.
If the second parameter ( $debug ) is set to true , the loader will parse the configuration files and regenerate the cache every time you edit a configuration file (including imports).
If set to false (in production), the loader will read the cache file directly if it exists or generate it if not. The configuration won’t be reloaded if you modify configuration files, so if you want to reload the cache, you have to delete the cache file.
Import files from the configuration
You can import other files into a configuration file with the imports key. The imports array will be removed from the final configuration. Valid file paths are:
- Relative paths: ../config.yml
- Absolute paths: /path/to/config.yml
- Relative or absolute paths using placeholders: %root_dir%/config/config.%env%.yml
# config.dev.yml imports: - 'parameters.yml' - 'config.yml' database: .
// config.dev.php return [ 'imports' => [ 'parameters.yml', // You can import YAML or JSON files from a PHP configuration file 'config.php' ], 'database' => [ . ] ];
// config.dev.json < "imports": [ "parameters.yml", "config.json" ], "database": <> >
# config.yml imports: - security.yml # security.yml security: login_path: /login logout_path: /logout
# config.yml imports: security: security.yml # security.yml login_path: /login logout_path: /logout
# parameters.yml parameters: database_name: my_db_name database_user: root database_password: my_password # config.yml imports: - 'parameters.yml' parameters: locale: en database: name: '%database_name%' user: '%database_user%' password: '%database_password%' translator: fallback: '%locale%' logfile: '%root_dir%/cache/%environment%.log' your_custom_config: '%your_custom_param%'
$loader = new AWurth\Config\ConfigurationLoader(); $loader->setParameters([ 'root_dir' => '/path/to/project/root', 'environment' => 'dev', 'your_custom_param' => 'your_custom_value' ]); // OR $loader ->setParameter('root_dir', '/path/to/project/root') ->setParameter('environment', 'dev') ->setParameter('your_custom_param', 'your_custom_value') ; $config = $loader->load(__DIR__.'/config.yml'); // Result: $config = [ 'parameters' => [ 'database_name' => 'my_db_name', 'database_user' => 'root', 'database_password' => 'my_password', 'locale' => 'en' ], 'database' => [ 'name' => 'my_db_name', 'user' => 'root', 'password' => 'my_password' ], 'translator' => [ 'fallback' => 'en' ], 'logfile' => '/path/to/project/root/cache/dev.log', 'your_custom_config' => 'your_custom_value' ];
Using PHP constants in YAML files
You can use simple PHP constants (like PHP_INT_MAX ) or class constants (like Monolog\Logger::DEBUG ) by using the YAML tag !php/const:
monolog: level: !php/const:Monolog\Logger::ERROR
Constants like __DIR__ and __FILE__ don’t work, use parameters instead.
Imports and parameters keys
$loader = new AWurth\Config\ConfigurationLoader(); $loader->getOptions() ->setImportsKey('require') ->setParametersKey('replacements'); $config = $loader->load(__DIR__.'/config.yml');
# config.yml require: # Does the same as 'imports:' - 'parameters.yml' - 'security.yml' replacements: # Does the same as 'parameters:' locale: en parameters: database_user: root translator: fallback: '%locale%' # Will be replaced by 'en' database: user: '%database_user%' # Will be replaced by null
Disable imports / parameters
Imports and parameters features can be disabled with the setEnableImports and setEnableParameters methods. If your configuration contains an imports key, it won’t be removed from the final configuration. Placeholders without corresponding parameters won’t be replaced by null .
$loader = new AWurth\Config\ConfigurationLoader(); $loader->getOptions() ->setEnableImports(false) ->setEnableParameters(false);
You can add your custom file loaders with the addLoader method. The loaders must implement Symfony\Component\Config\Loader\LoaderInterface .
$loader = new AWurth\Config\ConfigurationLoader(); $loader->addLoader(new XmlFileLoader()); $loader->addLoader(new CustomFileLoader()); $loader->load('path/to/xml_file.xml'); $loader->load('path/to/file.extension');