public function __invoke() { $version = $this->resourceDO->getVersion(); $filePath = $this->resourceDO->getFilePath(); if (!$this->resourceDO->getName() || !$this->resourceDO->getType() || !$this->resourceDO->getBaseDirectory()) { throw new CommandErrorException('Cannot delete empty resource'); } if ($this->filesystem->has($filePath)) { // Make backup of the default version if (ResourceDOInterface::DEFAULT_VERSION === $version) { $lastVersion = $this->findLastVersion(); // But only if previous existing version is not the default and not has the same content as deleting if (ResourceDOInterface::DEFAULT_VERSION !== $lastVersion) { $lastVersionResourceDO = clone $this->resourceDO; $lastVersionResourceDO->setVersion($lastVersion); $command = new DestroyEqualResourceCommand($lastVersionResourceDO, $this->resourceDO, $this->filesystem); $result = $command(); if ($result === $this->resourceDO) { // If the previous file version already the same, current version is already deleted // and backup and yet another deletion is not needed anymore return $this->resourceDO; } } $command = new BackupResourceCommand($this->resourceDO, $this->filesystem); $command($lastVersion); } $this->deleteFile($filePath); return $this->resourceDO; } return $this->resourceDO; }
protected function getUri(ResourceDOInterface $resourceDO) { $uri = $resourceDO->getName() . '.' . $resourceDO->getType(); $query = []; if (ResourceDOInterface::DEFAULT_VARIANT !== $resourceDO->getVariant()) { $query['var'] = $resourceDO->getVariant(); } if (ResourceDOInterface::DEFAULT_NAME_ALTERNATIVE !== $resourceDO->getNameAlternative()) { $query['alt'] = $resourceDO->getNameAlternative(); } if (ResourceDOInterface::DEFAULT_VERSION !== $resourceDO->getVersion()) { $query['v'] = $resourceDO->getVersion(); } $query = http_build_query($query, null, '&', PHP_QUERY_RFC3986); // RFC for correct spaces if ($query) { $uri .= '?' . $query; } return $uri; }
/** * @param bool $byPathOnly If true, no search on disk will be executed * @return ResourceDOInterface */ public function __invoke($byPathOnly = false) { $uuid = $this->resourceDO->getUuid(); $type = $this->resourceDO->getType(); $variant = $this->resourceDO->getVariant(); $version = $this->resourceDO->getVersion(); $baseDir = $this->resourceDO->getBaseDirectory(); $namespace = $this->resourceDO->getNamespace(); $filePath = $this->resourceDO->getFilePath(); if (!$uuid || !$type || !$baseDir || !$filePath) { throw new CommandErrorException('Cannot destroy the empty resource'); } if ($byPathOnly) { $this->deleteFile($filePath); } else { $command = new FindResourceOptionsCommand($this->resourceDO, $this->filesystem); $result = $command(); foreach ($result as $item) { if ($item[ResourceDOAbstract::TOKEN_TYPE] !== $type || $item['filename'] !== $uuid || $namespace && $item[ResourceDOAbstract::TOKEN_NAMESPACE] !== $namespace) { continue; } if ($version !== ResourceDOInterface::DEFAULT_VERSION) { if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT] && $version === (int) $item[ResourceDOAbstract::TOKEN_VERSION]) { $this->deleteFile($item['path']); } } elseif ($variant !== ResourceDOInterface::DEFAULT_VARIANT) { if ($variant === $item[ResourceDOAbstract::TOKEN_VARIANT]) { $this->deleteFile($item['path']); } } else { $this->deleteFile($item['path']); } } } return $this->resourceDO; }