Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 176 lines (144 sloc) 5.992 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
<?php
error_reporting(1803);

/**
* go-pear.phar creator. Requires PHP_Archive version 0.11.0 or newer
*
* PHP version 5.1+
*
* To use, in pear-core create a directory
* named go-pear-tarballs, and run these commands in the directory
*
* <pre>
* $ pear download -Z PEAR Archive_Tar Console_Getopt Structures_Graph XML_Util
* </pre>
*
* finally, run this script using PHP 5.1's cli php

*
* @category pear
* @package PEAR
* @author Greg Beaver <cellog@php.net>
* @copyright 2005-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id$
*/

$y = array();
foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
    if ($path == '.') {
        continue;
    }

    $y[] = $path;
}

// remove current dir, we will otherwise include git files, which is not good
set_include_path(implode(PATH_SEPARATOR, $y));
require_once 'PEAR/PackageFile.php';
require_once 'PEAR/Config.php';
require_once 'PHP/Archive/Creator.php';
$config = &PEAR_Config::singleton();

function replaceVersion($contents, $path)
{
    return str_replace(array('@PEAR-VER@', '@package_version@'), $GLOBALS['pearver'], $contents);
}

$outputFile = 'go-pear.phar';

$tardir = __DIR__ . '/go-pear-tarballs';
$dp = @scandir($tardir);
if ($dp === false) {
    die("while locating packages to install: scandir('" . $tardir. "') failed\n");
}

$required = array('Archive_Tar', 'Console_Getopt', 'PEAR', 'Structures_Graph', 'XML_Util');
$packages = array();
foreach ($dp as $entry) {
    if ($entry{0} == '.' || !in_array(substr($entry, -4), array('.tar'))) {
        continue;
    }

    $package = strstr($entry, '-', true);
    $key = array_search($package, $required);
    if ($key !== false) {
        unset($required[$key]);
    }

    $packages[] = $entry;
}

if (!empty($required)) {
    die('Following packages were not available in tar format in go-pear-tarballs: ' . implode(', ', $required). "\n");
}

if (!file_exists("$tardir/tmp")) {
    mkdir("$tardir/tmp");
}

// Use the tar files for required Phar files
require_once 'Archive/Tar.php';
require_once 'System.php';

foreach ($packages as $package) {
    $name = substr($package, 0, -4);
    $tar = new Archive_Tar("$tardir/$package");
    $tar->extractModify("$tardir/tmp", $name);
}

chdir(__DIR__);

$pkg = new PEAR_PackageFile($config);
$pf = $pkg->fromPackageFile($tardir . '/tmp/package2.xml', PEAR_VALIDATE_NORMAL);
if (PEAR::isError($pf)) {
    foreach ($pf->getUserInfo() as $warn) {
        echo $warn['message'] . "\n";
    }

    die($pf->getMessage());
}
$pearver = $pf->getVersion();

$creator = new PHP_Archive_Creator('index.php', $outputFile); // no compression
$creator->useDefaultFrontController('PEAR.php');
$creator->useSHA1Signature();

foreach ($packages as $package) {
    echo "adding PEAR/go-pear-tarballs/$package\n";
    $creator->addFile("go-pear-tarballs/$package", "PEAR/go-pear-tarballs/$package");
}

$commandcontents = file_get_contents(__DIR__ . '/go-pear-phar.php');
$commandcontents = str_replace('require_once \'', 'require_once \'phar://' . $outputFile . '/', $commandcontents);
$creator->addString($commandcontents, 'index.php');

$commandcontents = file_get_contents($tardir . '/tmp/PEAR/Frontend.php');
$commandcontents = str_replace(
    array(
        "\$file = str_replace('_', '/', \$uiclass) . '.php';"
    ),
    array(
        "\$file = 'phar://" . $outputFile . "/' . str_replace('_', '/', \$uiclass) . '.php';"
    ), $commandcontents);
$commandcontents = replaceVersion($commandcontents, '');
$creator->addString($commandcontents, 'PEAR/Frontend.php');

$commandcontents = file_get_contents($tardir . '/tmp/PEAR/PackageFile/v2.php');
$commandcontents = str_replace(
    array(
        '$fp = @fopen("PEAR/Task/$taskfile.php", \'r\', true);',
    ),
    array(
        '$fp = @fopen("phar://' . $outputFile . '/PEAR/Task/$taskfile.php", \'r\', true);'
    ), $commandcontents);
$commandcontents = replaceVersion($commandcontents, '');
$commandcontents = $creator->tokenMagicRequire($commandcontents, 'a.php');
$creator->addString($commandcontents, 'PEAR/PackageFile/v2.php');

$creator->addMagicRequireCallback(array($creator, 'limitedSmartMagicRequire'));
$creator->addMagicRequireCallback('replaceVersion');
$creator->addFile($tardir . '/tmp/PEAR/Command.php', 'PEAR/Command.php');

$creator->clearMagicRequire();
$creator->addMagicRequireCallback(array($creator, 'tokenMagicRequire'));
$creator->addMagicRequireCallback('replaceVersion');

$ignores = array(
    '*PEAR/Frontend.php',
    '*PEAR/PackageFile/v2.php',
    '*PEAR/Command.php',
);

$creator->addDir($tardir . '/tmp/PEAR', $ignores, array('*PEAR/*'), false, $tardir . '/tmp');

$creator->addFile($tardir . '/tmp/PEAR.php', 'PEAR.php');
$creator->addFile($tardir . '/tmp/PEAR5.php', 'PEAR5.php');
$creator->addFile($tardir . '/tmp/System.php', 'System.php');
$creator->addFile($tardir . '/tmp/OS/Guess.php', 'OS/Guess.php');

// Other packages
$creator->addFile($tardir . '/tmp/Archive/Tar.php', 'Archive/Tar.php');
$creator->addFile($tardir . '/tmp/Util.php', 'XML/Util.php');
$creator->addFile($tardir . '/tmp/Console/Getopt.php', 'Console/Getopt.php');
$creator->addFile($tardir . '/tmp/Structures/Graph.php', 'Structures/Graph.php');
$creator->addFile($tardir . '/tmp/Structures/Graph/Node.php', 'Structures/Graph/Node.php');
$creator->addFile($tardir . '/tmp/Structures/Graph/Manipulator/AcyclicTest.php', 'Structures/Graph/Manipulator/AcyclicTest.php');
$creator->addFile($tardir . '/tmp/Structures/Graph/Manipulator/TopologicalSorter.php', 'Structures/Graph/Manipulator/TopologicalSorter.php');

// Include Start scripts specifically since they are never in the releases
$creator->addFile(__DIR__ . '/PEAR/Start.php', 'PEAR/Start.php');
$creator->addFile(__DIR__ . '/PEAR/Start/CLI.php', 'PEAR/Start/CLI.php');

$creator->useSHA1Signature();
$creator->savePhar(__DIR__ . DIRECTORY_SEPARATOR . $outputFile);

System::rm(array("-rf", "$tardir/tmp"));
Something went wrong with that request. Please try again.