Implement LocalFileStorage::common()

refs #3016
This commit is contained in:
Alexander A. Klimov 2017-11-17 13:36:49 +01:00
parent 9adb516515
commit 2aed948896
2 changed files with 22 additions and 6 deletions

View File

@ -205,9 +205,9 @@ class ConfigController extends Controller
$this->createApplicationTabs()->activate('tls');
$rootCaCollections = array();
foreach (new LocalFileStorage(Icinga::app()->getStorageDir('framework/tls/rootcacollections')) as $ca) {
foreach (LocalFileStorage::common('tls/rootcacollections') as $rootCaCollection) {
$matches = array();
if (preg_match('~\A([0-9a-f]{2}+)\.pem\z~i', $ca, $matches)) {
if (preg_match('~\A([0-9a-f]{2}+)\.pem\z~i', $rootCaCollection, $matches)) {
$rootCaCollections[hex2bin($matches[1])] = null;
}
}
@ -216,9 +216,9 @@ class ConfigController extends Controller
$this->view->rootCaCollections = array_keys($rootCaCollections);
$clientIdentities = array();
foreach (new LocalFileStorage(Icinga::app()->getStorageDir('framework/tls/clientidentities')) as $client) {
foreach (LocalFileStorage::common('tls/clientidentities') as $clientIdentity) {
$matches = array();
if (preg_match('~\A([0-9a-f]{2}+)\.pem\z~i', $client, $matches)) {
if (preg_match('~\A([0-9a-f]{2}+)\.pem\z~i', $clientIdentity, $matches)) {
$clientIdentities[hex2bin($matches[1])] = null;
}
}

View File

@ -4,6 +4,7 @@
namespace Icinga\File\Storage;
use ErrorException;
use Icinga\Application\Icinga;
use Icinga\Exception\AlreadyExistsException;
use Icinga\Exception\NotFoundError;
use Icinga\Exception\NotReadableError;
@ -25,6 +26,21 @@ class LocalFileStorage implements StorageInterface
*/
protected $baseDir;
/**
* Factory for the common storage directory with optional subdirectory
*
* @param string $subDir
*
* @return static
*/
public static function common($subDir = null)
{
$baseDir = Icinga::app()->getStorageDir($subDir);
static::ensureDir($baseDir);
return new static($baseDir);
}
/**
* Constructor
*
@ -135,10 +151,10 @@ class LocalFileStorage implements StorageInterface
*
* @throws NotWritableError
*/
protected function ensureDir($dir)
protected static function ensureDir($dir)
{
if (! is_dir($dir)) {
$this->ensureDir(dirname($dir));
static::ensureDir(dirname($dir));
try {
mkdir($dir, 02770);