PHP Scripts What is PEAR in PHP

10.2 PHP_CompatInfo

Discussed version: 1.0.0RC3 License: PHP license 2.02
Class file (s): PHP / CompatInfo.php; PHP / CompatInfo / Cli.php

The packages PHP_CompatInfo and PHP_Compat are based on a very nice idea. These packages try to prevent problems that can occur when PHP scripts are not compatible with the PHP version that is used on a server.

PHP_CompatInfo is able to analyze a script and determine which PHP version is required to execute the script.

The package can analyze data from a string, a file or from several files (i.e. a subdirectory).

<?php require_once ('PHP/CompatInfo.php'); // Neues Objekt ableiten $info = new PHP_CompatInfo; // Dateinamen setzen $datei = 'quelle.php'; // Datei einlesen und analysieren $erg = $info->parseFile($datei); ?>

Listing 10.2 Analyzing a File

The data determined in Listing 10.2 are stored in $ erg in the form of an array. The structure could look like this, for example:

array (3) {["version"] => string (5) "4.0.0" ["extensions"] => array (5) {[0] => string (5) "mysql" [1] => string (5) "mysql" [2] => string (3) "pdf" [3] => string (5) "mysql" [4] => string (5) "mysql"} ["constants"] = > array (1) {[0] => string (8) "__FILE__"}}

The version key points to the minimum version of PHP required to run the code. The extensions key points to an array that contains information about which extensions must be available in order for the code to run. In this case, mysql appears several times because there are several mysql commands in the parsed file. In the case of longer programs, it makes sense at this point to process the array with array_unique () in order to avoid duplicate entries.

The last key, constants, contains an indexed array in which all system constants used by the script are listed.

If you don't want to parse a file but a string, parseString () is available. As a second parameter, you can transfer an array to the method that supports the key 'ignore_functions'. This can refer to an indexed array of function names that should be ignored during analysis.

In order to search through several strings or arrays at once, the parseArray () method is declared, which is passed an array with the file names or strings. If it is an array that consists of strings, you have to pass an array as the second parameter. This array must contain the key 'is_string', which must contain true. In addition, 'ignore_functions' is also supported.

The last way to parse files is to parseDir (). It is given the path of a directory in which all files are viewed. This method can also process a second parameter in the form of an array. In addition to 'ignore_functions', this array also supports the keys 'file_ext', 'recurse_dir', 'ignore_files' and 'ignore_dirs'. The first allows you to pass an array of filename extensions to specify the files to collect. In the standard setting, files with the extensions .php, .php4, .inc and .phtml are recorded. If you pass false with 'recurse_dir', subdirectories are not searched, which is done by default. The last two keys can each refer to an array in which you can pass the names of files or directories that should be ignored.

Just like parseArray (), this method returns a somewhat larger array. Here the beginning of the array is structured as described above. This first part describes the lowest common denominator that all files need. This is followed by an additional entry for each file containing a nested array that contains the specifications for this individual file.

The methods described are also contained in the PHP_CompatInfo_Cli class, which is defined in the PHP / CompatInfo / Cli.php file. The purpose of this class is to provide the simplest possible interface for using the class on the command line. On the one hand, the methods already described are available here. The constructor also processes command line options directly. Such a script can look like Listing 10.3.

#! / usr / bin / php run ()?>

Listing 10.3 Analyzing a Script Using the Command Line

The name of the file to be analyzed can be passed with --file = filename or -f. With --dir = directory or -d you can specify a directory that contains the files you want, and with --recurse or -n you can prevent subordinate directories from being searched. The method run (), which was used here, generates a screen output directly.