private function doSerializeResource(Resource $resource, $depth = 0) { $data = array(); $repositoryAlias = $this->registry->getRepositoryAlias($resource->getRepository()); $data['repository_alias'] = $repositoryAlias; $data['repository_type'] = $this->registry->getRepositoryType($resource->getRepository()); $data['payload_alias'] = $this->payloadAliasRegistry->getPayloadAlias($resource); $data['payload_type'] = null; if ($resource instanceof CmfResource) { $data['payload_type'] = $resource->getPayloadType(); } $data['path'] = $resource->getPath(); $data['label'] = $data['node_name'] = PathHelper::getNodeName($data['path']); $data['repository_path'] = $resource->getRepositoryPath(); $enhancers = $this->enhancerRegistry->getEnhancers($repositoryAlias); $children = array(); foreach ($resource->listChildren() as $name => $childResource) { $children[$name] = array(); if ($depth < 2) { $children[$name] = $this->doSerializeResource($childResource, $depth + 1); } } $data['children'] = $children; if ($resource instanceof BodyResource) { $data['body'] = $resource->getBody(); } foreach ($enhancers as $enhancer) { $data = $enhancer->enhance($data, $resource); } return $data; }
/** * Return the alias for the given PHPCR resource. * * @param resource $resource * * @return string */ public function getPayloadAlias(Resource $resource) { $repositoryType = $this->repositoryRegistry->getRepositoryType($resource->getRepository()); $type = null; if ($resource instanceof CmfResource) { $type = $resource->getPayloadType(); } if (null === $type) { return; } if (!isset($this->aliasesByRepository[$repositoryType])) { return; } if (!isset($this->aliasesByRepository[$repositoryType][$type])) { return; } return $this->aliasesByRepository[$repositoryType][$type]; }
/** * {@inheritdoc} */ public function enhance(array $data, Resource $resource) { $object = $resource->getPayload(); // sonata has dependency on ClassUtils so this is fine. $class = ClassUtils::getClass($object); if (false === $this->pool->hasAdminByClass($class)) { return $data; } $admin = $this->pool->getAdminByClass($class); $links = array(); $routeCollection = $admin->getRoutes(); foreach ($routeCollection->getElements() as $code => $route) { $routeName = $route->getDefault('_sonata_name'); $url = $this->urlGenerator->generate($routeName, array($admin->getIdParameter() => $admin->getUrlsafeIdentifier($object)), true); $routeRole = substr($code, strlen($admin->getCode()) + 1); $links[$routeRole] = $url; } $data['label'] = $admin->toString($object); $data['sonata_label'] = $admin->getLabel(); $data['sonata_links'] = $links; return $data; }
/** * Formats the name of the resource. * * Resources with children are colored. * * @param Resource $resource The resource. * * @return string The formatted name. */ private function formatName(Resource $resource) { $name = $resource->getName(); if ($resource->hasChildren()) { return '<c1>' . $name . '</c1>'; } return $name; }
/** * {@inheritdoc} */ public function enhance(array $data, Resource $resource) { $payload = $resource->getPayload(); $data['payload'] = $payload; return $data; }
/** * Returns the path where a resource is going to be installed. * * This is a path relative to the document root of the target server. * * @param Resource $resource The resource. * * @return string The server path. */ public function getServerPathForResource(Resource $resource) { $relPath = Path::makeRelative($resource->getRepositoryPath(), $this->basePath); return '/' . trim($this->mapping->getServerPath() . '/' . $relPath, '/'); }
/** * Recursively prints the tree for the given resource. * * @param IO $io The I/O. * @param Resource $resource The printed resource. * @param int $total Collects the total number of printed resources. * @param string $prefix The prefix for all printed resources. */ private function printTree(IO $io, Resource $resource, &$total, $prefix = '') { // The root node has an empty name $children = $resource->listChildren(); $lastIndex = count($children) - 1; $index = 0; foreach ($children as $child) { $isLastChild = $index === $lastIndex; $childPrefix = $isLastChild ? self::LAST_CHILD_PREFIX : self::CHILD_PREFIX; $nestingPrefix = $isLastChild ? self::NESTING_CLOSED_PREFIX : self::NESTING_OPEN_PREFIX; $name = $child->getName() ?: '/'; if ($child->hasChildren()) { $name = '<c1>' . $name . '</c1>'; } $io->writeLine($prefix . $childPrefix . $name); $this->printTree($io, $child, $total, $prefix . $nestingPrefix); ++$index; ++$total; } }
/** * Returns an iterator for the children of a resource. * * @param Resource $resource The resource. * * @return RegexFilterIterator|Resource[] The iterator. */ private function getChildIterator(Resource $resource) { $staticPrefix = rtrim($resource->getPath(), '/') . '/'; $regExp = '~^' . preg_quote($staticPrefix, '~') . '[^/]+$~'; return new RegexFilterIterator($regExp, $staticPrefix, new ArrayIterator($this->resources), RegexFilterIterator::FILTER_KEY); }
private function addResource($path, Resource $resource, $checkParentsForSymlinks = true) { $pathInBaseDir = $this->baseDir . $path; $hasChildren = $resource->hasChildren(); $hasBody = $resource instanceof BodyResource; if ($hasChildren && $hasBody) { throw new UnsupportedResourceException(sprintf('Instances of BodyResource do not support child resources in ' . 'FilesystemRepository. Tried to add a BodyResource with ' . 'children at %s.', $path)); } if ($this->symlink && $checkParentsForSymlinks) { $this->replaceParentSymlinksByCopies($path); } if ($resource instanceof FilesystemResource) { if ($this->symlink) { $this->symlinkMirror($resource->getFilesystemPath(), $pathInBaseDir); } elseif ($hasBody) { $this->filesystem->copy($resource->getFilesystemPath(), $pathInBaseDir); } else { $this->filesystem->mirror($resource->getFilesystemPath(), $pathInBaseDir); } return; } if ($hasBody) { file_put_contents($pathInBaseDir, $resource->getBody()); return; } if (is_file($pathInBaseDir)) { $this->filesystem->remove($pathInBaseDir); } if (!file_exists($pathInBaseDir)) { mkdir($pathInBaseDir, 0777, true); } foreach ($resource->listChildren() as $child) { $this->addResource($path . '/' . $child->getName(), $child, false); } }