PEAR logo

PEAR_PackageFileManager : The Definitive Guide



Chapter 12. Using custom file tasks

Table of Contents

Add a new custom file task
Improve warning message requiring custom file task handler

This lesson :

Add a new custom file task

Standard file tasks provided by default, allow to : search-and-replace string into file contents, change line-ending, and add a complex postinstall script.

If you want to specialize your installation with a complex task, you will need first to learn how to create customized task in PHP .

Once a package that reference, manage, the new task file is properly installed in your system (into directory PEAR/Task/), you can use it with a call to addTaskToFile().

We suppose to have previously installed a package that manage the new task stripwhitespace.

[Note] Note
Install the PEAR_Task_Stripwhitespace package version 0.1.0 on __URI channel.
      pear install http://pear.laurent-laville.org/get/PEAR_Task_Stripwhitespace-0.1.0.tgz
     
  1. <?php
  2. require_once 'PEAR/PackageFileManager2.php';
  3. require_once 'PEAR/Task/Stripwhitespace/rw.php';
  4. require_once 'PEAR/Config.php';
  5. require_once 'PEAR/Frontend.php';
  6.  
  7. PEAR::setErrorHandling(PEAR_ERROR_DIE);
  8.  
  9. $p2 = new PEAR_PackageFileManager2();
  10. //...
  11.  
  12. $p2->generateContents();
  13.  
  14. $config = PEAR_Config::singleton();
  15. $log = PEAR_Frontend::singleton();
  16. $xml = $p2->getArray();
  17.  
  18. $task = new PEAR_Task_Stripwhitespace_rw($p2, $config, $log, $xml);
  19. $p2->addTaskToFile('index.php', $task);
  20. //...
  21.  
  22. $p2->resetUsestask();
  23. $p2->addUsestask($task->getName(),
  24.     'http://pear.laurent-laville.org/get/PEAR_Task_Stripwhitespace-0.1.0' );
  25.  
  26. if (isset($_GET['make']) || (isset($_SERVER['argv']) && @$_SERVER['argv'][1] == 'make')) {
  27.     $p2->writePackageFile();
  28. } else {
  29.     $p2->debugPackageFile();
  30. }
  31. ?>

This script will generate something like :

  1. <contents>
  2.   <dir baseinstalldir="HTML" name="/">
  3.    <file baseinstalldir="HTML" name="server/mysqlinstall.php" role="php" />
  4.    <file baseinstalldir="HTML" name="index.php" role="php">
  5.     <tasks:stripwhitespace />
  6.    </file>
  7.   </dir>
  8.  </contents>
[Important] Important
Allways generate file list contents with generateContents() before trying to add a task to one of file with addTaskToFile().

However, if a user does not have the package installed that provides the custom task stripwhitespace, then the error message on installation will simply say Unknown task "stripwhitespace" which is not very helpful.

PEAR_PackageFileManager : The Definitive Guide v 1.6.0 : November 17, 2006