PPU without frontend

Overview

Always keep your application up-to-date with the most recent and stable version of PEAR packages.

This example implement the communication with pear channel and the Log package.

Screenshot

Once download operation is completed and update succeeded with pear installer, the page will be display again and print "Hello World"

Dependencies

This example requires mandatory resources :

Explains step by step

This example will simulate an application that used the PEAR::Log package we try to keep up-to-date.

The PEAR::Log package is loaded at line 13, and of course PPU at line line 14.
On lines 20-37 we defined a null frontend that allow to keep your application ressources up-to-date without asking end-user inputs.
This null frontend is instanciated at line 40 where we ask to check for update on pear channel for Log package.

On lines 25-26 we set preferences to check for any next stable version.

forceRestart method defined on lines 20 to 36 and lines 45;50 allow to reload the current script/page with new version of PEAR::Log package and consume all messages given by the pear installer.

Source Code

  1. <?php
  2. /**
  3. * Always keep your application up-to-date with the most recent and stable version
  4. * of PEAR::Log package.
  5. *
  6. * @version    $Id: withoutFrontend.php,v 1.1 2006/05/07 18:14:09 farell Exp $
  7. * @author     Laurent Laville <pear@laurent-laville.org>
  8. * @package    PEAR_PackageUpdate
  9. * @access     public
  10. * @since      File available since Release 0.5.0
  11. */
  12.  
  13. require_once 'Log.php';
  14. require_once 'PEAR/PackageUpdate.php';
  15.  
  16. /**
  17. * This class allow to use PEAR_PackageUpdate as backend without any frontend.
  18. * No end-user action needed.
  19. */
  20. class PEAR_PackageUpdate_Null extends PEAR_PackageUpdate
  21. {
  22.     function PEAR_PackageUpdate_Null($packageName, $channel)
  23.     {
  24.         parent::PEAR_PackageUpdate($packageName, $channel);
  25.         $this->setMinimumState(PEAR_PACKAGEUPDATE_STATE_STABLE);
  26.         $this->setMinimumReleaseType(PEAR_PACKAGEUPDATE_TYPE_BUG);
  27.     }
  28.  
  29.     function forceRestart()
  30.     {
  31.         // removes warning message given by pear installer
  32.         ob_end_clean();
  33.         // Reload current page.
  34.         header('Location: ' . $_SERVER['PHP_SELF']);
  35.         exit();
  36.     }
  37. }
  38.  
  39. // Check for updates of PEAR::Log package though pear.php.net channel
  40. $ppu =& PEAR_PackageUpdate::factory('Null', 'Log', 'pear');
  41. if ($ppu !== false) {
  42.     // Check for new stable version
  43.     if ($ppu->checkUpdate()) {
  44.         // Update your local copy
  45.         ob_start();
  46.         if ($ppu->update()) {
  47.             // If the update succeeded, the application should be restarted.
  48.             $ppu->forceRestart();
  49.         }
  50.         ob_end_clean();
  51.     }
  52. }
  53.  
  54. // your application code goes here ...
  55. print 'Hello World';
  56. ?>