0 follower

Final Class Yiisoft\Rbac\Php\ItemsStorage

InheritanceYiisoft\Rbac\Php\ItemsStorage » Yiisoft\Rbac\SimpleItemsStorage
ImplementsYiisoft\Rbac\Php\FileStorageInterface
Uses TraitsYiisoft\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).

Method Details

Hide inherited methods

__construct() public method

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();
}

            
add() public method

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();
}

            
addChild() public method

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();
}

            
clear() public method

public void clear ( )

                public function clear(): void
{
    parent::clear();
    $this->save();
}

            
getFileUpdatedAt() public method
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;
}

            
load() public method

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];
            }
        }
    }
}

            
remove() public method

public void remove ( string $name )
$name string

                public function remove(string $name): void
{
    parent::remove($name);
    $this->save();
}

            
removeChild() public method

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();
}

            
removeChildren() public method

public void removeChildren ( string $parentName )
$parentName string

                public function removeChildren(string $parentName): void
{
    if (!$this->hasChildren($parentName)) {
        return;
    }
    parent::removeChildren($parentName);
    $this->save();
}