Skip to content

Instantly share code, notes, and snippets.

@WyriHaximus
Last active December 29, 2015 05:09
Show Gist options
  • Save WyriHaximus/7619947 to your computer and use it in GitHub Desktop.
Save WyriHaximus/7619947 to your computer and use it in GitHub Desktop.
Add any metadata and serialize it.
<?php
$filesystem = new Filesystem(new Local(TMP . DS . 'flytest' . DS), new Predis());
$filesystem->addPlugin(new MetadataSerializer('yourawesomemetadatatag'));
$filesystem->getYourawesomemetadatatag('filename.txt', ['foo' => 'bar'], true);
<?php
use Flysystem\FilesystemInterface;
use Flysystem\PluginInterface;
class MetadataSerializer implements PluginInterface {
protected $filesystem;
protected $fieldName;
public function __construct($fieldName) {
$this->fieldName = $fieldName;
}
public function setFilesystem(FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
}
public function getMethod()
{
return 'get' . ucfirst($this->fieldName);
}
public function handle($path = null, $fieldContents = null, $autosave = false)
{
$this->assertPresent($path);
if ($fieldContents !== null) {
$data = $this->filesystem->getCache()->updateObject($path, [
$this->fieldName => serialize($fieldContents),
], $autosave);
return $data[$this->fieldName];
}
if ($metadata = $this->filesystem->getCache()->getMetadata($path)) {
if (isset($metadata[$this->fieldName])) {
return unserialize($metadata[$this->fieldName]);
}
}
return false;
}
protected function assertPresent($path)
{
if ( ! $this->filesystem->has($path)) {
throw new FileNotFoundException($path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment