p4.php #2

  • //
  • guest/
  • tony_smith/
  • perforce/
  • API/
  • php/
  • docs/
  • examples/
  • p4.php
  • View
  • Commits
  • Open Download .zip Download (4 KB)
<?php

// Load the module if necessary
if (!extension_loaded('PerforceClient')) 
{
    if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) 
    {
	dl('perforce.dll');
    } 
    else 
    {
        dl('perforce.so');
    }
}

//
// Simple function to show the results of a p4 command. Useful
// for testing.
//
function show_results( $p4 )
{
    $o  = $p4->Output();
    $e  = $p4->Errors();
    $w  = $p4->Warnings();

    $co = count( $o );
    $ce = count( $e );
    $cw = count( $w );

    printf( "There were %d results, %d errors and %d warnings\n\n", 
    		$co, $ce, $cw );

    for( $idx = 0; $idx < $co; $idx++ )
    {
	printf( "RESULT[ %d ]:\n", $idx );

	if( is_array( $o[ $idx ] ) )
	{
	    reset( $o[ $idx ] );
	    while( list( $key, $val ) = each( $o[ $idx ] ) )
	    {
		printf( "\t%s\t-> %s\n", $key, $val );
	    }
	}
	else
	{
	    printf( "\t%s\n",  $o[ $idx ] );
	}
	printf( "\n" );
    }

    if( $ce == 0 && $cw == 0 ) return;


    if( $cw )
    {
	for( $idx = 0; $idx < $cw; ++$idx )
	{
	    printf( "WARNING[ %d ]: %s\n", $idx, $w[ $idx ] );
	}
    }
    if( $ce )
    {
	for( $idx = 0; $idx < $ce; ++$idx )
	{
	    printf( "ERROR[ %d ]: %s\n", $idx, $e[ $idx ] );
	}
    }

}

//------------------------------------------------------------------------------
// Start of main script
//------------------------------------------------------------------------------
$p4 = new PerforceClient();
$p4->DebugLevel( 0 );

// Defaults to P4PORT, but here we override.
$p4->SetPort( "localhost:1666" );

//------------------------------------------------------------------------------
// Enable tagged mode. Mostly you want this or ParseForms() mode. Without
// one or the other, you'll be parsing a lot of output by hand.
//------------------------------------------------------------------------------
$p4->Tagged();

//------------------------------------------------------------------------------
// Will convert Perforce forms into hashes and vice-versa. Implies tagged
// mode.
//------------------------------------------------------------------------------
$p4->ParseForms();

//------------------------------------------------------------------------------
// Connect to server
//------------------------------------------------------------------------------
if( ! $p4->Connect() )
{
    echo( "Failed to connect\n" );
    exit( 0 );
}
    
//------------------------------------------------------------------------------
// Run a 'p4 info' and print the results
//------------------------------------------------------------------------------
$res = $p4->Run( "info" );
show_results( $p4 );


//------------------------------------------------------------------------------
// Fetch the current definitions of our user account. Note
// that Run() returns an ARRAY, so if you're running a
// command that only returns one result, you must shift/pop/index
// the array
//------------------------------------------------------------------------------
$res = $p4->Run( "user", "-o" );
$res = array_shift( $res );
show_results( $p4 );

//------------------------------------------------------------------------------
// Now we can modify the hash we got back and supply it as 
// input to a Perforce command. This example is commented
// out by default so that this script is not invasive.
//------------------------------------------------------------------------------

// $res[ "Email" ] = "someone@somewhere.com";
// $p4->SetInput( $res );
// $p4->Run( "user", "-i" );
// show_results( $p4 );

//------------------------------------------------------------------------------
// Similarly, you can do the same thing to submit a change
//------------------------------------------------------------------------------

// $change = array_shift( $p4->Run( "change", "-o" ) );
// $change[ "Description" ] = "Some change I wanted to submit";
// $p4->SetInput( $change );
// $p4->Run( "submit", "-i" );

$p4->Disconnect();
?>
# Change User Description Committed
#2 4627 Tony Smith Rework Jon Parise's PHP interface to work like P4Perl and P4Ruby,
mostly. It's pretty close, but lacking method autoloading so
the only way to run commands is through the Run() method. This
should be considered early beta quality at the moment, Jon will
be pulling the bits he likes  back into his part of the depot after
he's reviewed it.
#1 4595 Tony Smith Copy Jon Parise's PHP integration into my directory.
//guest/jon_parise/api/php/docs/examples/p4.php
#1 4294 Jon_Parise Adding an example command line script and bumping the package's version
number to 1.2.0.