Perforce Chronicle 2012.2/486814
API Documentation

P4_Connection_Extension Class Reference

P4PHP Perforce connection implementation. More...

Inheritance diagram for P4_Connection_Extension:
P4_Connection_Abstract P4_Connection_Interface

List of all members.

Public Member Functions

 __construct ($port=null, $user=null, $client=null, $password=null, $ticket=null)
 Constructs a P4 connection object.
 disconnect ()
 Disconnect from the Perforce Server.
 getConnectionIdentity ()
 Get the identity of this Connection implementation.
 isConnected ()
 Check connected state.
 login ()
 Extends parent to set our instance's password to the returned ticket value if login succeeds.
 setAppName ($name)
 Extended to set app name in p4-php.
 setCharset ($charset)
 Extended to set charset in p4-php.
 setClient ($client)
 Extend set client to update p4-php.
 setHost ($host)
 Extended to set host name in p4-php.
 setPassword ($password)
 Extend set password to update p4-php.
 setPort ($port)
 Extend set port to update p4-php.
 setTicket ($ticket)
 Extend set ticket to update p4-php.
 setUser ($user)
 Extend set user to update p4-php.

Protected Member Functions

 _connect ()
 Does real work of establishing connection.
 _prepareInput ($input, $command)
 Prepare input for passing to the p4 extension.
 _run ($command, $params=array(), $input=null, $tagged=true)
 Actually issues a command.

Protected Attributes

 $_instance

Detailed Description

P4PHP Perforce connection implementation.

This client implementation provides access to the P4PHP extension in a way that conforms to P4_Connection_Interface. This allows the P4PHP extension and the Perforce Command-Line Client wrapper to be used interchangeably.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Constructor & Destructor Documentation

P4_Connection_Extension::__construct ( port = null,
user = null,
client = null,
password = null,
ticket = null 
)

Constructs a P4 connection object.

Parameters:
string$portoptional - the port to connect to.
string$useroptional - the user to connect as.
string$clientoptional - the client spec to use.
string$passwordoptional - the password to use.
string$ticketoptional - a ticket to use.

Reimplemented from P4_Connection_Abstract.

    {
        // ensure that p4-php is installed.
        if (!extension_loaded('perforce')) {
            throw new P4_Exception(
                'Cannot create P4 API extension instance. Perforce extension not loaded.');
        }

        // create an instance of p4-php.
        $this->_instance = new P4;

        // disable automatic sequence expansion (call expandSequences on result object if desired)
        $this->_instance->expand_sequences = false;

        // prevent command exceptions from being thrown by P4.
        // we throw our own so that we can attach the result.
        $this->_instance->exception_level = 0;

        parent::__construct($port, $user, $client, $password, $ticket);
    }

Member Function Documentation

P4_Connection_Extension::_connect ( ) [protected]

Does real work of establishing connection.

Called by connect().

Exceptions:
P4_Connection_ConnectExceptionif the connection fails.

Reimplemented from P4_Connection_Abstract.

    {
        // temporarily enable exceptions to catch connection failure.
        $this->_instance->exception_level = 1;
        try {
            $this->_instance->connect();
            $this->_instance->exception_level = 0;
        } catch (P4_Exception $e) {
            $this->_instance->exception_level = 0;
            throw new P4_Connection_ConnectException(
                "Connect failed: " . $e->getMessage()
            );
        }
    }
P4_Connection_Extension::_prepareInput ( input,
command 
) [protected]

Prepare input for passing to the p4 extension.

Ensure input is either a string or an array of strings.

Parameters:
string | array$inputthe input to prepare for p4.
string$commandthe command to prepare input for.
Returns:
string|array the prepared input.

Reimplemented from P4_Connection_Abstract.

    {
        // if input is not an array, cast to string and return.
        if (!is_array($input)) {
            return (string) $input;
        }

        // ensure each element of array is a string.
        $stringify = function(&$input)
        {
            $input = (string) $input;
        };
        array_walk_recursive($input, $stringify);

        return $input;
    }
P4_Connection_Extension::_run ( command,
params = array(),
input = null,
tagged = true 
) [protected]

Actually issues a command.

Called by run() to perform the dirty work.

Parameters:
string$commandthe command to run.
array$paramsoptional - arguments.
array | string$inputoptional - input for the command - should be provided in array form when writing perforce spec records.
boolean$taggedoptional - true/false to enable/disable tagged output. defaults to true.
Returns:
P4_Result the perforce result object.

Reimplemented from P4_Connection_Abstract.

    {
        // push command to front of parameters array
        array_unshift($params, $command);

        // set input for the command.
        if ($input !== null) {
            $this->_instance->input = $input;
        }

        // toggle tagged output.
        $this->_instance->tagged = (bool) $tagged;

        // establish connection to perforce server.
        if (!$this->isConnected()) {
            $this->connect();
        }

        // run command.
        $data = call_user_func_array(array($this->_instance, "run"), $params);

        // collect data in result object and ensure output is in array form.
        $result = new P4_Result($command, $data, $tagged);
        $result->setErrors($this->_instance->errors);
        $result->setWarnings($this->_instance->warnings);

        return $result;
    }
P4_Connection_Extension::disconnect ( )

Disconnect from the Perforce Server.

Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        // call parent to run disconnect callbacks.
        parent::disconnect();

        if ($this->isConnected()) {
            $this->_instance->disconnect();
        }

        return $this;
    }
P4_Connection_Extension::getConnectionIdentity ( )

Get the identity of this Connection implementation.

Resulting array will contain:

  • name
  • platform
  • version (p4-php version)
  • build (p4-php build)
  • apiversion (p4-api version)
  • apibuild (p4-api build)
  • date
  • original (all text following 'Rev. ' from original response)
Returns:
array an array of client Connection information
Exceptions:
P4_Exceptionif the returned version string is invalid

Implements P4_Connection_Interface.

    {
        // obtain the extension's identification
        $output = $this->_instance->identify();

        // extract the version string and split into components
        preg_match('/\nRev. (.*)\.$/', $output, $matches);
        $parts = isset($matches[1]) ? preg_split('/\/| \(| API\) \(|\)/', $matches[1]) : null;
        if (count($parts) < 8) {
            $message = 'p4php returned an invalid version string';
            throw new P4_Exception($message);
        }

        // build identity array of version components, including original string
        $identity = array(
            'name'       => $parts[0],
            'platform'   => $parts[1],
            'version'    => $parts[2],
            'build'      => $parts[3],
            'apiversion' => $parts[4],
            'apibuild'   => $parts[5],
            'date'       => $parts[6] . '/' . $parts[7] . '/' . $parts[8],
            'original'   => $matches[1]
        );

        return $identity;
    }
P4_Connection_Extension::isConnected ( )

Check connected state.

Returns:
bool true if connected, false otherwise.

Implements P4_Connection_Interface.

    {
        return $this->_instance->connected();
    }
P4_Connection_Extension::login ( )

Extends parent to set our instance's password to the returned ticket value if login succeeds.

Returns:
string|null the ticket issued by the server or null if no ticket issued (ie. user has no password).
Exceptions:
P4_Connection_LoginExceptionif login fails.

Reimplemented from P4_Connection_Abstract.

    {
        $ticket = parent::login();

        if ($ticket) {
            $this->_instance->password = $ticket;
        }

        return $ticket;
    }
P4_Connection_Extension::setAppName ( name)

Extended to set app name in p4-php.

Set the name of the application that is using this connection.

Parameters:
string | null$namethe app name to report to the server.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        $this->_instance->set_protocol('app', (string) $name);

        return parent::setAppName($name);
    }
P4_Connection_Extension::setCharset ( charset)

Extended to set charset in p4-php.

Sets the character set to use for this perforce connection.

You should only set a character set when connecting to a 'unicode enabled' server, or when setting the special value of 'none'.

Parameters:
string$charsetthe charset to use (e.g. 'utf8').
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        $this->_instance->charset = $charset;

        return parent::setCharset($charset);
    }
P4_Connection_Extension::setClient ( client)

Extend set client to update p4-php.

Parameters:
string$clientthe name of the client workspace to use.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        parent::setClient($client);

        // if no client is specified, normally the host name is used.
        // this can collide with an existing depot or client name, so
        // we use a temp id to avoid errors.
        $this->_instance->client = $this->getClient() ?: P4_Client::makeTempId();

        return $this;
    }
P4_Connection_Extension::setHost ( host)

Extended to set host name in p4-php.

Sets the client host name overriding the environment.

Parameters:
string | null$hostthe host name to use.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        $this->_instance->host = $host;

        return parent::setHost($host);
    }
P4_Connection_Extension::setPassword ( password)

Extend set password to update p4-php.

Parameters:
string$passwordthe password to use as authentication.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        parent::setPassword($password);
        $this->_instance->password = $this->getPassword();

        return $this;
    }
P4_Connection_Extension::setPort ( port)

Extend set port to update p4-php.

Parameters:
string$portthe port to connect to.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        parent::setPort($port);
        $this->_instance->port = $this->getPort();

        return $this;
    }
P4_Connection_Extension::setTicket ( ticket)

Extend set ticket to update p4-php.

Note: the ticket is stored in the password field in p4-php.

Parameters:
string$ticketthe ticket to use as authentication.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        parent::setTicket($ticket);
        if ($ticket) {
            $this->_instance->password = $this->getTicket();
        }

        return $this;
    }
P4_Connection_Extension::setUser ( user)

Extend set user to update p4-php.

Parameters:
string$userthe user to connect as.
Returns:
P4_Connection_Interface provides fluent interface.

Reimplemented from P4_Connection_Abstract.

    {
        parent::setUser($user);
        $this->_instance->user = $this->getUser();

        return $this;
    }

Member Data Documentation

P4_Connection_Extension::$_instance [protected]

The documentation for this class was generated from the following file: