![]() PEAR_PackageFileManager : The Definitive Guide |
If you want to do something else than just basic tasks (search and replace string, converting line ending), extreme customization is possible with <postinstallscript> tag. This tag define parameters that are used by PEAR installer to retrieve user input, while running a post-install script.
![]() |
Note |
---|---|
This feature is only available for package.xml , version 2.0
|
Distribution of full web application, including a database, is one of common problem that <tasks:postinstallscript> may solve.
In this example we will install a MySQL database with the
mysqlinstall
script that resides in
server
sub-directory of target
directory installation (see attribute baseinstalldir).
In package.xml, between <contents> tag we will find something like :
Generated by a script like this:
Adding a post install task <tasks:postinstallscript> requires
to build a PEAR_Task_Postinstallscript_rw
instance for the
post install script ($task). This is made by PEAR_PackageFileManager::initPostinstallScript()
method.
Next we can add as much as we want any paramgroup id, such as :
<tasks:paramgroup><tasks:id>databaseSetup</tasks:id>.
This is made by PEAR_Task_Postinstallscript_rw::addParamGroup()
method.
Each paramgroup may have zero, one or more param. These params are
added by an array of PEAR_Task_Postinstallscript_rw::getParam()
, or
false if there is no param.
Finally, post install script task is added to package xml by a call
to PEAR_PackageFileManager::addPostinstallTask()
method.
Post-install script files can be named anything one desires, but
the class within the file must be the same name as the file with
all path separators replaced by underscores, and a fixed postfix
"_postinstall". In other words, this postinstall script:
server/mysqlinstall.php
, must contain
a class named server_mysqlinstall_postinstall.
API of a post install script always match, at least 2 functions :
init()
that initialize script environment with :
parameter #1 a reference to a PEAR_Config
instance (current
configuration used for installation).
parameter #2 a reference to the current PEAR_PackageFileManager
instance.
parameter #3 the last version of this package that was installed. This is a very important parameter, as it is the only way to determine whether a package is being installed from scratch, or upgraded from a previous version. Using this parameter, it is possible to determine what incremental changes, if any, need to be performed.
run()
is called at the conclusion of each parameter group in order to process the user's responses to queries. This method carry two parameters :
parameter #1 is an array that will contain a list of successfully completed parameter group sections. This can be used to restore any system changes made by the installation script.
parameter #2 identify the most recent paramgroup or
contains _undoOnError
only
triggered by the PEAR installer on rollback process.
In our example, we should have a class pattern as this one :
This phase is important and allow script to communicate with a backend logger ($this->_ui), get information from your PEAR installation copy ($this->_config), get information on package release you are trying to install/upgrade ($this->_pkg) and the previous version installed (if any).
You may also add other data you think it's necessary to task proceed. For example a database existence indicator ($this->databaseExists), and what kind of driver we could use: either mysql or mysqli ($this->mysqliAvailable).
Deeper inside class server_mysqlinstall_postfix
and run()
method, we find :
a way to commit action (each paramgroup id).
and another to revert action : _undoOnError
Explore in details one action (paramgroup id): databaseCreate
specialized to creating new
database (default is peartest
) given
by param name database
of this
paramgroup.
![]() |
Important |
---|---|
In case of problem, don't forget to provide a way to revert
action already applied. PEAR installer will then trigger
_undoOnError phase run.
|
PEAR_PackageFileManager : The Definitive Guide | v 1.6.0 : November 17, 2006 |