Skip to content
Snippets Groups Projects
Select Git revision
  • 37a62bb6443a04273641ce30a463f2fe3a2193cd
  • master default protected
2 results

implements.ejs.t

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StudipTreeNodeCachableTrait.php 1.67 KiB
    <?php
    trait StudipTreeNodeCachableTrait
    {
        protected static $descendants_cache_array = null;
    
        protected static function getDescendantsCacheArray(): StudipCachedArray
        {
            if (self::$descendants_cache_array === null) {
                self::$descendants_cache_array = new StudipCachedArray(
                    static::class . '/descendants',
                    30 * 60
                );
            }
            return self::$descendants_cache_array;
        }
    
        protected static function registerCachableCallbacks(array $config): array
        {
            if (!isset($config['registered_callbacks'])) {
                $config['registered_callbacks'] = [];
            }
    
            if (!isset($config['registered_callbacks']['before_store'])) {
                $config['registered_callbacks']['before_store'] = [];
            }
            $config['registered_callbacks']['before_store'][] = function ($node): void {
                self::getDescendantsCacheArray()->expire();
            };
    
            if (!isset($config['registered_callbacks']['after_delete'])) {
                $config['registered_callbacks']['after_delete'] = [];
            }
            $config['registered_callbacks']['after_delete'][] = function ($node): void {
                self::getDescendantsCacheArray()->expire();
            };
    
            return $config;
        }
    
        protected function getDescendantIds(): array
        {
            $cache = self::getDescendantsCacheArray();
    
            if (isset($cache[$this->id])) {
                return $cache[$this->id];
            }
    
            $ids = [];
    
            foreach ($this->getChildNodes() as $child) {
                $ids = array_merge($ids, [$child->id], $child->getDescendantIds());
            }
    
            $cache[$this->id] = $ids;
    
            return $ids;
        }
    
    }