p4.php #1

  • //
  • guest/
  • jon_parise/
  • api/
  • php/
  • examples/
  • p4.php
  • View
  • Commits
  • Open Download .zip Download (6 KB)
#!/usr/local/bin/php
<?php
/**
 * Perforce Command Line Interface
 *
 * Copyright 2004-2007 Jon Parise <jon@php.net>.  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE. 
 *
 * @author  Jon Parise <jon@php.net>
 */

if (!extension_loaded('perforce') || !class_exists('PerforceClient')) {
    die("The Perforce extension has not been loaded!\n");
}

class ClientUser implements PerforceClientUser
{
    /**
     * Output an error.
     */
    function OutputError($error)
    {
        fwrite(STDERR, "$error\n");
    }

    /**
     * Handle an error with a severity level.
     */
    function HandleError($msg, $severity)
    {
        $this->OutputError($msg);
    }

    /**
     * Output data.
     */
    function Message($msg, $severity)
    {
        $this->OutputInfo(0, $msg);
    }

    /**
     * Return input data from the user.
     */
    function InputData()
    {
        return fgets(STDIN);
    }

    /**
     * Output data with level info.
     */
    function OutputInfo($level, $data)
    {
        fwrite(STDOUT, $data);
    }

    /**
     * Output binary data.
     */
    function OutputBinary($data)
    {
        fwrite(STDOUT, $data);
    }

    /**
     * Output text data.
     */
    function OutputText($data)
    {
        fwrite(STDOUT, $data);
    }

    /**
     * Output an array of file status tags.
     */
    function OutputStat($tags)
    {
        foreach ($tags as $key => $value) {
            if ($key !== 'func') {
                $this->OutputText('... ' . $key . ' ' . $value . "\n");
            }
        }
    }

    /**
     * Prompt the user for input.
     */
    function Prompt($msg)
    {
        echo $msg;

        return fgets(STDIN);
    }

    /**
     * Display an error message and wait for user to respond.
     */
    function ErrorPause($buf)
    {
        $this->OutputError($buf);
        fgetc(STDIN);
    }

    /**
     * Handle a file edit request.
     */
    function Edit($file)
    {
        if (!empty($_ENV['P4EDITOR'])) {
            $editor = $_ENV['P4EDITOR'];
        } else if (!empty($_ENV['EDITOR'])) {
            $editor = $_ENV['EDITOR'];
        } else {
            $editor = 'vi';
        }

        system("$editor $file");
    }

    /**
     * Handle a two-way diff request.
     */
    function Diff($file1, $file2, $doPage, $flags)
    {
        if (!empty($_ENV['P4DIFF'])) {
            $diff = $_ENV['P4DIFF'];
        } else if (!empty($_ENV['DIFF'])) {
            $diff = $_ENV['DIFF'];
        } else {
            $this->OutputError(
                'No diff program specified with $P4DIFF or $DIFF.');
        }

        $pager = false;
        if ($doPage) {
            if (!empty($_ENV['P4PAGER'])) {
                $pager = $_ENV['P4PAGER'];
            } else if (!empty($_ENV['PAGER'])) {
                $pager = $_ENV['PAGER'];
            } else {
                $pager = false;
            }
        }

        if ($pager) {
            system("$diff $flags $file1 $file2 | $pager");
        } else {
            system("$diff $flags $file1 $file2");
        }
    }

    /**
     * Handle a merge request.
     */
    function Merge($base, $leg1, $leg2, $result)
    {
        if (!empty($_ENV['P4MERGE'])) {
            $merge = $_ENV['P4MERGE'];
        } else if (!empty($_ENV['MERGE'])) {
            $merge = $_ENV['MERGE'];
        } else {
            $this->OutputError(
                'No merge program specified with $P4MERGE or $MERGE.');
        }

        system("$merge $base $leg1 $leg2 $result");
    }

    /**
     * Output help text.
     */
    function Help($help)
    {
        $this->Message(implode("\n", $help));
    }
}

/* Create the Perforce Client objects. */
$ui = new ClientUser();
$p4 = new PerforceClient($ui);

/* Shift off the name of the script. */
array_shift($argv);

/* Handle any application-specific options. */
while (!empty($argv) && strncmp($argv[0], '-', 1) === 0) {
    $option = array_shift($argv);

    switch ($option) {
    case '-ztag':
        $p4->setProtocol('tag', '');
        break;
    }
}

/* Initialize the Perforce interface. */
if (!$p4->init()) {
    die("Failed to initialize Perforce client interface.\n");
}

/* Run the command specified on the command line. */
$command = array_shift($argv);
$p4->setArgs($argv);
$p4->run($command);

/* Shut down the Perforce interface. */
$p4->final();
# Change User Description Committed
#1 6142 Jon_Parise Relocating the examples directory to the top level.
//guest/jon_parise/api/php/docs/examples/p4.php
#4 6098 Jon_Parise Updating the example script to handle application-specific command line
options and to better handle tagged output.
#3 6093 Jon_Parise Fixing a small PHP parse error.
#2 6079 Jon_Parise We now use a formal interface to define the UI object.
 This requires PHP5
   so all PHP4 support has been removed.  A number of other small code
   improvements are also included.
#1 4294 Jon_Parise Adding an example command line script and bumping the package's version
number to 1.2.0.