getRootDirectory($abstraction->getFileSystemId()); return 'file://' . urlencode($abstraction->getFileSystemId()) . '/' . urlencode( $baseDir->getRelPath($abstraction) ); } elseif ($abstraction instanceof Directory) { $baseDir = $this->getRootDirectory($abstraction->getFileSystemId()); return 'dir://' . urlencode($abstraction->getFileSystemId()) . '/' . urlencode( $baseDir->getRelPath($abstraction) ); } else { throw new FileSerializerException( __CLASS__ . '::' . __FUNCTION__ . ' expects parameter to be an instance of Directory or File' ); } } /** * {@inheritDoc} * @see FileReferenceSerializer::unserialize() */ public function unserialize($serial) { $serial = $this->cleanSerial($serial); $type = substr($serial, 0, strpos($serial, ':')); if ($type == 'file') { return $this->unserializeFile($serial); } elseif ($type == 'dir') { return $this->unserializeDirectory($serial); } else { throw new FileSerializerException('Unsupported type "' . $type . '" in ' . __CLASS__); } } /** * {@inheritDoc} * @see FileReferenceSerializer::unserializeFile() */ public function unserializeFile($serial) { $parts = $this->extract($serial); return $this->getRootDirectory(urldecode($parts['fs']))->getFile(urldecode($parts['path'])); } /** * {@inheritDoc} * @see FileReferenceSerializer::unserializeDirectory() */ public function unserializeDirectory($serial) { $parts = $this->extract($serial); return $this->getRootDirectory(urldecode($parts['fs']))->getDirectory(urldecode($parts['path'])); } /** * Ensure serial is a string * * @param string $serial * @throws FileSerializerException * @return string */ protected function cleanSerial($serial) { if ($serial instanceof \core_kernel_classes_Resource) { $serial = $serial->getUri(); } elseif ($serial instanceof \core_kernel_classes_Literal) { $serial = $serial->__toString(); } elseif (!is_string($serial)) { throw new FileSerializerException('Unsupported serial "' . gettype($serial) . '" in ' . __CLASS__); } return $serial; } /** * Extract filesystem id and path from serial * * @param string $serial * @throws FileSerializerException * @return string[] */ protected function extract($serial) { $serial = $this->cleanSerial($serial); $parts = explode('/', substr($serial, strpos($serial, '://') + 3), 2); if (count($parts) != 2) { throw new FileSerializerException('Unsupported dir in ' . __CLASS__); } return ['fs' => $parts[0], 'path' => $parts[1]]; } /** * Return root directory represented by the given uri * * @return Directory */ protected function getRootDirectory($id) { return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID)->getDirectory($id); } /** * {@inheritDoc} * @see FileReferenceSerializer::cleanUp() */ public function cleanUp($serial) { // nothing to do return true; } }