Section 12.4. CLI Environment


12.4. CLI Environment

PEAR lets you include command-line scripts in a package. However, when doing so, you will quickly run into configuration problems like, "Which include_path should I use here" or "What is the full path of the PHP executable that should be used?" This information may be specified by users in a set of environment variables, as shown in Table 12.1.

Table 12.1. PEAR Installer Environment Variables

Environment Variable

Corresponding Configuration Parameter

PHP_PEAR_SYSCONF_DIR

none

PHP_PEAR_MASTER_SERVER

master_server

PHP_PEAR_HTTP_PROXY

http_proxy

PHP_PEAR_INSTALL_DIR

php_dir

PHP_PEAR_EXTENSION_DIR

ext_dir

PHP_PEAR_DOC_DIR

doc_dir

PHP_PEAR_BIN_DIR

bin_dir

PHP_PEAR_DATA_DIR

data_dir

PHP_PEAR_TEST_DIR

test_dir

PHP_PEAR_CACHE_DIR

cache_dir

PHP_PEAR_PHP_BIN

php_bin

PHP_PEAR_VERBOSE

verbose

PHP_PEAR_PREFERRED_STATE

preferred_state

PHP_PEAR_UMASK

umask

PHP_PEAR_CACHE_TTL

cache_ttl

PHP_PEAR_SIG_TYPE

sig_type

PHP_PEAR_SIG_BIN

sig_bin

PHP_PEAR_SIG_KEYDIR

sig_keydir


If any of this information is needed during bootstrapping of a PHP script, these environment variables should be used. The PEAR installer uses these environment variables when it sets up default values for its configuration parameters.

Here is an example of a UNIX command-line scripts, using the PHP_PEAR_PHP_BIN environment variable to find the right PHP binary:

 #!/bin/sh export PHP_PEAR_PHP_BIN=${PHP_PEAR_PHP_BIN:-php} exec $PHP_PEAR_PHP_BIN d output_buffering=1 $0 $@ <?php ob_end_clean(); print "PHP " . phpversion() . " on " . php_uname() . "\n"; 

  • PHP embedded in UNIX shell script. What happens here is that the PHP_PEAR_PHP_BIN is set to either its current existing value or to php if it is not set. Then, the shell script exec's (replaces itself with) PHP with a parameter that enables output buffering, followed by the name of the script and all the command-line parameters. When PHP starts executing the file, it would normally just display the second and third line with shell script code, but because it is running with output buffering enabled, these lines are just buffered. In the PHP block, ob_end_clean() ends output buffering and discards the output so far, so PHP never displays the shell code:

     @echo off if "%OS"=="Windows_NT" @setlocal if "%PHP_PEAR_PHP_BIN%"=="" goto useDefault goto run :useDefault set PHP_PEAR_PHP_BIN=php.exe :run %PHP_PEAR_PHP_BIN% -d output_buffering=1 %~dp0 %1 %2 %3 %4 %5 %6 %7  %8 %9 <?php ob_end_clean(); print "PHP " . phpversion() . " on " . php_uname() . "\n"; 

  • PHP embedded in a Windows .bat file. The basic approach here is the same as in the UNIX shell example. The PHP_PEAR_PHP_BIN environment variable is used to getting the right PHP executable, defaulting to just php.exe. (One limitation to note for .bat scripts is that you cannot pass more than nine parameters.)



    PHP 5 Power Programming
    PHP 5 Power Programming
    ISBN: 013147149X
    EAN: 2147483647
    Year: 2003
    Pages: 240

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net