Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 262 | All time: 7,789 This week: 673 |
Version | License | PHP version | Categories | |||
php-settings 1.0.0 | BSD License | 5.6 | PHP 5, Systems administration, Text p..., L..., C..., P... |
<?php
|
The PhpSettings class is intended to manage settings coming from a php.ini file.
It can modify settings, enable or disable PHP extensions and save the results back to the original input file or to another file.
It tries to enforce type checking on the setting values, taking type information from either the PhpSettings.csv file (see the PhpSettings.csv file section in this document) or from the values found in the supplied php.ini file. An exception will be thrown if you try to change a setting using a value whose type is not compatible with the original value ; for example, you will not be able to set the memory\_limit setting to "hello world", since it accepts an integer value which can also be expressed as a byte quantity.
Formatting and comments are preserved when writing back the modified contents. Additionally, quoted values will remain quoted, and unquoted values will remain unquoted. Great care has been taken for preserving the initial formatting, so that running the diff command on the input and output files will only show the modifications you made.
In the same way, most php.ini files contain commented-out directives, such as :
;realpath_cache_size = 16k
The PhpSettings class perfectly handles this kind of situation ; if you try to set the value of the realpath\_cache\_size setting to, say, "32k" then, instead of appending a new setting value to the end of file, the original - commented-out - setting will be reused, uncommented and its value will be change, to give the following :
realpath_cache_size = 32k
This class uses the Path Utiities package (http://www.phpclasses.org/package/10018-PHP-Platform-independent-path-management-utitlities.html). A copy of file Path.phpclass has been provided here for your convenience, but it may not be the latest release.
This class only handles changes to php.ini settings ; it is not intended to administrate an Apache server. After every change, you will need to restart your server manually for the changes to take effect.
Before digging into the class' details, a few examples will give you an overview of its capabilities.
Getting the value of a setting is really straightforward ; just load the php.ini file of your choice, then access the setting as if it was a property of the PhpSettings object :
<?php
require ( 'PhpSettings.phpclass' ) ;
$settings = new PhpSettings ( 'php.ini' ) ;
echo "Memory limit is : " . $settings -> memory_limit ;
Note that you can also use the Get() method for that :
echo "Memory limit is : " . $settings -> Get ( 'memory_limit' ) ;
The following example reads file php.ini, modifies the memory\_limit setting, then writes the contents back to file php.ini.out :
<?php
require ( 'PhpSettings.phpclass' ) ;
$settings = new PhpSettings ( 'php.ini' ) ;
$settings -> memory_limit = '128M' ;
$settings -> SaveTo ( 'php.ini.out' ) ;
As for retrieving a setting value, there is a counterpart method called Set() that allows you to change the value of a setting :
$settings -> Set ( 'memory_limit', '128M' ) ;
Extensions are special settings that can occur several times throughout a php.ini file, with different values. For example :
extension=mbstring.so
extension=exif.so
on Unix systems ; the equivalent on Windows platforms would be :
extension=php_mbstring.dll
extension=php_exif.dll
To enable an extension, just specify its name ; you don't have to worry about the file extension (".so" on Unix systems, ".dll" on Windows) as well as the "php_" prefix that is added on Windows platforms :
<?php
require ( 'PhpSettings.phpclass' ) ;
$settings = new PhpSettings ( 'php.ini' ) ;
$settings -> EnableExtension ( 'mbstring' ) ;
If the extension is already present in the input php.ini file, the concerned line will be reused and uncommented if necessary. If the extension is not already present, it will be inserted just after the very last extension specified in the input file.
This method also handles Zend extensions. See the description of the EnableExtension() method for more information on this topic.
The PhpSettings class enforces type-checking, which is either deduced from the setting value specified in the php.ini file, or from the description found in the PhpSettings.csv optional file that is provided with this package.
Changing a setting using an incorrect value will throw an exception ; for example, there is a setting called engine whose value should be boolean. If you try the following :
$settings -> engine = "hello world" ;
then you will get the following PhpSettingsException :
The value "hello world" supplied for the "engine" boolean setting is not a valid boolean value.
This section provides a reference for the methods, properties and constants defined by the PhpSettings class.
$settings = new PhpSettings ( $file, $options = PhpSettings::OPTIONS\_NONE ) ;
Loads a php.ini file specified by the $file parameter. After loading, all the settings will be accessible as properties of the $settings object, or through the Get(), Set() or extension management methods.
See the Constants section later in this chapter for an explanation about the flags that can be specified for the $options parameter.
$contents = $settings -> AsString ( ) ;
Returns the contents of the modified php.ini file, as a string.
$status = $settings -> DisableExtension ( $name, $zend = false, $prefix = true ) ;
Disables the specified extension. Parameters are the following :
Returns false if the extension was already disabled, true otherwise.
$status = $settings -> EnableExtension ( $name, $zend = false, $prefix = true ) ;
Enables the specified extension. Parameters are the following :
Returns false if the extension was already enabled, true otherwise.
$value = $settings -> Get ( $setting, $default = null ) ;
Returns the value of the specified PHP .ini setting, converted to the appropriate type, or the value returned by the ini_get() function.
Whatever the result, the function returns null if the setting is definitely not defined in the php.ini file.
Settings can also be accessed as if they were properties of the PhpSettings class ; for example :
$value = $settings -> memory_limit ;
For settings containing characters not authorized in PHP identifiers, you can also use the following form :
$value = $setting -> {'mbstring.strict_detection'} ;
$array = $settings -> GetEnabledExtensions ( $zend = false ) ;
Returns the list of enabled extensions as an array of strings. If the $zend parameter is false, only regular PHP extensions will be returned (the ones that have been specified using the extension setting). When true, only Zend extensions will be returned (zend_extension settings).
$status = $settings -> IsExtensionEnabled ( $name, $zend = false, $prefix = true ) ;
Checks if an extension is enabled. Parameters are the following :
$settings -> Save ( ) ;
Writes back the modified contents of the supplied input file.
No saving will occur if no setting has been modified, ie when the IsDirty property is false.
Returns true if contents have been saved, false otherwise.
This method sets the IsDirty property to false.
$settings -> SaveTo ( $output_file ) ;
Saves the .INI file contents to the specified output file, either modified or not.
This method does not affect the isDirty property.
$settings -> $setting = $value ;
$settings -> $setting = [ $value, $section ] ;
$settings -> Set ( $setting, $value, $section = false ) ;
Changes the value of an existing setting or creates a new one.
Care is taken about the supplied value type ; when a setting is modified, its target type is taken from the setting found in the .INI file, or from the settings loaded from the PhpSettings.csv file.
When a new setting is created, the information loaded from the PhpSettings.csv file is used to determine its type ; if not found, the type will be guessed from the specified value.
The parameters are the following :
You do not need to care about quoting the value you specified : this will automatically be done if the string contains characters that needs to be quoted in a php.ini file.
$settings -> $setting = null ;
$settings -> Unset ( $setting ) ;
Undefines the specified setting.
If the setting already exists in the INI file, it will simply be commented out.
If it is defined multiple times (which should never happen), only the last non-commented occurrence will be commented out.
This property is set when a backup copy has been made and gives the path of the backup file.
This path will have the following form :
Absolute path of the file that has been supplied to the class constructor.
This property will be set to true whenever a setting has been changed using either the Set() or EnableExtension method.
It is used by the Save() method to determine if file contents should be written back or not.
A combination of the following flags can be specified to the $options parameter of the class constructor :
Backup files are always created in the same directory as the supplied input file.
These constants define the locations where a setting can be modified :
These constants specify the type of a setting :
The PhpSettings.csv file is completely optional ; when present, it must reside in the same directory as the PhpSettings.phpclass file.
It contains a description of the PHP settings, taken from the following address : http://php.net/manual/en/ini.list.php.
This is a CSV file whose fields are separated by semicolons, and contains the following columns :
Files (10) |
File | Role | Description | ||
---|---|---|---|---|
Data (3 files) | ||||
example.php | Example | Example script | ||
example.php.ini | Data | Auxiliary data | ||
example.php.out | Output | Example output | ||
Path.phpclass | Class | Class source | ||
PhpSettings.csv | Data | Auxiliary data | ||
PhpSettings.phpclass | Class | Class source | ||
README.md | Doc. | Documentation |
Files (10) | / | Data |
File | Role | Description |
---|---|---|
mapsettings.phs | Appl. | Generator script for PhpSettings.csv |
settings-20161106.csv | Output | PhpSettings.csv file contents |
settings-20161106.txt | Data | PHP Settings description |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.