/** * Remove the parent node of multiple child level */ public function testRemove3() { $parent = new ChildTestObj(); $parent->name = 'Parent'; $parent->id = '/functional/childtest'; // Lv1 $childLv1 = new ChildReferenceableTestObj(); $childLv1->name = 'Child-Lv1'; $parent->child = $childLv1; // Lv2 $childLv2 = new ChildChildTestObj(); $childLv2->name = 'Child-Lv2'; $childLv1->aChild = $childLv2; $this->dm->persist($parent); $this->dm->flush(); $this->dm->clear(); $parent = $this->dm->find($this->type, '/functional/childtest'); $this->assertTrue($this->node->hasNode('childtest')); $this->assertTrue($this->node->getNode('childtest')->hasNode('test')); $this->assertTrue($this->node->getNode('childtest')->getNode('test')->hasNode('test')); $this->dm->remove($parent); $this->dm->flush(); $this->dm->clear(); $parent = $this->dm->find($this->type, '/functional/childtest'); $this->assertNull($parent); }
/** * Gets a relative filesystem path based on the repository path, AND * creates the file on the filesystem if it's in the repository * and not yet on the filesystem. * The repository path points to a nt-resource node, whose title * should be the filename, and which has a child+property * jcr:content/jcr:data where the file data is stored. * * @param string $path path to the nt-resource node. * @return string with a path to the file, relative to the web directory. */ public function getUrl(NodeInterface $node) { $hasData = false; if ($node->hasNode('jcr:content')) { $contentNode = $node->getNode('jcr:content'); if ($contentNode->hasProperty('jcr:data')) { $hasData = true; } } if (!$hasData) { //TODO: notfound exception is not appropriate ... how to best do this? //throw new NotFoundHttpException('no picture found at ' . $node->getPath()); return 'notfound'; } $path = $node->getPath(); $relativePath = $this->pathMapper->getUrl($path); $extension = $this->getExtension($contentNode); $fullPath = $this->fileBasePath . '/' . $relativePath . $extension; if (!file_exists($fullPath)) { if (!$this->saveData($contentNode, $fullPath)) { throw new FileException('failed to save data to file: ' . $fullPath); } } return $this->webRelativePath . '/' . $relativePath . $extension; }
/** * @param NodeInterface $parentNode * @param string $name * @param null|NodeInterface $forNode * * @return string */ public function resolveName(NodeInterface $parentNode, $name, $forNode = null) { if ($forNode && $parentNode->hasNode($name) && $parentNode->getNode($name) == $forNode) { return $name; } $index = 0; $baseName = $name; do { if ($index > 0) { $name = $baseName . '-' . $index; } $hasChild = $parentNode->hasNode($name); ++$index; } while ($hasChild); return $name; }
/** * Gets a relative filesystem path based on the repository path, AND * creates the file on the filesystem if it's in the repository * and not yet on the filesystem. * The repository path points to a nt-resource node, whose title * should be the filename, and which has a child+property * jcr:content/jcr:data where the file data is stored. * * @param string $path path to the nt-resource node. * @return string with a path to the file, relative to the web directory. */ public function getUrl(NodeInterface $node) { $hasData = false; if ($node->hasNode('jcr:content')) { $contentNode = $node->getNode('jcr:content'); if ($contentNode->hasProperty('jcr:data')) { $hasData = true; } } if (!$hasData) { //TODO: notfound exception is not appropriate ... how to best do this? //throw new NotFoundHttpException('no picture found at ' . $node->getPath()); return 'notfound'; } $path = $node->getPath(); $relativePath = $this->pathMapper->getUrl($path); $fullPath = $this->fileBasePath . $relativePath . $this->getExtension($contentNode); if (!file_exists($fullPath)) { try { $this->saveData($contentNode, $fullPath); } catch (Imagine\Exception\Exception $e) { //TODO: notfound exception is not appropriate ... how to best do this? //throw new NotFoundHttpException('image save to filesystem failed: ' . $e->getMessage()); return 'notfound'; } } return $this->webRelativePath . $relativePath . $this->getExtension($contentNode); }
protected function getTranslationNode(NodeInterface $parentNode, $locale) { $name = Translation::LOCALE_NAMESPACE . ":{$locale}"; if (!$parentNode->hasNode($name)) { $node = $parentNode->addNode($name); } else { $node = $parentNode->getNode($name); } return $node; }
/** * Assert that $node has a folder child that is an nt:folder and has a * child * called file that is an nt:file. * * @param NodeInterface $node */ private function assertFolderAndFile(NodeInterface $node) { $this->assertTrue($node->hasNode('folder')); $folder = $node->getNode('folder'); $this->assertTrue($folder->hasProperty('jcr:created')); $this->assertInstanceOf('\\DateTime', $folder->getPropertyValue('jcr:created')); $this->assertTrue($folder->hasNode('file')); $file = $folder->getNode('file'); $this->assertTrue($file->hasNode('jcr:content')); $resource = $file->getNode('jcr:content'); $this->assertTrue($resource->hasProperty('jcr:lastModified')); $this->assertInstanceOf('\\DateTime', $resource->getPropertyValue('jcr:lastModified')); $this->assertTrue($resource->hasProperty('jcr:data')); $binaryStream = $file->getNode('jcr:content')->getProperty('jcr:data')->getBinary(); $this->assertNotNull($binaryStream, 'We got no content from the file'); $content = stream_get_contents($binaryStream); $this->assertEquals(self::FILE_CONTENT, $content); }
/** * check if path is unique from given $root node. * * @param NodeInterface $root route node * @param string $path requested path * * @return bool path is unique */ private function isUnique(NodeInterface $root, $path) { // check if root has node return !$root->hasNode(ltrim($path, '/')); }
/** * @param $name * @param NodeInterface $parent * * @return string */ private function getUniquePath($name, NodeInterface $parent) { if ($parent->hasNode($name)) { $i = 0; do { $i++; } while ($parent->hasNode($name . '-' . $i)); return $name . '-' . $i; } else { return $name; } }
/** * {@inheritdoc} */ public function hasNode($relPath) { return $this->node->hasNode($relPath); }
/** * Validate this node with the nodetype and generate not yet existing * autogenerated properties as necessary. * * @param NodeInterface $node * @param NodeType $nodeTypeDefinition * * @return AddNodeOperation[] Additional operations to handle autocreated nodes. * * @throws \InvalidArgumentException * @throws RepositoryException * @throws ItemExistsException * @throws LockException * @throws ConstraintViolationException * @throws PathNotFoundException * @throws VersionException * @throws ValueFormatException */ private function processNodeWithType(NodeInterface $node, NodeType $nodeTypeDefinition) { $additionalOperations = array(); foreach ($nodeTypeDefinition->getDeclaredChildNodeDefinitions() as $childDef) { /* @var $childDef NodeDefinitionInterface */ if (!$node->hasNode($childDef->getName())) { if ('*' === $childDef->getName()) { continue; } if ($childDef->isMandatory() && !$childDef->isAutoCreated()) { throw new RepositoryException(sprintf('Child "%s" is mandatory, but is not present while saving "%s" at path "%s"', $childDef->getName(), $nodeTypeDefinition->getName(), $node->getPath())); } if ($childDef->isAutoCreated()) { $requiredPrimaryTypeNames = $childDef->getRequiredPrimaryTypeNames(); $primaryType = count($requiredPrimaryTypeNames) ? current($requiredPrimaryTypeNames) : null; $newNode = $node->addNode($childDef->getName(), $primaryType); $absPath = $node->getPath() . '/' . $childDef->getName(); $operation = new AddNodeOperation($absPath, $newNode); $additionalOperations[] = $operation; } } } foreach ($nodeTypeDefinition->getDeclaredPropertyDefinitions() as $propertyDef) { /* @var $propertyDef PropertyDefinitionInterface */ if ('*' === $propertyDef->getName()) { continue; } if (!$node->hasProperty($propertyDef->getName())) { if ($propertyDef->isMandatory() && !$propertyDef->isAutoCreated()) { throw new RepositoryException(sprintf('Property "%s" is mandatory, but is not present while saving "%s" at "%s"', $propertyDef->getName(), $nodeTypeDefinition->getName(), $node->getPath())); } if ($propertyDef->isAutoCreated()) { switch ($propertyDef->getName()) { case 'jcr:uuid': $value = UUIDHelper::generateUUID(); break; case 'jcr:createdBy': case 'jcr:lastModifiedBy': $value = $this->userId; break; case 'jcr:created': case 'jcr:lastModified': $value = new \DateTime(); break; case 'jcr:etag': // TODO: http://www.day.com/specs/jcr/2.0/3_Repository_Model.html#3.7.12.1%20mix:etag $value = 'TODO: generate from binary properties of this node'; break; default: $defaultValues = $propertyDef->getDefaultValues(); if ($propertyDef->isMultiple()) { $value = $defaultValues; } elseif (isset($defaultValues[0])) { $value = $defaultValues[0]; } else { // When implementing versionable or activity, we need to handle more properties explicitly throw new RepositoryException(sprintf('No default value for autocreated property "%s" at "%s"', $propertyDef->getName(), $node->getPath())); } } $node->setProperty($propertyDef->getName(), $value, $propertyDef->getRequiredType()); } } elseif ($propertyDef->isAutoCreated()) { $prop = $node->getProperty($propertyDef->getName()); if (!$prop->isModified() && !$prop->isNew()) { switch ($propertyDef->getName()) { case 'jcr:lastModified': if ($this->autoLastModified) { $prop->setValue(new \DateTime()); } break; case 'jcr:lastModifiedBy': if ($this->autoLastModified) { $prop->setValue($this->userId); } break; case 'jcr:etag': // TODO: update etag if needed break; } } } } foreach ($nodeTypeDefinition->getDeclaredSupertypes() as $superType) { $this->processNodeWithType($node, $superType); } foreach ($node->getProperties() as $property) { $this->assertValidProperty($property); } return $additionalOperations; }
public function testHasNodeFalse() { $this->assertFalse($this->node->hasNode('foobar')); }