Final Class Yiisoft\Rbac\Php\ItemsStorage
| Inheritance | Yiisoft\Rbac\Php\ItemsStorage » Yiisoft\Rbac\SimpleItemsStorage |
|---|---|
| Implements | Yiisoft\Rbac\Php\FileStorageInterface |
| Uses Traits | Yiisoft\Rbac\Php\FileStorageTrait |
Storage stores roles and permissions in PHP file specified in {@see ItemsStorage::$itemFile}.
It is suitable for authorization data that is not too big (for example, the authorization data for a personal blog system).
Public Methods
Method Details
| public mixed __construct ( string $filePath, callable|null $getFileUpdatedAt = null ) | ||
| $filePath | string | |
| $getFileUpdatedAt | callable|null | |
public function __construct(string $filePath, ?callable $getFileUpdatedAt = null)
{
$this->initFileProperties($filePath, $getFileUpdatedAt);
$this->load();
}
| public void add ( \Yiisoft\Rbac\Permission|\Yiisoft\Rbac\Role $item ) | ||
| $item | \Yiisoft\Rbac\Permission|\Yiisoft\Rbac\Role | |
public function add(Permission|Role $item): void
{
parent::add($item);
$this->save();
}
| public void addChild ( string $parentName, string $childName ) | ||
| $parentName | string | |
| $childName | string | |
public function addChild(string $parentName, string $childName): void
{
parent::addChild($parentName, $childName);
$this->save();
}
| public integer getFileUpdatedAt ( ) |
public function getFileUpdatedAt(): int
{
$getFileUpdatedAt = $this->getFileUpdatedAt;
$fileUpdatedAt = $getFileUpdatedAt($this->filePath);
if (!is_int($fileUpdatedAt)) {
throw new RuntimeException('getFileUpdatedAt callable must return a UNIX timestamp.');
}
return $fileUpdatedAt;
}
| public void load ( ) |
public function load(): void
{
parent::clear();
/** @psalm-var array<string, RawItem> $items */
$items = $this->loadFromFile($this->filePath);
if (empty($items)) {
return;
}
$fileUpdatedAt = $this->getFileUpdatedAt();
foreach ($items as $item) {
$this->items[$item['name']] = $this
->getInstanceFromAttributes($item)
->withCreatedAt($item['created_at'] ?? $fileUpdatedAt)
->withUpdatedAt($item['updated_at'] ?? $fileUpdatedAt);
}
foreach ($items as $item) {
foreach ($item['children'] ?? [] as $childName) {
if ($this->hasItem($childName)) {
$this->children[$item['name']][$childName] = $this->items[$childName];
}
}
}
}
| public void remove ( string $name ) | ||
| $name | string | |
public function remove(string $name): void
{
parent::remove($name);
$this->save();
}
| public void removeChild ( string $parentName, string $childName ) | ||
| $parentName | string | |
| $childName | string | |
public function removeChild(string $parentName, string $childName): void
{
if (!$this->hasDirectChild($parentName, $childName)) {
return;
}
parent::removeChild($parentName, $childName);
$this->save();
}
| public void removeChildren ( string $parentName ) | ||
| $parentName | string | |
public function removeChildren(string $parentName): void
{
if (!$this->hasChildren($parentName)) {
return;
}
parent::removeChildren($parentName);
$this->save();
}
Signup or Login in order to comment.