New logic for storage provider (breaking changes)

This commit is contained in:
RainLoop Team 2015-02-05 03:54:26 +04:00
parent 44566aad4b
commit 51666d7c7e
19 changed files with 453 additions and 297 deletions

View file

@ -71,7 +71,7 @@ class DefaultStorage implements \RainLoop\Providers\Files\FilesInterface
{
$mResult = @\fopen($sFileName, $sOpenMode);
}
return $mResult;
}
@ -107,7 +107,7 @@ class DefaultStorage implements \RainLoop\Providers\Files\FilesInterface
{
$mResult = @\unlink($sFileName);
}
return $mResult;
}
@ -125,7 +125,7 @@ class DefaultStorage implements \RainLoop\Providers\Files\FilesInterface
{
$mResult = \filesize($sFileName);
}
return $mResult;
}
@ -165,13 +165,28 @@ class DefaultStorage implements \RainLoop\Providers\Files\FilesInterface
*/
private function generateFileName($oAccount, $sKey, $bMkDir = false)
{
$sEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_',
('' === $oAccount->ParentEmail() ? '' : $oAccount->ParentEmail().'/').$oAccount->Email());
$sEmail = $sSubEmail = '';
if ($oAccount instanceof \RainLoop\Model\Account)
{
$sEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $oAccount->ParentEmailHelper());
if ($oAccount->IsAdditionalAccount())
{
$sSubEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $oAccount->Email());
}
}
if (empty($sEmail))
{
$sEmail = '__unknown__';
}
$sKeyPath = \sha1($sKey);
$sKeyPath = \substr($sKeyPath, 0, 2).'/'.\substr($sKeyPath, 2, 2).'/'.$sKeyPath;
$sFilePath = $this->sDataPath.'/'.rtrim(substr($sEmail, 0, 2), '@').'/'.$sEmail.'/'.$sKeyPath;
$sFilePath = $this->sDataPath.'/'.
\str_pad(\rtrim(\substr($sEmail, 0, 2), '@'), 2, '_').'/'.$sEmail.'/'.
(0 < \strlen($sSubEmail) ? $sSubEmail.'/' : '').
$sKeyPath;
if ($bMkDir && !empty($sFilePath) && !@\is_dir(\dirname($sFilePath)))
{

View file

@ -42,16 +42,6 @@ class Settings extends \RainLoop\Providers\AbstractProvider
return $this->oDriver->Save($oAccount, $oSettings->DataAsArray());
}
/**
* @param string $sEmail
*
* @return bool
*/
public function ClearByEmail($sEmail)
{
return $this->oDriver->ClearByEmail($sEmail);
}
/**
* @return bool
*/

View file

@ -5,6 +5,7 @@ namespace RainLoop\Providers\Settings;
class DefaultSettings implements \RainLoop\Providers\Settings\SettingsInterface
{
const FILE_NAME = 'settings';
const FILE_NAME_LOCAL = 'settings_local';
/**
* @var \RainLoop\Providers\Storage
@ -28,7 +29,10 @@ class DefaultSettings implements \RainLoop\Providers\Settings\SettingsInterface
{
$sValue = $this->oStorageProvider->Get($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG,
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME);
$this->oStorageProvider->IsLocal() ?
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME_LOCAL :
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME
);
$aSettings = array();
if (\is_string($sValue))
@ -53,19 +57,23 @@ class DefaultSettings implements \RainLoop\Providers\Settings\SettingsInterface
{
return $this->oStorageProvider->Put($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG,
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME,
$this->oStorageProvider->IsLocal() ?
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME_LOCAL :
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME,
\json_encode($aSettings));
}
/**
* @param string $sEmail
* @param \RainLoop\Model\Account $oAccount
*
* @return bool
*/
public function ClearByEmail($sEmail)
public function Delete($oAccount)
{
return $this->oStorageProvider->Clear($sEmail,
return $this->oStorageProvider->Clear($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG,
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME);
$this->oStorageProvider->IsLocal() ?
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME_LOCAL :
\RainLoop\Providers\Settings\DefaultSettings::FILE_NAME);
}
}

View file

@ -20,9 +20,9 @@ interface SettingsInterface
public function Save($oAccount, array $aSettings);
/**
* @param string $sEmail
* @param \RainLoop\Model\Account $oAccount
*
* @return bool
*/
public function ClearByEmail($sEmail);
public function Delete($oAccount);
}

View file

@ -18,15 +18,15 @@ class Storage extends \RainLoop\Providers\AbstractProvider
}
/**
* @param \RainLoop\Model\Account|string|null $oAccount
* @param \RainLoop\Model\Account|string|null $mAccount
* @param int $iStorageType
*
* @return bool
*/
public function verifyAccount($oAccount, $iStorageType)
private function verifyAccount($mAccount, $iStorageType)
{
if (\RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY !== $iStorageType &&
!($oAccount instanceof \RainLoop\Model\Account || \is_string($oAccount)))
!($mAccount instanceof \RainLoop\Model\Account || \is_string($mAccount)))
{
return false;
}
@ -83,10 +83,20 @@ class Storage extends \RainLoop\Providers\AbstractProvider
{
return false;
}
return $this->oDriver->Clear($oAccount, $iStorageType, $sKey);
}
/**
* @param \RainLoop\Model\Account|string $oAccount
*
* @return bool
*/
public function DeleteStorage($oAccount)
{
return $this->oDriver->DeleteStorage($oAccount);
}
/**
* @return bool
*/
@ -94,4 +104,13 @@ class Storage extends \RainLoop\Providers\AbstractProvider
{
return $this->oDriver instanceof \RainLoop\Providers\Storage\StorageInterface;
}
/**
* @return bool
*/
public function IsLocal()
{
return $this->oDriver instanceof \RainLoop\Providers\Storage\StorageInterface &&
$this->oDriver->IsLocal();
}
}

View file

@ -9,14 +9,21 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
*/
private $sDataPath;
/**
* @var bool
*/
private $bLocal;
/**
* @param string $sStoragePath
* @param bool $bLocal = false
*
* @return void
*/
public function __construct($sStoragePath)
public function __construct($sStoragePath, $bLocal = false)
{
$this->sDataPath = \rtrim(\trim($sStoragePath), '\\/');
$this->bLocal = !!$bLocal;
}
/**
@ -49,7 +56,7 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
{
$mValue = \file_get_contents($sFileName);
}
return false === $mValue ? $mDefault : $mValue;
}
@ -68,33 +75,78 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
{
$mResult = @\unlink($sFileName);
}
return $mResult;
}
/**
* @param \RainLoop\Model\Account|string $oAccount
*
* @return bool
*/
public function DeleteStorage($oAccount)
{
$sPath = $this->generateFileName($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::USER, 'xxx', false, true);
if (!empty($sPath) && \is_dir($sPath))
{
\MailSo\Base\Utils::RecRmDir($sPath);
}
$sPath = $this->generateFileName($oAccount,
\RainLoop\Providers\Storage\Enumerations\StorageType::CONFIG, 'xxx', false, true);
if (!empty($sPath) && \is_dir($sPath))
{
\MailSo\Base\Utils::RecRmDir($sPath);
}
return true;
}
/**
* @return bool
*/
public function IsLocal()
{
return $this->bLocal;
}
/**
* @param \RainLoop\Model\Account|string|null $mAccount
* @param int $iStorageType
* @param string $sKey
* @param bool $bMkDir = false
* @param bool $bForDeleteAction = false
*
* @return string
*/
private function generateFileName($mAccount, $iStorageType, $sKey, $bMkDir = false)
private function generateFileName($mAccount, $iStorageType, $sKey, $bMkDir = false, $bForDeleteAction = false)
{
if (null === $mAccount)
{
$iStorageType = \RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY;
}
$sEmail = $mAccount instanceof \RainLoop\Model\Account ? \preg_replace('/[^a-z0-9\-\.@]+/', '_',
('' === $mAccount->ParentEmail() ? '' : $mAccount->ParentEmail().'/').$mAccount->Email()) : '';
$sEmail = $sSubEmail = '';
if ($mAccount instanceof \RainLoop\Model\Account)
{
$sEmail = $mAccount->ParentEmailHelper();
if ($this->bLocal && $mAccount->IsAdditionalAccount() && !$bForDeleteAction)
{
$sSubEmail = $mAccount->Email();
}
}
if (\is_string($mAccount) && empty($sEmail))
{
$sEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $mAccount);
$sEmail = $mAccount;
}
$sEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $sEmail);
$sSubEmail = \preg_replace('/[^a-z0-9\-\.@]+/', '_', $sSubEmail);
$sTypePath = $sKeyPath = '';
switch ($iStorageType)
{
@ -118,10 +170,13 @@ class DefaultStorage implements \RainLoop\Providers\Storage\StorageInterface
}
else if (!empty($sEmail))
{
$sFilePath = $this->sDataPath.'/'.$sTypePath.'/'.rtrim(substr($sEmail, 0, 2), '@').'/'.$sEmail.'/'.$sKeyPath;
$sFilePath = $this->sDataPath.'/'.$sTypePath.'/'.
\str_pad(\rtrim(\substr($sEmail, 0, 2), '@'), 2, '_').'/'.$sEmail.'/'.
(0 < \strlen($sSubEmail) ? $sSubEmail.'/' : '').
($bForDeleteAction ? '' : $sKeyPath);
}
if ($bMkDir && !empty($sFilePath) && !@\is_dir(\dirname($sFilePath)))
if ($bMkDir && !$bForDeleteAction && !empty($sFilePath) && !@\is_dir(\dirname($sFilePath)))
{
if (!@\mkdir(\dirname($sFilePath), 0755, true))
{

View file

@ -32,4 +32,9 @@ interface StorageInterface
* @return bool
*/
public function Clear($oAccount, $iStorageType, $sKey);
/**
* @return bool
*/
public function IsLocal();
}