1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-12 03:34:04 +02:00

[ticket/15253] Add storage abstraction

PHPBB3-15253
This commit is contained in:
Rubén Calvo
2017-06-25 14:22:54 +02:00
parent 2482ec6897
commit 334b44057d
13 changed files with 1078 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\storage\adapter;
interface adapter_interface
{
/**
* Dumps content into a file.
*
* @param string path The file to be written to.
* @param string content The data to write into the file.
*
* @throws \phpbb\storage\exception\exception When the file already exists
* When the file cannot be written
*/
public function put_contents($path, $content);
/**
* Read the contents of a file
*
* @param string $path The file to read
*
* @throws \phpbb\storage\exception\exception When the file dont exists
* When cannot read file contents
*/
public function get_contents($path);
/**
* Checks the existence of files or directories.
*
* @param string $path file/directory to check
*
* @return bool Returns true if all files/directories exist, false otherwise
*/
public function exists($path);
/**
* Removes files or directories.
*
* @param string $path file/directory to remove
*
* @throws \phpbb\storage\exception\exception When removal fails.
*/
public function delete($path);
/**
* Rename a file or a directory.
*
* @param string $path_orig The original file/direcotry
* @param string $path_dest The target file/directory
*
* @throws \phpbb\storage\exception\exception When target exists
* When file/directory cannot be renamed
*/
public function rename($path_orig, $path_dest);
/**
* Copies a file.
*
* @param string $path_orig The original filename
* @param string $path_dest The target filename
*
* @throws \phpbb\storage\exception\exception When target exists
* When the file cannot be copied
*/
public function copy($path_orig, $path_dest);
}

View File

@@ -0,0 +1,146 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\storage\adapter;
use phpbb\storage\exception\exception;
use phpbb\filesystem\filesystem_exception;
class local implements adapter_interface
{
/**
* Filesystem component
*
* @var \phpbb\filesystem\filesystem
*/
protected $filesystem;
/** @var string phpBB root path */
protected $phpbb_root_path;
/**
* Constructor
*/
public function __construct($filesystem, $phpbb_root_path)
{
$this->filesystem = $filesystem;
$this->phpbb_root_path = $phpbb_root_path;
}
/**
* {@inheritdoc}
*/
public function put_contents($path, $content)
{
if ($this->exists($this->phpbb_root_path.$path))
{
throw new exception('', $path); // FILE_EXISTS
}
try
{
$this->filesystem->dump_file($this->phpbb_root_path.$path, $content);
}
catch (filesystem_exception $e)
{
throw new exception('', $path, array(), $e); // CANNOT_DUMP_FILE
}
}
/**
* {@inheritdoc}
*/
public function get_contents($path)
{
if (!$this->exists($this->phpbb_root_path.$path))
{
throw new exception('', $path); // FILE_DONT_EXIST
}
if (($content = @file_get_contents($this->phpbb_root_path.$path)) === false)
{
throw new exception('', $path); // CANNOT READ FILE
}
return $content;
}
/**
* {@inheritdoc}
*/
public function exists($path)
{
return $this->filesystem->exists($this->phpbb_root_path.$path);
}
/**
* {@inheritdoc}
*/
public function delete($path)
{
try
{
$this->filesystem->remove($this->phpbb_root_path.$path);
}
catch (filesystem_exception $e)
{
throw new exception('', $path, array(), $e); // CANNOT DELETE
}
}
/**
* {@inheritdoc}
*/
public function rename($path_orig, $path_dest)
{
try
{
$this->filesystem->rename($this->phpbb_root_path.$path_orig, $this->phpbb_root_path.$path_dest, false);
}
catch (filesystem_exception $e)
{
throw new exception('', $path_orig, array(), $e); // CANNOT_RENAME
}
}
/**
* {@inheritdoc}
*/
public function copy($path_orig, $path_dest)
{
try
{
$this->filesystem->copy($this->phpbb_root_path.$path_orig, $this->phpbb_root_path.$path_dest, false);
}
catch (filesystem_exception $e)
{
throw new exception('', '', array(), $e); // CANNOT_COPY_FILES
}
}
/**
* {@inheritdoc}
*/
public function create_dir($path)
{
try
{
$this->filesystem->mkdir($this->phpbb_root_path.$path);
}
catch (filesystem_exception $e)
{
throw new exception('', $path, array(), $e); // CANNOT_CREATE_DIRECTORY
}
}
}