15

I need to list all PHP extensions that's required by a given code base. What would be the best way to tackle this problem?

My initial thought is to write a script that goes through all files and find all functions and compare them to a given extension/function database. Any other suggestions?


Update: I already did some Bash script with grep and using get_loaded_modules, get_extension_funcs PHP functions, but this extension is exactly what I was looking for.

Charles
  • 50,943
  • 13
  • 104
  • 142
ibrahim
  • 151
  • 1
  • 3
  • Does this answer your question? [How to automatically determine which PHP extensions are used by a project?](https://stackoverflow.com/questions/34041290/how-to-automatically-determine-which-php-extensions-are-used-by-a-project) – kdmitry Dec 30 '20 at 14:33

3 Answers3

13

PHP CompatInfo (not the PEAR extension) is great once you learn how to use it:

  1. Go to PHP CompatInfo site
  2. Download and install the tool
  3. Copy the configuration file contents from their site and put it into a new file named phpcompatinfo.json
  4. run the following command (current dir being the directory where phpcompatinfo.json is placed)

    phpcompatinfo analyser:run . extension

You will get an output like this:

Extensions Analysis

 Extension  REF EXT min/Max PHP min/Max
 Core           4.2.0       4.2.0
 PDO            5.1.0       5.1.0
 Reflection     5.0.0       5.0.0
 calendar       4.0.0       4.0.0
 date           5.3.0       5.3.0
 filter         0.11.0      5.0.0
 gettext        4.0.0       4.0.0
 iconv          4.0.5       4.0.5
 json           5.4.0       5.4.0
 mbstring       4.4.3       4.4.3
 pcre           4.0.0       4.0.0
 session        4.0.3       4.0.3
 spl            5.4.0       5.4.0
 standard       5.5.0       5.5.0
 Total [14]                 5.5.0
hegemon
  • 6,614
  • 2
  • 32
  • 30
  • where is the `phpcompatinfo.json`? Do we have to make a file or replace an existing file? It says the `src/` dir but I can't find it. – Philll_t Nov 07 '14 at 16:44
  • Okay, so for those of you who had a little trouble here's what I did. You can just put it (**phpcompatinof.json**) in your etc folder (_linux_) `/etc/` and it will run. – Philll_t Nov 07 '14 at 17:10
  • 1
    I just edited the answer to make it clear that phpcompatinfo.json is a new file to be created, not an existing one – hegemon Nov 10 '14 at 11:57
  • Great answer! Why haven't I found this years ago? – halfpastfour.am Oct 25 '17 at 08:33
  • 1
    The phpcompatinfo.json file is no longer required for basic usage, the docs have been updated. Now you just have to install the tool and execute `phpcompatinfo analyser:run .` – Cécile Fecherolle Oct 25 '18 at 14:56
7

There is a PEAR package that does that, from what I remember : PHP_CompatInfo :

Find out the minimum version and the extensions required for a piece of code to run

See The Command-Line Parser to know how to run it from the Command Line, and to get some examples of output.


(It's marked as not maintained anymore, so there might be some troubles with recent versions of PHP, but last time I used it -- maybe one year ago -- it worked fine)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    The latest version (december 2013) of PHP_CompatInfo is available from http://bartlett.laurent-laville.org/ – MV. Apr 18 '14 at 02:13
2

As an alternative to PHP CompatInfo, I've developed a tool for PHP 7 that works similarly. It doesn't provide as much detailed information as CompatInfo, but it does list the extensions required by your project.

See: https://github.com/RogerGee/php-ext-depends

Example usage:

$ php depends.php --suffix .php ~/code/open-source/drupal/core
Core (builtin)
ctype
curl
date (builtin)
dom
filter
hash
iconv
json
libxml
pcre (builtin)
PDO
posix
readline
Reflection (builtin)
session
SimpleXML
SPL (builtin)
standard (builtin)
xml
zlib
Roger Gee
  • 851
  • 5
  • 10