PHP Class Jackalope\Item

Jackalope Implementation: The item has a state machine to track in what state it currently is. All API exposed methods must call Item::checkState() before doing anything. Most important is that everything that is in state deleted can not be used anymore (will detect logic errors in client code) and that if the item needs to be refreshed from the backend, this can be postponed until the item is actually accessed again. Figure: workflow state transitions For the special case of Item state after a failed transaction, see Item::rollbackTransaction()
Inheritance: implements PHPCR\ItemInterface
Show file Open project: jackalope/jackalope Class Usage Examples

Protected Properties

Property Type Description
$available_states The states an Item can take
$depth Depth in the workspace graph
$factory The jackalope object factory for this object
$isNode Whether this item is a node (otherwise it is a property)
$keepChanges To know whether to keep changes or not when reloading in dirty state
$name the node or property name
$new false if item is read from backend, true if created locally in this session
$objectManager The object manager to get nodes and properties from
$oldPath While this item is moved but unsaved, stores the old path for refresh.
$parentPath Normalized and absolute path to the parent item for convenience.
$path Normalized and absolute path to this item.
$postDirtyState The state to take after this dirty node has been refreshed. One of the STATE_ constants
$savedState The state of the item saved when a transaction is started
$session The session this item belongs to
$state The state of the item, one of the STATE_ constants
$valueConverter PHPCR\Util\ValueConverter

Public Methods

Method Description
accept ( PHPCR\ItemVisitorInterface $visitor ) {@inheritDoc}
beginTransaction ( ) Manage item state when transaction starts. This method is called on every cached item by the ObjectManager.
commitTransaction ( ) Clean up state after a transaction. This method is called on every cached item by the ObjectManager.
confirmSaved ( ) notify this item that it has been saved into the backend.
getAncestor ( $depth ) {@inheritDoc}
getDepth ( ) {@inheritDoc}
getName ( ) {@inheritDoc}
getParent ( ) {@inheritDoc}
getPath ( ) {@inheritDoc}
getSession ( ) {@inheritDoc}
getState ( ) : integer Get the state of the item
isClean ( ) : boolean Whether this item is in STATE_CLEAN (meaning its data is fully synchronized with the backend)
isDeleted ( ) : boolean Whether this item has been deleted and can not be used anymore.
isDirty ( ) : boolean Whether this item is in state dirty.
isModified ( ) {@inheritDoc}
isMoved ( ) {@inheritDoc}
isNew ( ) {@inheritDoc}
isNode ( ) {@inheritDoc}
isSame ( PHPCR\ItemInterface $otherItem ) {@inheritDoc}
remove ( ) {@inheritDoc}
revert ( ) {@inheritDoc}
rollbackTransaction ( ) Adjust the correct item state after a transaction rollback. This method is called on every cached item by the ObjectManager.
setClean ( ) Tell this item it is clean (i.e. it has been refreshed after a modification)
setDeleted ( ) Tell this item it has been deleted and cannot be used anymore
setDirty ( boolean $keepChanges = false, $targetState = false ) Tell this item that it is dirty and needs to be refreshed
setModified ( ) Tell this item that it has been modified.
setPath ( string $path, boolean $move = false ) Set or update the path, depth, name and parent reference

Protected Methods

Method Description
__construct ( jackalope\FactoryInterface $factory, string $path, jackalope\Session $session, ObjectManager $objectManager, boolean $new = false ) Initialize basic information common to nodes and properties
checkState ( ) This function will modify the state of the item as well as refresh it if necessary (i.e.
findItemDefinition ( callable $definitions ) : PHPCR\NodeType\ItemDefinitionInterface Find the matching item definition for this item.
refresh ( boolean $keepChanges ) Updates the state of the current item.

Private Methods

Method Description
setState ( integer $state ) Change the state of the item

Method Details

__construct() protected method

Initialize basic information common to nodes and properties
protected __construct ( jackalope\FactoryInterface $factory, string $path, jackalope\Session $session, ObjectManager $objectManager, boolean $new = false )
$factory jackalope\FactoryInterface the object factory
$path string The normalized and absolute path to this item
$session jackalope\Session
$objectManager ObjectManager
$new boolean can be set to true to tell the object that it has been created locally

accept() public method

{@inheritDoc}
public accept ( PHPCR\ItemVisitorInterface $visitor )
$visitor PHPCR\ItemVisitorInterface

beginTransaction() public method

Saves the current item state in case a rollback occurs.
See also: Item::rollbackTransaction
public beginTransaction ( )

checkState() protected method

if it is DIRTY).
protected checkState ( )

commitTransaction() public method

Clean up state after a transaction. This method is called on every cached item by the ObjectManager.
See also: Item::rollbackTransaction
public commitTransaction ( )

confirmSaved() public method

allowing it to clear the modified / new flags
public confirmSaved ( )

findItemDefinition() protected method

Find the matching item definition for this item.
protected findItemDefinition ( callable $definitions ) : PHPCR\NodeType\ItemDefinitionInterface
$definitions callable Function that extracts the ItemDefinitions from a NodeType
return PHPCR\NodeType\ItemDefinitionInterface The definition for this item.

getAncestor() public method

{@inheritDoc}
public getAncestor ( $depth )

getDepth() public method

{@inheritDoc}
public getDepth ( )

getName() public method

{@inheritDoc}
public getName ( )

getParent() public method

{@inheritDoc}
public getParent ( )

getPath() public method

{@inheritDoc}
public getPath ( )

getSession() public method

{@inheritDoc}
public getSession ( )

getState() public method

Get the state of the item
public getState ( ) : integer
return integer one of the state constants

isClean() public method

Whether this item is in STATE_CLEAN (meaning its data is fully synchronized with the backend)
public isClean ( ) : boolean
return boolean

isDeleted() public method

Whether this item has been deleted and can not be used anymore.
public isDeleted ( ) : boolean
return boolean

isDirty() public method

Returns true if this Item has been marked dirty (i.e. being saved) and has not been refreshed since. The in-memory representation of the item in memory might not reflect the current state in the backend (for instance if mix:referenceable mixin type has been added to the item the backend creates a UUID on save).
public isDirty ( ) : boolean
return boolean

isModified() public method

{@inheritDoc}
public isModified ( )

isMoved() public method

{@inheritDoc}
public isMoved ( )

isNew() public method

{@inheritDoc}
public isNew ( )

isNode() public method

{@inheritDoc}
public isNode ( )

isSame() public method

{@inheritDoc}
public isSame ( PHPCR\ItemInterface $otherItem )
$otherItem PHPCR\ItemInterface

refresh() abstract protected method

In JSR-283 this was part of the interface. While JSR-333 deprecated the refresh() method and replaces it with revert(), the functionality is still needed as Session::refresh() has been kept. If keepChanges is false, this method discards all pending changes currently recorded in this Session that apply to this Item or any of its descendants (that is, the subgraph rooted at this Item) and returns all items to reflect the current saved state. Outside a transaction this state is simply the current state of persistent storage. Within a transaction, this state will reflect persistent storage as modified by changes that have been saved but not yet committed. If keepChanges is true then pending change are not discarded but items that do not have changes pending have their state refreshed to reflect the current saved state, thus revealing changes made by other sessions.
abstract protected refresh ( boolean $keepChanges )
$keepChanges boolean a boolean

remove() public method

{@inheritDoc}
public remove ( )

revert() public method

{@inheritDoc}
public revert ( )

rollbackTransaction() public method

Item state represents the state of an in-memory item. This has nothing to do with the state of the item in the backend. Referring to the JCR spec (21.3 Save vs. Commit) a transaction rollback or commit will not change the in-memory state of items, but only the backend. When a transaction is rolled back, we try to correct the state of in-memory items so that the session could be correctly saved if no more constraint violations remain. Note that this does not fully work yet. On Item::beginTransaction() we save the current state into savedState. On a rollback, we basically go back to the saved state, with a couple of exceptions. The following table shows an ordered list of rules - the first match is used. The * denotes any state.
#$savedState$state Resulting $state
1DELETED * DELETED
2* DELETED DELETED
3NEW * NEW
4* MODIFIEDMODIFIED
5MODIFIED * MODIFIED
6CLEAN CLEAN CLEAN (if the item was not modified in the TRX)
7CLEAN CLEAN MODIFIED (if the item was modified in the TRX)
note: case 7 is handled in Item::setState() by changing $savedState to MODIFIED if $savedState is CLEAN and current state changes to MODIFIED Without this special case, we would miss the situation where a clean node is modified after transaction start and successfully saved, ending up with clean state again. it has to be modified as its different from the backend value.
8CLEAN DIRTY DIRTY
9DIRTY * DIRTY
See also: ObjectManager::rollbackTransaction()
public rollbackTransaction ( )

setClean() public method

Tell this item it is clean (i.e. it has been refreshed after a modification)
public setClean ( )

setDeleted() public method

Tell this item it has been deleted and cannot be used anymore
public setDeleted ( )

setDirty() public method

Tell this item that it is dirty and needs to be refreshed
public setDirty ( boolean $keepChanges = false, $targetState = false )
$keepChanges boolean whether to keep changes when reloading or not

setModified() public method

This will do nothing if the node is new, to avoid duplicating store commands.
public setModified ( )

setPath() public method

Set or update the path, depth, name and parent reference
public setPath ( string $path, boolean $move = false )
$path string the new path this item lives at
$move boolean whether this item is being moved in session context and should store the current path until the next save operation.

Property Details

$available_states protected property

The states an Item can take
protected $available_states

$depth protected property

Depth in the workspace graph
protected $depth

$factory protected property

The jackalope object factory for this object
protected $factory

$isNode protected property

Whether this item is a node (otherwise it is a property)
protected $isNode

$keepChanges protected property

To know whether to keep changes or not when reloading in dirty state
protected $keepChanges

$name protected property

the node or property name
protected $name

$new protected property

false if item is read from backend, true if created locally in this session
protected $new

$objectManager protected property

The object manager to get nodes and properties from
protected $objectManager

$oldPath protected property

While this item is moved but unsaved, stores the old path for refresh.
protected $oldPath

$parentPath protected property

Normalized and absolute path to the parent item for convenience.
protected $parentPath

$path protected property

Normalized and absolute path to this item.
protected $path

$postDirtyState protected property

The state to take after this dirty node has been refreshed. One of the STATE_ constants
protected $postDirtyState

$savedState protected property

The state of the item saved when a transaction is started
See also: Item::rollbackTransaction()
protected $savedState

$session protected property

The session this item belongs to
protected $session

$state protected property

The state of the item, one of the STATE_ constants
protected $state

$valueConverter protected property

protected ValueConverter,PHPCR\Util $valueConverter
return PHPCR\Util\ValueConverter