Recommend this page to a friend! |
Classes of Christian Vigh | PHP.ini Settings | README.md | Download |
|
DownloadIntroductionThe 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 :
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 :
DependenciesThis 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. LimitationsThis 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. ExamplesBefore digging into the class' details, a few examples will give you an overview of its capabilities. Getting the value of a settingGetting 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 :
Note that you can also use the Get() method for that :
Changing a setting value and writing back the resultsThe following example reads file php.ini, modifies the memory\_limit setting, then writes the contents back to file 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 :
Enabling an extensionExtensions are special settings that can occur several times throughout a php.ini file, with different values. For example :
on Unix systems ; the equivalent on Windows platforms would be :
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 :
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. Value checkingThe 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 :
then you will get the following PhpSettingsException :
ReferenceThis section provides a reference for the methods, properties and constants defined by the PhpSettings class. Constructor
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. MethodsAsString
Returns the contents of the modified php.ini file, as a string. DisableExtension
Disables the specified extension. Parameters are the following :
Returns false if the extension was already disabled, true otherwise. EnableExtension
Enables the specified extension. Parameters are the following :
Returns false if the extension was already enabled, true otherwise. Get
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 :
For settings containing characters not authorized in PHP identifiers, you can also use the following form :
GetEnabledExtensions
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). IsExtensionEnabled
Checks if an extension is enabled. Parameters are the following :
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. SaveTo
Saves the .INI file contents to the specified output file, either modified or not. This method does not affect the isDirty property. Set
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. Unset
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. PropertiesBackupFileThis 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 :
FilenameAbsolute path of the file that has been supplied to the class constructor. IsDirtyThis 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. ConstantsOPTION\_\* flagsA 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. PHP\_INI\_\* constantsThese constants define the locations where a setting can be modified :
VALUE\_TYPE\_\* constantsThese constants specify the type of a setting :
PhpSettings.csv fileThe 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 :
|