/** * @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; }
protected function findAllResourceOptions(ResourceDOInterface $resourceDO) { /** @var \League\Flysystem\FilesystemInterface $filesystem */ $filesystem = $this->filesystem; $uuid = $resourceDO->getUuid(); $type = $resourceDO->getType(); $name = $resourceDO->getName(); if (!$name || !$type) { throw new CommandErrorException('Can not look for options: resource is empty'); } $basename = $resourceDO->getBaseDirectory(); $namespace = $resourceDO->getNamespace(); $path = $basename . ($namespace ? $namespace . DIRECTORY_SEPARATOR : '') . $type . DIRECTORY_SEPARATOR; $found = $filesystem->listContents($path, true); $found = array_filter($found, function ($file) use($uuid, $type) { return array_key_exists('filename', $file) && $file['filename'] === $uuid && array_key_exists('extension', $file) && $file['extension'] === $type && array_key_exists('type', $file) && $file['type'] === 'file'; }); array_walk($found, [$this, 'hydrateElementFile'], ['resourceDO' => $resourceDO]); $found = array_values($found); // reset keys return $found; }