Simple caching tool

@param  string $attribute
@param  Clousre $closure

@return mixed
970182
public function remember($attribute, $closure)
    {
        $key = $attribute.'_'.$this->location;

        if (!Cache::has($key)) {
            $result = $closure();
            Cache::forever($key, $result);
        }

        return Cache::get($key);
    }
959863
public function share(Closure $closure)
    {
        return function($container) use ($closure)
        {
            // We'll simply declare a static variable within the Closures and if it has
            // not been set we will execute the given Closures to resolve this value
            // and return it back to these consumers of the method as an instance.
            static $object;

            if (is_null($object)) {
                $object = $closure($container);
            }

            return $object;
        };
    }
780083
public function api(array $attributes, Closure $callback)
    {
        $attributes['prefix'] = array_key_exists('prefix', $attributes) ? sprintf('%s/%s', 'api', $attributes['prefix']) : 'api';
        parent::group($attributes, $callback);
    }
1239031
function sortCustomKey(callable $closure) {
        $args = $this->baseStorageArgs;
        $args[] = parent::sortCustomKey($closure)->all();
        
        return (new static(...$args));
    }
748281
public function andFilter(Closure $closure)
    {
        $query = $this->newNestedInstance($closure);

        $filter = $this->grammar->compileAnd($query->getQuery());

        return $this->rawFilter($filter);
    }
74149
public function resolve($resource, $attribute = null)
    {
        $attribute = $attribute ?? $this->attribute;

        $value = $resource->{$attribute} ?? null;

        $value = is_object($value) || is_array($value) ? $value : json_decode($value);

        $fields = $this->fields->whereInstanceOf(Resolvable::class)->reduce(function ($values, $field) {
            return $values->map(function ($row) use ($field) {
                $key = $field->attribute;
                $cb = $field->resolveCallback;

                if (isset($row->{$key})) {
                    $row->{$key} = $cb ? call_user_func($cb, $row->{$key}) : $row->{$key};
                }

                if (property_exists($field, 'computeCallback') && $field->computeCallback) {
                    $row->{$key} = call_user_func($field->computeCallback, $row);
                }

                return $row;
            });
        }, collect($value));

        if (!$this->resolveCallback) {
            $this->resolveCallback = function () use ($fields) {
                return $fields->toArray();
            };
        }

        $this->withMeta(['fields' => $this->fields]);

        parent::resolve($resource, $attribute);
    }
378695
public function group($attributes, $closure)
    {
        $this->updateGroupStack($attributes);

        // Once we have updated the group stack, we will execute the user Closure and
        // merge in the groups attributes when the item is created. After we have
        // run the callback, we will pop the attributes off of this group stack.
        call_user_func($closure, $this);

        array_pop($this->groupStack);
    }
1360558
public function rememberForever($key, Closure $callback)
	{
		// If the item exists in the cache we will just return this immediately
		// otherwise we will execute the given Closure and cache the result
		// of that execution for the given number of minutes. It's easy.
		if ($this->has($key)) return $this->get($key);

		$this->forever($key, $value = $callback());

		return $value;
	}
1362520
public function provideOpenLinkAttributes($context, Closure $closure)
    {
        $closure->bindTo($this);
        $this->extend($this->getContextExtendName($context), $closure);
        return $this;
    }
211911
function storeObjectAttribute( $attribute )
    {
        $ini = eZINI::instance();
        // Delete compiled template
        $siteINI = eZINI::instance();
        if ( $siteINI->hasVariable( 'FileSettings', 'CacheDir' ) )
        {
            $cacheDir = $siteINI->variable( 'FileSettings', 'CacheDir' );
            if ( $cacheDir[0] == "/" )
            {
                $cacheDir = eZDir::path( array( $cacheDir ) );
            }
            else
            {
                if ( $siteINI->hasVariable( 'FileSettings', 'VarDir' ) )
                {
                    $varDir = $siteINI->variable( 'FileSettings', 'VarDir' );
                    $cacheDir = eZDir::path( array( $varDir, $cacheDir ) );
                }
            }
        }
        else if ( $siteINI->hasVariable( 'FileSettings', 'VarDir' ) )
        {
            $varDir = $siteINI->variable( 'FileSettings', 'VarDir' );
            $cacheDir = $ini->variable( 'FileSettings', 'CacheDir' );
            $cacheDir = eZDir::path( array( $varDir, $cacheDir ) );
        }
        else
        {
            $cacheDir =  eZSys::cacheDirectory();
        }
        $compiledTemplateDir = $cacheDir ."/template/compiled";
        eZDir::unlinkWildcard( $compiledTemplateDir . "/", "*pagelayout*.*" );

        // Expire template block cache
        eZContentCacheManager::clearTemplateBlockCacheIfNeeded( false );
    }
817810
public static function create($attributes = null, Closure $block = null)
    {
        if ($attributes instanceof \Closure) {
            $block = $attributes;
            $attributes = [];
        } elseif (!is_array($attributes)) {
            throw new Exception\InvalidArgumentException(
                sprintf(
                    'First argument must be either array or Closure, %s passed',
                    gettype($attributes)
                )
            );
        }
        
        if (isset($attributes[0])) {
            $coll = static::collection();
            foreach ($attributes as $attrs) {
                $coll->add(static::create($attrs, $block));
            }
            return $coll;
        } else {
            $model = new static();
            $model->assignAttributes($attributes);
            if ($block) {
                $block($model);
            }
            $model->save();
            return $model;
        }
    }
515764
public function getAttached($attribute): Collection
    {
        /** @var Collection $coll */
        $coll = $this->getRelationValue($attribute);

        return $coll->map(function ($data) {
            return Finder::createStore($data);
        });
    }
948051
public static function group(array $attributes, Closure $closure)
    {
        $hash = uniqid('prop_');
        //Add options
        if (isset($attributes['prefix'])) {
            static::$groupsProps['prefix'][$hash] = $attributes['prefix'];
        }
        if (isset($attributes['namespace'])) {
            static::$groupsProps['namespace'][$hash] = $attributes['namespace'];
        }
        if (isset($attributes['name'])) {
            static::$groupsProps['name'][$hash] = $attributes['name'];
        }
        if (isset($attributes['middlewares']) || isset($attributes['middleware'])) {
            static::$groupsProps['middleware'][$hash] = isset($attributes['middleware']) ?
                (array)$attributes['middleware'] :
                (array)$attributes['middlewares'];
        }
        //Run closure
        $closure();
        //Remove options
        unset(static::$groupsProps['prefix'][$hash]);
        unset(static::$groupsProps['namespace'][$hash]);
        unset(static::$groupsProps['name'][$hash]);
        unset(static::$groupsProps['middleware'][$hash]);
    }
283709
public function byClosure(Closure $closure)
    {
        $this->controller = new Controller();
        if ($this->hasContainer() && method_exists($this->controller, 'raikiri'))
            $this->controller->setContainer($this->raikiri());

        $this->action = $closure->bindTo($this->controller);
    }
590707
public function resolve($resource, $attribute = null)
    {
        $attribute = $attribute ?? $this->attribute;

        $value = $resource->{$attribute};

        $this->value = is_object($value) || is_array($value) ? $value : json_decode($value);

        $this->fields->whereInstanceOf(Date::class)->each(function ($dateField) {
            $dateField->resolveCallback = function ($value) {
                return \Carbon\Carbon::parse($value)->format('Y-m-d');
            };
        });

        $this->fields->whereInstanceOf(Resolvable::class)->each->resolve($this->value);

        $this->withMeta(['fields' => $this->fields]);

        parent::resolve($resource, $attribute);
    }
819137
public function lookup($key, $closure=null)
	{
		// if a closure was provided and we missed, store it
		if(!is_null($closure) && !isset($this->_registry[$key]) && !isset($this->_triggers[$key]))
		{
			$this->_registry[$key] = $this->_memoize($closure($key));
		}

		// the registry stores closures
		if(isset($this->_registry[$key]))
		{
			$result = call_user_func($this->_registry[$key], $this);
			$this->_registry[$key] = $this->_memoize($result);
			return $result;
		}
		// try any registered triggers
		else if(isset($this->_triggers[$key]))
		{
			call_user_func($this->_triggers[$key], $this);
			unset($this->_triggers[$key]);
			return $this->lookup($key);
		}
		else
		{
			throw new RegistryException("No entry for key '$key'");
		}
	}
1265587
protected function fetch($id, $closure, $refresh = false)
    {
        return $this->once($id, $closure, $refresh);
    }