/** * @inheritDoc */ public function purge() { $session = $this->dm->getPhpcrSession(); NodeHelper::deleteAllNodes($session); $session->save(); }
/** * Helper method for importing to add a node with the proper uuid behavior * * @param \PHPCR\NodeInterface $parentNode the node to add this node to * @param string $nodename the node name to use * @param string $type the primary type name to use * @param int $uuidBehavior one of the constants of ImportUUIDBehaviorInterface * * @return NodeInterface the created node * * @throws \PHPCR\ItemExistsException if IMPORT_UUID_COLLISION_THROW and * duplicate id * @throws \PHPCR\NodeType\ConstraintViolationException if behavior is remove or * replace and the node with the uuid is in the parent path. */ private static function addNode(NodeInterface $parentNode, $nodename, $type, $properties, $uuidBehavior) { $forceReferenceable = false; if (isset($properties['jcr:uuid'])) { try { $existing = $parentNode->getSession()->getNodeByIdentifier($properties['jcr:uuid']['values']); switch ($uuidBehavior) { case self::IMPORT_UUID_CREATE_NEW: unset($properties['jcr:uuid']); $forceReferenceable = true; break; case self::IMPORT_UUID_COLLISION_THROW: throw new ItemExistsException('There already is a node with uuid ' . $properties['jcr:uuid']['values'] . ' in this workspace.'); case self::IMPORT_UUID_COLLISION_REMOVE_EXISTING: case self::IMPORT_UUID_COLLISION_REPLACE_EXISTING: if (self::IMPORT_UUID_COLLISION_REPLACE_EXISTING == $uuidBehavior && 'jcr:root' == $nodename && $existing->getDepth() == 0) { break; } if (!strncmp($existing->getPath() . '/', $parentNode->getPath() . "/{$nodename}", strlen($existing->getPath() . '/'))) { throw new ConstraintViolationException('Trying to remove/replace parent of the path we are adding to. ' . $existing->getIdentifier() . ' at ' . $existing->getPath()); } if (self::IMPORT_UUID_COLLISION_REPLACE_EXISTING == $uuidBehavior) { // replace the found node. spec is not precise: do we keep the name or use the one of existing? $parentNode = $existing->getParent(); } $existing->remove(); break; default: // @codeCoverageIgnoreStart throw new RepositoryException("Unexpected type {$uuidBehavior}"); // @codeCoverageIgnoreEnd } } catch (\PHPCR\ItemNotFoundException $e) { // nothing to do, we can add the node without conflict } } if ('jcr:root' == $nodename && isset($existing) && $existing->getDepth() == 0 && self::IMPORT_UUID_COLLISION_REPLACE_EXISTING == $uuidBehavior) { // update the root node properties // http://www.day.com/specs/jcr/2.0/11_Import.html#11.9%20Importing%20%3CI%3Ejcr:root%3C/I%3E NodeHelper::deleteAllNodes($parentNode->getSession()); $node = $existing; } else { // logging echo "Adding node $nodename ($type)\n"; $node = $parentNode->addNode($nodename, $type); } foreach ($properties as $name => $info) { // logging echo "Adding property $name\n"; if ('jcr:primaryType' == $name) { // handled in node constructor } else { if ('jcr:mixinTypes' == $name) { if (is_array($info['values'])) { foreach ($info['values'] as $type) { $node->addMixin($type); } } else { $node->addMixin($info['values']); } } else { if ('jcr:created' == $name || 'jcr:createdBy' == $name) { // skip PROTECTED properties. TODO: get the names from node type instead of hardcode } else { if ('jcr:uuid' == $name) { //avoid to throw an exception when trying to set a UUID when importing from XML $node->setProperty($name, $info['values'], $info['type'], false); } else { $node->setProperty($name, $info['values'], $info['type']); } } } } } if ($forceReferenceable && !$node->isNodeType('mix:referenceable')) { $node->addMixin('mix:referenceable'); } return $node; }