Skip to content

Instantly share code, notes, and snippets.

@crisu83
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisu83/cd3f24d8b7340c9c1a51 to your computer and use it in GitHub Desktop.
Save crisu83/cd3f24d8b7340c9c1a51 to your computer and use it in GitHub Desktop.
WIP file manager module for Lumen framework
<?php namespace Nord\Lumen\FileManager;
use Jenssegers\Date\Date;
use Nord\Lumen\Doctrine\Traits\AutoIncrements;
class File
{
use AutoIncrements;
const DISK_LOCAL = 'local';
const DISK_S3 = 's3';
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $filename;
/**
* @var string
*/
private $mimeType;
/**
* @var int
*/
private $byteSize;
/**
* @var string
*/
private $disk;
/**
* @var Date
*/
private $savedAt;
/**
* @param string $id
* @param string $fileName
* @param string $mimeType
* @param int $byteSize
* @param string $disk
* @param array $data
*/
public function __construct($id, $fileName, $mimeType, $byteSize, $disk)
{
$this->setId($id);
$this->setFilename($fileName);
$this->setMimeType($mimeType);
$this->setByteSize($byteSize);
$this->setDisk($disk);
$this->setSavedAt(Date::now());
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* @return string
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* @return int
*/
public function getByteSize()
{
return $this->byteSize;
}
/**
* @return string
*/
public function getDisk()
{
return $this->disk;
}
/**
* @return Date
*/
public function getSavedAt()
{
return $this->savedAt;
}
/**
* @param string $id
*/
private function setId($id)
{
$this->id = $id;
}
/**
* @param string $filename
*
* @throws \InvalidArgumentException
*/
private function setFilename($filename)
{
if (empty($filename)) {
throw new \Exception('Filename cannot be empty.');
}
$this->filename = $filename;
}
/**
* @param string $mimeType
*/
private function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
}
/**
* @param int $byteSize
*/
private function setByteSize($byteSize)
{
$this->byteSize = $byteSize;
}
/**
* @param string $storage
*/
private function setDisk($storage)
{
$this->disk = $storage;
}
/**
* @param Date $savedAt
*/
private function setSavedAt(Date $savedAt)
{
$this->savedAt = $savedAt;
}
}
<?php namespace Nord\Lumen\FileManager\Contracts;
interface FileIdGenerator
{
/**
* @return mixed
*/
public function generate();
}
<?php namespace Nord\Lumen\FileManager;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Nord\Lumen\FileManager\Contracts\FileIdGenerator;
use Nord\Lumen\FileManager\Contracts\FilesystemAdapter;
use Nord\Lumen\FileManager\Contracts\FileManager as FileManagerContract;
use Nord\Lumen\FileManager\Contracts\FileStorage;
use Symfony\Component\HttpFoundation\File\File as FileInfo;
class FileManager implements FileManagerContract
{
/**
* @var FilesystemFactory
*/
private $factory;
/**
* @var FileStorage
*/
private $storage;
/**
* @var FileIdGenerator
*/
private $idGenerator;
/**
* @var FilesystemAdapter[]
*/
private $adapters;
/**
* FileManager constructor.
*
* @param FilesystemFactory $factory
* @param FileStorage $storage
* @param FileIdGenerator $idGenerator
* @param FilesystemAdapter[] $adapters
*/
public function __construct(
FilesystemFactory $factory,
FileStorage $storage,
FileIdGenerator $idGenerator,
array $adapters
) {
$this->setFactory($factory);
$this->setStorage($storage);
$this->setIdGenerator($idGenerator);
$this->setAdapters($adapters);
}
/**
* @inheritdoc
*/
public function saveFile(FileInfo $info, $path, $disk = File::DISK_LOCAL, $visibility = null)
{
$file = new File($this->generateId(), $path, $info->getMimeType(), $info->getSize(), $disk);
$this->storage->saveFile($file);
$this->getAdapter($file->getDisk())->saveFile($path, file_get_contents($info->getRealPath()), $visibility);
return $file;
}
/**
* @inheritdoc
*/
public function getFile($id)
{
return $this->storage->getFile($id);
}
/**
* @param mixed $id
*
* @return string
* @throws \Exception
*/
public function getFileUrl($id)
{
$file = $this->getFile($id);
if ($file === null) {
throw new \Exception("File '$id' not found.");
}
return $this->getAdapter($file->getDisk())->getFileUrl($file);
}
/**
* @inheritdoc
*/
public function deleteFile($id)
{
return $this->storage->deleteFile($id);
}
/**
* @param FilesystemAdapter $adapter
*
* @throws \Exception
*/
public function addAdapter(FilesystemAdapter $adapter)
{
$name = $adapter->getName();
$adapter->setFilesystem($this->factory->disk($name));
$this->adapters[$name] = $adapter;
}
/**
* @param string $name
*
* @return FilesystemAdapter
*/
protected function getAdapter($name)
{
if (!isset($this->adapters[$name])) {
throw new \Exception("Adapter for filesystem '$name' not found.");
}
return $this->adapters[$name];
}
/**
* @return mixed
*/
protected function generateId()
{
while (!isset($id) || $this->storage->idExists($id)) {
$id = $this->idGenerator->generate();
}
return $id;
}
/**
* @param FilesystemFactory $factory
*/
private function setFactory($factory)
{
$this->factory = $factory;
}
/**
* @param FileStorage $storage
*/
private function setStorage($storage)
{
$this->storage = $storage;
}
/**
* @param FileIdGenerator $idGenerator
*/
private function setIdGenerator($idGenerator)
{
$this->idGenerator = $idGenerator;
}
/**
* @param FilesystemAdapter[] $adapters
*/
private function setAdapters($adapters)
{
foreach ($adapters as $adapter) {
$this->addAdapter($adapter);
}
}
}
<?php namespace Nord\Lumen\FileManager\Contracts;
use Nord\Lumen\FileManager\File;
interface FileStorage
{
/**
* @param File $file
*
* @return bool
*/
public function saveFile(File $file);
/**
* @param string $id
*
* @return File
*/
public function getFile($id);
/**
* @param string $id
*
* @return bool
*/
public function deleteFile($id);
/**
* @param string $id
*
* @return bool
*/
public function idExists($id);
}
<?php namespace Nord\Lumen\FileManager\Contracts;
use Nord\Lumen\FileManager\File;
use Illuminate\Contracts\Filesystem\Filesystem;
interface FilesystemAdapter
{
/**
* @return string
*/
public function getName();
/**
* @param string $path
* @param string $contents
* @param null|bool $visibility
*
* @return bool
*/
public function saveFile($path, $contents, $visibility);
/**
* @param File $file
*
* @return string
*/
public function getFilePath(File $file);
/**
* @param string $directory
* @param File $file
*
* @return string
*/
public function getFileUrl(File $file);
/**
* @param Filesystem $filesystem
*/
public function setFilesystem($filesystem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment