Author: | Jon Parise |
---|---|
Contact: | jon@php.net |
Data: | 2008/03/13 |
Revision: | #13 |
Contents
Perforce is a fast, professional Software Configuration Management system. The Perforce Extension for PHP wraps the public Perforce C/++ API, giving PHP developers direct access to Perforce's native client functions.
The Perforce extension has the following system requirements:
The easiest way to install the Perforce extension is by using the PECL installer:
pecl install perforce
If you're building from source code, should use the --with-perforce configuration option. If your copy of the Perforce C/C++ API hasn't been installed in one of the default locations, you can specify it as a parameter:
--with-perforce=/opt/p4api
More detailed information on installing PECL packages in general can be found in the Installation of PECL Extensions section of the PHP Manual.
This package's source code is hosted in a guest branch of the Perforce Public Depot.
A number of unit tests are included to help maintain correct and expected behavior. The tests can be run using PECL tool's run-tests command:
pecl run-tests -p perforce
Additional unit test contributions are welcome, especially when they increase code coverage or exercise specific use cases.
The Perforce extension provides the PerforceClient class and the PerforceClientUser interface.
All requests to the Perforce server are made through the PerforceClient object, and all responses from the Perforce server are received via methods of the PerforceClientUser interface.
Users must provide their own class which implements the PerforceClientUser interface and then pass an instance of that class to the PerforceClient class's constructor.
class MyClientUser implements PerforceClientUser { ... } $ui = new MyClientUser(); $p4 = new PerforceClient($ui);
Before the PerforceClient instance can be used, it must be initialized. This will open the connection to the Perforce server. Any desired non-default connection parameters (such as the clientspec name or the server's port) must be specified before attempting to initialize the connection.
$p4->setClient('ClientspecName'); $p4->setPort('public.perforce.com:1666'); if ($p4->init() === false) { die('Failed to initialize Perforce server connection'); }
Once the connection has been initialized, commands can be issued to the server using the setArgs() and run() methods.
$p4->setArgs('Jon_Parise'); $p4->run('users');
The run() method will send the command to the server immediately and then wait for the response. When the response is received, the appropriate method (or methods) on the user's PerforceClientUser class will called.
At the end of the session, the connection should be closed.
$p4->final();
More detailed information on the various client methods is provided in this document's Class Reference section.
Creates a new PerforceClient instance which uses the provided object for all user input and output.
Checks if the connection is no longer usable. This is useful if you want to reuse a client connection for multiple commands and need to make sure that the connection is still alive.
If a dropped connection is detected, the actual error message won't be available until final() is called to completely close the connection.
Closes the current connection and returns the final number of errors that occurred during the connection's lifetime.
Gets the current character set.
Gets the current client setting.
Gets the current working directory as set by setCwd().
Gets the client hostname. If a prior call to setHost() has not been made, calling this function may result in reverse DNS lookup.
Gets the current language.
Gets the name of the client operating system. Value operating system names are UNIX, vms, NT, Mac, and null.
Gets the current password setting.
Gets the current port setting for this connection.
Gets the current user setting.
Sets the port to be used for this connection. This must be called prior to creating the connection via init().
Gets the current value of a specific protocol being used by this connection. This should only be called after a call to run(); otherwise, incomplete information will be returned.
The list of supported protocols is detailed in the documentation for setProtocol().
This function will only report protocol variables set by the server, not those variables set by the client via calls to setProtocol().
Establishes a connection to the server specified by setPort() in preparation for running commands via run().
Runs a Perforce command and returns when it completes. Command arguments must be set separately by calling setArgs(). This must be called prior to creating the connection via init().
Sets the user to be used for subsequent commands executed via run().
Sets the password to be used for subsequent commands executed via run().
Sets the client to be used for subsequent commands executed via run().
Gets the name of the current configuration file. If none has been set, the string noconfig will be returned.
Sets a list of arguments that will be passed to a Perforce command executed using run(). This function must be called for each call to run().
This function accepts either multiple individual arguments or an array of arguments.
Sets the character set to be used for subsequent commands executed via run().
Sets the working directory to be used for subsequent commands executed via run().
Sets the hostname to be used for subsequent commands executed via run().
Sets the language to be used for subsequent commands executed via run().
Sets the application or string name for this connection. The program's identity will be reported by the p4 monitor command and recorded in the server's log. This function should be called after init() and prior to calling run().
Sets special protocols for the server to use for this connection. This must be called prior to creating the connection via init().
Protocol | Description |
---|---|
tag | Enables tagged output if set to any value. |
specstring | Enables specially formatted forms if set to any value. |
api | Specifies a specific server API value for this connection. |
Sets the absolute filename of the ticket file to be used for subsequent commands executed via run().
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 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"); }
Display an error message and wait for user to respond.
function ErrorPause($message) { $this->OutputError($message); fgetc(STDIN); }
Handle an error after a failed command.
function HandleError($message, $severity) { fwrite(STDERR, "[$severity] $message\n"); }
Output help text.
function Help($help) { $this->Message(implode("\n", $help)); }
Gather input from the user and return it to the current command.
function InputData() { return fgets(STDIN); }
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 information or errors from the current command.
function Message($message, $severity) { $this->OutputInfo(0, $message); }
Output binary data.
function OutputBinary($data) { fwrite(STDOUT, $data); }
Display an error message.
function OutputError($message) { fwrite(STDERR, "$message\n"); }
Output tabular data.
function OutputInfo($level, $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"); } } }
Output textual data.
function OutputText($data) { fwrite(STDOUT, $data); }
Prompt the user for input and return it to the current command.
function Prompt($message) { echo $message; return fgets(STDIN); }
If you run into a problem or would like to make a suggestion, please use the PECL Bug Tracker. Feel free to contact me directly for other issues, but please try to use the bug tracker whenever possible so that others in the community will benefit from your feedback and my responses.
This section contains a list of "todo" items that will hopefully be addressed in future releases.
If you have feature suggestions, please submit them using the PECL Bug Tracker.