Skip to content

Commit

Permalink
kw_auth - paths via array string
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-kalanis committed May 14, 2023
1 parent 90353c3 commit f03d234
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 362 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"type": "library",
"require": {
"alex-kalanis/kw_locks": ">=2.0",
"alex-kalanis/kw_paths": ">=3.0",
"php": ">=7.2.0"
},
"require-dev": {
Expand All @@ -22,7 +23,6 @@
"alex-kalanis/kw_confs": ">=1.1",
"alex-kalanis/kw_mapper": ">=2.0",
"alex-kalanis/kw_storage": ">=3.3",
"alex-kalanis/kw_paths": ">=3.0",
"alex-kalanis/kw_files": ">=3.2",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.0",
Expand Down
8 changes: 4 additions & 4 deletions php-src/Sources/Files/AFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ abstract class AFile implements Interfaces\IAuth, Interfaces\IAccessAccounts
protected $mode = null;
/** @var Interfaces\IStatus */
protected $status = null;
/** @var string */
protected $path = '';
/** @var string[] */
protected $path = [];

/**
* @param Interfaces\IMode $mode hashing mode
* @param Interfaces\IStatus $status which status is necessary to use that feature
* @param ILock $lock file lock
* @param string $path use full path with file name
* @param string[] $path use full path with file name
* @param Interfaces\IKauTranslations|null $lang
*/
public function __construct(Interfaces\IMode $mode, Interfaces\IStatus $status, ILock $lock, string $path, ?Interfaces\IKauTranslations $lang = null)
public function __construct(Interfaces\IMode $mode, Interfaces\IStatus $status, ILock $lock, array $path, ?Interfaces\IKauTranslations $lang = null)
{
$this->setAuLang($lang);
$this->mode = $mode;
Expand Down
27 changes: 17 additions & 10 deletions php-src/Sources/Files/AFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ abstract class AFiles implements Interfaces\IAuthCert, Interfaces\IAccessGroups,
protected $mode = null;
/** @var Interfaces\IStatus */
protected $status = null;
/** @var string */
protected $path = '';
/** @var string[] */
protected $path = [];

public function __construct(Interfaces\IMode $mode, Interfaces\IStatus $status, ILock $lock, string $dir, ?Interfaces\IKauTranslations $lang = null)
/**
* @param Interfaces\IMode $mode
* @param Interfaces\IStatus $status
* @param ILock $lock
* @param string[] $path
* @param Interfaces\IKauTranslations|null $lang
*/
public function __construct(Interfaces\IMode $mode, Interfaces\IStatus $status, ILock $lock, array $path, ?Interfaces\IKauTranslations $lang = null)
{
$this->setAuLang($lang);
$this->initAuthLock($lock);
$this->path = $dir;
$this->path = $path;
$this->mode = $mode;
$this->status = $status;
}
Expand Down Expand Up @@ -441,7 +448,7 @@ protected function checkRest(int $groupId): void
*/
protected function openPassword(): array
{
return $this->openFile($this->path . DIRECTORY_SEPARATOR . Interfaces\IFile::PASS_FILE);
return $this->openFile(array_merge($this->path, [Interfaces\IFile::PASS_FILE]));
}

/**
Expand All @@ -450,7 +457,7 @@ protected function openPassword(): array
*/
protected function savePassword(array $lines): void
{
$this->saveFile($this->path . DIRECTORY_SEPARATOR . Interfaces\IFile::PASS_FILE, $lines);
$this->saveFile(array_merge($this->path, [Interfaces\IFile::PASS_FILE]), $lines);
}

/**
Expand All @@ -459,7 +466,7 @@ protected function savePassword(array $lines): void
*/
protected function openShadow(): array
{
return $this->openFile($this->path . DIRECTORY_SEPARATOR . Interfaces\IFile::SHADE_FILE);
return $this->openFile(array_merge($this->path, [Interfaces\IFile::SHADE_FILE]));
}

/**
Expand All @@ -468,7 +475,7 @@ protected function openShadow(): array
*/
protected function saveShadow(array $lines): void
{
$this->saveFile($this->path . DIRECTORY_SEPARATOR . Interfaces\IFile::SHADE_FILE, $lines);
$this->saveFile(array_merge($this->path, [Interfaces\IFile::SHADE_FILE]), $lines);
}

/**
Expand All @@ -477,7 +484,7 @@ protected function saveShadow(array $lines): void
*/
protected function openGroups(): array
{
return $this->openFile($this->path . DIRECTORY_SEPARATOR . Interfaces\IFile::GROUP_FILE);
return $this->openFile(array_merge($this->path, [Interfaces\IFile::GROUP_FILE]));
}

/**
Expand All @@ -486,6 +493,6 @@ protected function openGroups(): array
*/
protected function saveGroups(array $lines): void
{
$this->saveFile($this->path . DIRECTORY_SEPARATOR . Interfaces\IFile::GROUP_FILE, $lines);
$this->saveFile(array_merge($this->path, [Interfaces\IFile::GROUP_FILE]), $lines);
}
}
8 changes: 4 additions & 4 deletions php-src/Sources/Files/AGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ abstract class AGroups implements IAccessGroups
use TLines;
use TStore;

/** @var string */
protected $path = '';
/** @var string[] */
protected $path = [];

/**
* @param ILock $lock
* @param string $path full path to group file
* @param string[] $path full path to group file
* @param IKauTranslations|null $lang
*/
public function __construct(ILock $lock, string $path, ?IKauTranslations $lang = null)
public function __construct(ILock $lock, array $path, ?IKauTranslations $lang = null)
{
$this->setAuLang($lang);
$this->initAuthLock($lock);
Expand Down
4 changes: 2 additions & 2 deletions php-src/Sources/Files/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class File extends AFile
* @param IMode $mode hashing mode
* @param IStatus $status which status is necessary to use that feature
* @param ILock $lock file lock
* @param string $path use full path with file name
* @param string[] $path use full path with file name
* @param IKauTranslations|null $lang
*/
public function __construct(CompositeAdapter $files, IMode $mode, IStatus $status, ILock $lock, string $path, ?IKauTranslations $lang = null)
public function __construct(CompositeAdapter $files, IMode $mode, IStatus $status, ILock $lock, array $path, ?IKauTranslations $lang = null)
{
$this->files = $files;
parent::__construct($mode, $status, $lock, $path, $lang);
Expand Down
12 changes: 10 additions & 2 deletions php-src/Sources/Files/Files/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ class Files extends AFiles
{
use TFiles;

public function __construct(CompositeAdapter $files, IMode $mode, IStatus $status, ILock $lock, string $dir, ?IKauTranslations $lang = null)
/**
* @param CompositeAdapter $files
* @param IMode $mode
* @param IStatus $status
* @param ILock $lock
* @param string[] $path
* @param IKauTranslations|null $lang
*/
public function __construct(CompositeAdapter $files, IMode $mode, IStatus $status, ILock $lock, array $path, ?IKauTranslations $lang = null)
{
$this->files = $files;
parent::__construct($mode, $status, $lock, $dir, $lang);
parent::__construct($mode, $status, $lock, $path, $lang);
}
}
8 changes: 7 additions & 1 deletion php-src/Sources/Files/Files/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class Groups extends AGroups
{
use TFiles;

public function __construct(CompositeAdapter $files, ILock $lock, string $path, ?IKauTranslations $lang = null)
/**
* @param CompositeAdapter $files
* @param ILock $lock
* @param string[] $path
* @param IKauTranslations|null $lang
*/
public function __construct(CompositeAdapter $files, ILock $lock, array $path, ?IKauTranslations $lang = null)
{
$this->files = $files;
parent::__construct($lock, $path, $lang);
Expand Down
16 changes: 7 additions & 9 deletions php-src/Sources/Files/Files/TFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ trait TFiles
protected $files = null;

/**
* @param string $path
* @param string[] $path
* @throws AuthException
* @return array<int, array<int, string>>
*/
protected function openFile(string $path): array
protected function openFile(array $path): array
{
try {
$pt = Stuff::pathToArray($path);
$content = $this->files->readFile($pt);
$content = $this->files->readFile($path);
$lines = explode(IFile::CRLF, strval($content));
return array_map([$this, 'explosion'], array_filter(array_map('trim', $lines), [$this, 'filterEmptyLines']));
} catch (FilesException | PathsException $ex) {
Expand All @@ -46,17 +45,16 @@ abstract public function explosion(string $input): array;
abstract public function filterEmptyLines(string $input): bool;

/**
* @param string $path
* @param string[] $path
* @param array<int, array<int, string|int>> $lines
* @throws AuthException
*/
protected function saveFile(string $path, array $lines): void
protected function saveFile(array $path, array $lines): void
{
$content = implode(IFile::CRLF, array_map([$this, 'implosion'], $lines)) . IFile::CRLF;
try {
$pt = Stuff::pathToArray($path);
if (false === $this->files->saveFile($pt, $content)) {
throw new AuthException($this->getAuLang()->kauPassFileNotSave($path));
if (false === $this->files->saveFile($path, $content)) {
throw new AuthException($this->getAuLang()->kauPassFileNotSave(Stuff::arrayToPath($path)));
}
} catch (FilesException | PathsException $ex) {
throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
Expand Down
4 changes: 2 additions & 2 deletions php-src/Sources/Files/Storage/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class File extends AFile
* @param IMode $mode hashing mode
* @param IStatus $status which status is necessary to use that feature
* @param ILock $lock file lock
* @param string $path use full path with file name
* @param string[] $path use full path with file name
* @param IKauTranslations|null $lang
*/
public function __construct(IStorage $storage, IMode $mode, IStatus $status, ILock $lock, string $path, ?IKauTranslations $lang = null)
public function __construct(IStorage $storage, IMode $mode, IStatus $status, ILock $lock, array $path, ?IKauTranslations $lang = null)
{
$this->storage = $storage;
parent::__construct($mode, $status, $lock, $path, $lang);
Expand Down
10 changes: 9 additions & 1 deletion php-src/Sources/Files/Storage/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ class Files extends AFiles
{
use TStorage;

public function __construct(IStorage $storage, IMode $mode, IStatus $status, ILock $lock, string $dir, ?IKauTranslations $lang = null)
/**
* @param IStorage $storage
* @param IMode $mode
* @param IStatus $status
* @param ILock $lock
* @param string[] $dir
* @param IKauTranslations|null $lang
*/
public function __construct(IStorage $storage, IMode $mode, IStatus $status, ILock $lock, array $dir, ?IKauTranslations $lang = null)
{
$this->storage = $storage;
parent::__construct($mode, $status, $lock, $dir, $lang);
Expand Down
8 changes: 7 additions & 1 deletion php-src/Sources/Files/Storage/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class Groups extends AGroups
{
use TStorage;

public function __construct(IStorage $storage, ILock $lock, string $path, ?IKauTranslations $lang = null)
/**
* @param IStorage $storage
* @param ILock $lock
* @param string[] $path
* @param IKauTranslations|null $lang
*/
public function __construct(IStorage $storage, ILock $lock, array $path, ?IKauTranslations $lang = null)
{
$this->storage = $storage;
parent::__construct($lock, $path, $lang);
Expand Down
21 changes: 12 additions & 9 deletions php-src/Sources/Files/Storage/TStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use kalanis\kw_auth\AuthException;
use kalanis\kw_auth\Interfaces\IFile;
use kalanis\kw_auth\Traits\TLang;
use kalanis\kw_paths\PathsException;
use kalanis\kw_paths\Stuff;
use kalanis\kw_storage\Interfaces\IStorage;
use kalanis\kw_storage\StorageException;

Expand All @@ -23,17 +25,17 @@ trait TStorage
protected $storage = null;

/**
* @param string $path
* @param string[] $path
* @throws AuthException
* @return array<int, array<int, string>>
*/
protected function openFile(string $path): array
protected function openFile(array $path): array
{
try {
$content = $this->storage->read($path);
$content = $this->storage->read(Stuff::arrayToPath($path));
$lines = explode(IFile::CRLF, strval($content));
return array_map([$this, 'explosion'], array_filter(array_map('trim', $lines), [$this, 'filterEmptyLines']));
} catch (StorageException $ex) {
} catch (StorageException | PathsException $ex) {
throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
}
}
Expand All @@ -43,18 +45,19 @@ abstract public function explosion(string $input): array;
abstract public function filterEmptyLines(string $input): bool;

/**
* @param string $path
* @param string[] $path
* @param array<int, array<int, string|int>> $lines
* @throws AuthException
*/
protected function saveFile(string $path, array $lines): void
protected function saveFile(array $path, array $lines): void
{
$content = implode(IFile::CRLF, array_map([$this, 'implosion'], $lines)) . IFile::CRLF;
try {
if (false === $this->storage->write($path, $content)) {
throw new AuthException($this->getAuLang()->kauPassFileNotSave($path));
$pt = Stuff::arrayToPath($path);
if (false === $this->storage->write($pt, $content)) {
throw new AuthException($this->getAuLang()->kauPassFileNotSave($pt));
}
} catch (StorageException $ex) {
} catch (StorageException | PathsException $ex) {
throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
}
}
Expand Down
8 changes: 4 additions & 4 deletions php-src/Sources/Files/TStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
trait TStore
{
/**
* @param string $path
* @param string[] $path
* @throws AuthException
* @return array<int, array<int, string>>
*/
abstract protected function openFile(string $path): array;
abstract protected function openFile(array $path): array;

/**
* @param string $path
* @param string[] $path
* @param array<int, array<int, string|int>> $lines
* @throws AuthException
*/
abstract protected function saveFile(string $path, array $lines): void;
abstract protected function saveFile(array $path, array $lines): void;
}

0 comments on commit f03d234

Please sign in to comment.