PEAR logo

PHP_CompatInfo : The Definitive Guide

Detection of a directory

Rather than parsing file after file of an application, you may give the root of your application path as the main directory to parse. Default is recursive parsing: that mean each directory children will be also parsed. And only files with extension php, php4, inc, phtml will be proceed.

Suppose we have to detect which PHP version we need to run the PEAR::File_Find package release 1.3.0

First begin to download the archive from http://pear.php.net/package/File_Find/download/1.3.0 and extract the full contents to a temporary directory (in our example its '/tmp')

We will use this very simple detection script.

  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $source = '/tmp/File_Find-1.3.0';
  5.  
  6. $info = new PHP_CompatInfo();
  7. $info->parseDir($source);
  8. // you may also use unified method:  $info->parseData($source);
  9. ?>

Results displayed:

array (
  'ignored_files' =>
  array (
    0 => '/tmp/File_Find-1.3.0/package.xml',
    1 => '/tmp/File_Find-1.3.0/tests/01glob.phpt',
    2 => '/tmp/File_Find-1.3.0/tests/02maptree.phpt',
    3 => '/tmp/File_Find-1.3.0/tests/03maptreemultiple.phpt',
    4 => '/tmp/File_Find-1.3.0/tests/04search.phpt',
    5 => '/tmp/File_Find-1.3.0/tests/05search_inside.phpt',
    6 => '/tmp/File_Find-1.3.0/tests/06match_shell.phpt',
    7 => '/tmp/File_Find-1.3.0/tests/bug2773.phpt',
  ),
  'ignored_functions' =>
  array (
  ),
  'ignored_extensions' =>
  array (
  ),
  'ignored_constants' =>
  array (
  ),
  'max_version' => '',
  'version' => '4.3.0',
  'extensions' =>
  array (
    0 => 'pcre',
  ),
  'constants' =>
  array (
    0 => '__FILE__',
  ),
  'tokens' =>
  array (
  ),
  'cond_code' =>
  array (
    0 => 4,
  ),
  '/tmp/File_Find-1.3.0/Find.php' =>
  array (
    'ignored_functions' =>
    array (
    ),
    'ignored_extensions' =>
    array (
    ),
    'ignored_constants' =>
    array (
    ),
    'max_version' => '',
    'version' => '4.3.0',
    'extensions' =>
    array (
      0 => 'pcre',
    ),
    'constants' =>
    array (
    ),
    'tokens' =>
    array (
    ),
    'cond_code' =>
    array (
      0 => 4,
    ),
  ),
  '/tmp/File_Find-1.3.0/tests/setup.php' =>
  array (
    'ignored_functions' =>
    array (
    ),
    'ignored_extensions' =>
    array (
    ),
    'ignored_constants' =>
    array (
    ),
    'max_version' => '',
    'version' => '4.0.0',
    'extensions' =>
    array (
    ),
    'constants' =>
    array (
      0 => '__FILE__',
    ),
    'tokens' =>
    array (
    ),
    'cond_code' =>
    array (
      0 => 0,
    ),
  ),
)
    

means that package PEAR::File_Find 1.3.0 need at least PHP 4.3.0 with extension pcre.

[Caution] Caution

cond_cond offset 0 is set to 4. That means there are conditional code (constant condition) implemented in source code (with php defined function).

If you have a look on source code, you will see that all conditions referred to private package constant FILE_FIND_DEBUG

[Tip] Tip

You may avoid to read the source code to know the constant name, if you specify the debug option when parsing the directory.

  1. <?php
  2. require_once 'PHP/CompatInfo.php';
  3.  
  4. $source = '/tmp/File_Find-1.3.0';
  5.  
  6. $info = new PHP_CompatInfo();
  7. $info->parseDir($source, array('debug' => true));
  8. ?>

And you will see in displayed results, something like :

  'cond_code' =>
  array (
    0 => 4,
    1 =>
    array (
      0 =>
      array (
      ),
      1 =>
      array (
      ),
      2 =>
      array (
        0 => 'FILE_FIND_DEBUG',
      ),
    ),
    

cond_code offset 1 is an array available only when debug mode is set to true. In this array :

PHP_CompatInfo : The Definitive Guide v 1.8.0 : August 1, 2008