Last active
December 29, 2015 05:09
-
-
Save WyriHaximus/7619947 to your computer and use it in GitHub Desktop.
Add any metadata and serialize it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$filesystem = new Filesystem(new Local(TMP . DS . 'flytest' . DS), new Predis()); | |
$filesystem->addPlugin(new MetadataSerializer('yourawesomemetadatatag')); | |
$filesystem->getYourawesomemetadatatag('filename.txt', ['foo' => 'bar'], true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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