Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Record_Adapter Class Reference

Encapsulates everything needed for Records to read from and write to storage. More...

Inheritance diagram for P4Cms_Record_Adapter:
P4Cms_Record_DeferredAdapter

List of all members.

Public Member Functions

 beginBatch ($description)
 Start a batch.
 commitBatch ($description=null, $options=null)
 Commit the batch.
 getBasePath ()
 Get the base path in Perforce under which records should be read from and written to.
 getBatchId ()
 Get the id of the current batch.
 getConnection ()
 Get the connection object to use to communicate with Perforce.
 getProperties ()
 Get all properties of this adapter.
 getProperty ($name)
 Get a particular property value of this adapter.
 hasProperty ($name)
 Check if adapter has a particular property.
 inBatch ()
 Determine if we are currently in a batch on this storage adapter.
 revertBatch ()
 Reverts all files in the pending change corresponding to the batch id.
 setBasePath ($path)
 Set the base path in Perforce under which records should be read from and written to.
 setBatchDescription ($description)
 Change the description of the current batch.
 setConnection ($p4)
 Set the Perforce connection to use to communicate with the Perforce backend.
 setProperties (array $properties)
 Set adapter properties.
 setProperty ($name, $value)
 Set a particular property of this adapter.

Public Attributes

const COMMIT_THROW_CONFLICT = "throw"

Protected Attributes

 $_basePath = null
 $_batchId = null
 $_connection = null
 $_properties = array()

Detailed Description

Encapsulates everything needed for Records to read from and write to storage.

Specifically, a p4 connection, a base path in perforce, and a prefix for sequence counters.

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

Member Function Documentation

P4Cms_Record_Adapter::beginBatch ( description)

Start a batch.

Changes made to records will be placed in a numbered pending change and will not be submitted until the batch is committed.

Batches cannot be nested. Attempting to begin a batch while in a batch will result in an exception.

Parameters:
string$descriptionrequired - a description of the batch.
Returns:
P4Cms_Record_Adapter provides fluent interface.
Exceptions:
P4Cms_Record_Exceptionif already in a batch.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if ($this->inBatch()) {
            throw new P4Cms_Record_Exception(
                "Cannot begin batch. Already in a batch."
            );
        }

        // create a new pending change.
        $change = new P4_Change($this->getConnection());
        $change->setDescription($description)->save();

        return $this->_batchId = $change->getId();
    }
P4Cms_Record_Adapter::commitBatch ( description = null,
options = null 
)

Commit the batch.

Submits the pending change corresponding to the batch id.

Parameters:
string$descriptionoptional - a final description of the batch.
null | string | array$optionsoptional - passing the SAVE_THROW_CONFLICTS flag will cause exceptions on conflict; default behaviour is to crush any conflicts.
Returns:
P4Cms_Record_Adapter provides fluent interface.
Exceptions:
P4Cms_Record_Exceptionif not in a batch.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!$this->inBatch()) {
            throw new P4Cms_Record_Exception(
                "Cannot commit batch. Not in a batch."
            );
        }

        // submit the change identified by batch id.
        $change = P4_Change::fetch(
            $this->getBatchId(),
            $this->getConnection()
        );
        try {
            // default option is to 'accept yours' but we switch to
            // null if SAVE_THROW_CONFLICTS flag is passed.
            $resolveFlag = P4_Change::RESOLVE_ACCEPT_YOURS;
            if (in_array(static::COMMIT_THROW_CONFLICT, (array)$options)) {
                $resolveFlag = null;
            }

            $change->submit($description, $resolveFlag);
        } catch (P4_Connection_CommandException $e) {
            // ignore exception if change is empty - otherwise rethrow.
            if (count($change->getFiles()) == 0) {
                $change->delete();
            } else {
                throw $e;
            }
        }

        // clear the batch id.
        $this->_batchId = null;

        return $this;
    }
P4Cms_Record_Adapter::getBasePath ( )

Get the base path in Perforce under which records should be read from and written to.

Returns:
string the record storage base path.
Exceptions:
P4Cms_Record_Exceptionif no base path is set.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!is_string($this->_basePath) || !strlen($this->_basePath)) {
            throw new P4Cms_Record_Exception(
                "Cannot get base path. No base path is set."
            );
        }

        return $this->_basePath;
    }
P4Cms_Record_Adapter::getBatchId ( )

Get the id of the current batch.

The batch id corresponds to a pending changelist in Perforce.

Returns:
int the batch id (pending change number).
Exceptions:
P4Cms_Record_Exceptionif not in a batch.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!$this->inBatch()) {
            throw new P4Cms_Record_Exception(
                "Cannot get batch id. Not in a batch."
            );
        }

        return (int) $this->_batchId;
    }
P4Cms_Record_Adapter::getConnection ( )

Get the connection object to use to communicate with Perforce.

Returns:
P4_Connection_Abstract the p4 connection to use.
Exceptions:
P4Cms_Record_Exceptionif no valid connection is set.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!$this->_connection instanceof P4_Connection_Abstract) {
            throw new P4Cms_Record_Exception(
                "Cannot get connection. No valid p4 connection has been set."
            );
        }

        return $this->_connection;
    }
P4Cms_Record_Adapter::getProperties ( )

Get all properties of this adapter.

Returns:
array all properties set to this adapter

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        return $this->_properties;
    }
P4Cms_Record_Adapter::getProperty ( name)

Get a particular property value of this adapter.

Parameters:
string$namename of the property to get the value of
Returns:
mixed the value of the property name
Exceptions:
P4Cms_Record_Exceptionif the property name does not exist

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        // return property value if it was set, otherwise throw an exception
        if ($this->hasProperty($name)) {
            return $this->_properties[$name];
        }

        throw new P4Cms_Record_Exception(
            "Cannot find adapter property '$name'. Property was not set."
        );
    }
P4Cms_Record_Adapter::hasProperty ( name)

Check if adapter has a particular property.

Parameters:
string$namethe property name to check for the existence of
Returns:
boolean true if the adapter has the named property, false otherwise.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        return array_key_exists($name, $this->_properties);
    }
P4Cms_Record_Adapter::inBatch ( )

Determine if we are currently in a batch on this storage adapter.

Returns:
bool true if we are in a batch.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        $id = $this->_batchId;
        return (int) $id > 0;
    }
P4Cms_Record_Adapter::revertBatch ( )

Reverts all files in the pending change corresponding to the batch id.

Returns:
P4Cms_Record_Adapter provides fluent interface.
Exceptions:
P4Cms_Record_Exceptionif not in a batch.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!$this->inBatch()) {
            throw new P4Cms_Record_Exception(
                "Cannot revert batch. Not in a batch."
            );
        }

        // revert the change identified by batch id.
        $change = P4_Change::fetch(
            $this->getBatchId(),
            $this->getConnection()
        );
        $change->revert()
               ->delete();

        // clear the batch id.
        $this->_batchId = null;

        return $this;
    }
P4Cms_Record_Adapter::setBasePath ( path)

Set the base path in Perforce under which records should be read from and written to.

Parameters:
string$paththe record storage base path.
Returns:
P4Cms_Record_Adapter provides fluent interface.
Exceptions:
P4Cms_Record_Exceptionif the base path is not a valid string.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!is_string($path) || !strlen($path)) {
            throw new P4Cms_Record_Exception(
                "Cannot set base path. Given path is not a valid string."
            );
        }

        $this->_basePath = $path;
        return $this;
    }
P4Cms_Record_Adapter::setBatchDescription ( description)

Change the description of the current batch.

Parameters:
string$descriptionthe description of the current batch.
Returns:
P4Cms_Record_Adapter provides fluent interface.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        $change = P4_Change::fetch(
            $this->getBatchId(),
            $this->getConnection()
        );
        $change->setDescription($description)
               ->save();

        return $this;
    }
P4Cms_Record_Adapter::setConnection ( p4)

Set the Perforce connection to use to communicate with the Perforce backend.

Parameters:
P4_Connection_Abstract$p4the p4 connection to use.
Returns:
P4Cms_Record_Adapter provides fluent interface.
Exceptions:
P4Cms_Record_Exceptionif the connection object is invalid.

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        if (!$p4 instanceof P4_Connection_Abstract) {
            throw new P4Cms_Record_Exception(
                "Cannot set connection. The given argument is not a valid p4 connection."
            );
        }

        $this->_connection = $p4;
        return $this;
    }
P4Cms_Record_Adapter::setProperties ( array $  properties)

Set adapter properties.

Parameters:
array$propertiesarray with properties to set
Returns:
P4Cms_Record_Adapter provides fluent interface

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        $this->_properties = $properties;
        return $this;
    }
P4Cms_Record_Adapter::setProperty ( name,
value 
)

Set a particular property of this adapter.

Parameters:
string$namename of the property to set the value of
mixed$valuevalue to set
Returns:
P4Cms_Record_Adapter provides a fluent interface

Reimplemented in P4Cms_Record_DeferredAdapter.

    {
        $this->_properties[$name] = $value;
        return $this;
    }

Member Data Documentation

P4Cms_Record_Adapter::$_basePath = null [protected]
P4Cms_Record_Adapter::$_batchId = null [protected]
P4Cms_Record_Adapter::$_connection = null [protected]
P4Cms_Record_Adapter::$_properties = array() [protected]

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