Stash::destroy PHP Method

destroy() public method

Unset variable(s) in the current session, optionally flush from db
public destroy ( mixed $params = [], string $type = 'variable', string $scope = 'user' ) : void
$params mixed The name of the variable to unset, or an array of key => value pairs
$type string The type of variable
$scope string The scope of the variable
return void
    public function destroy($params = array(), $type = 'variable', $scope = 'user')
    {
        // is this method being called directly?
        if (func_num_args() > 0) {
            if (!(isset($this) && get_class($this) == __CLASS__)) {
                return self::_api_static_call(__FUNCTION__, $params, $type, $scope);
            } else {
                return $this->_api_call(__FUNCTION__, $params, $type, $scope);
            }
        }
        // register params
        $name = $this->EE->TMPL->fetch_param('name', FALSE);
        $context = $this->EE->TMPL->fetch_param('context', NULL);
        $scope = strtolower($this->EE->TMPL->fetch_param('scope', $this->default_scope));
        $flush_cache = (bool) preg_match('/1|on|yes|y/i', $this->EE->TMPL->fetch_param('flush_cache', 'yes'));
        $bundle = $this->EE->TMPL->fetch_param('bundle', NULL);
        $bundle_id = $this->EE->TMPL->fetch_param('bundle_id', FALSE);
        // narrow the scope to user session?
        $session_id = NULL;
        if ($scope === 'user') {
            $session_id = $this->_session_id;
        } elseif ($scope === 'site') {
            $session_id = '_global';
        }
        // named bundle?
        if (!is_null($bundle) && !$bundle_id) {
            $bundle_id = $this->EE->stash_model->get_bundle_by_name($bundle);
        }
        // unset a single variable, or multiple variables that match a regex
        if ($name) {
            if (preg_match('/^#(.*)#$/', $name)) {
                // remove matching variable keys from the session
                foreach ($this->_stash as $var_key => $var_val) {
                    if (preg_match($name, $var_key)) {
                        unset($this->_stash[$var_key]);
                    }
                }
                // remove from db cache?
                if ($flush_cache && $scope !== 'local') {
                    // delete variables with key_names that match the regex
                    $this->EE->stash_model->delete_matching_keys($bundle_id, $session_id, $this->site_id, trim($name, '#'), $this->invalidation_period);
                }
            } else {
                // a named variable
                if ($context !== NULL && count(explode(':', $name) == 1)) {
                    $name = $context . ':' . $name;
                }
                // remove from session
                if (isset($this->_stash[$name])) {
                    unset($this->_stash[$name]);
                }
                // remove from db cache?
                if ($flush_cache && $scope !== 'local') {
                    // replace '@' placeholders with the current context
                    $stash_key = $this->_parse_context($name);
                    // as we're deleting a specific key, the bundle_id is required
                    $bundle_id = $bundle_id ? $bundle_id : $this->bundle_id;
                    $this->EE->stash_model->delete_key($stash_key, $bundle_id, $session_id, $this->site_id, $this->invalidation_period);
                }
            }
        } elseif ($scope === 'user' || $scope === 'site' || $scope === 'all') {
            // unset ALL user-scoped variables in the current process
            $this->_stash = array();
            // remove from cache
            if ($flush_cache) {
                $this->EE->stash_model->delete_matching_keys($bundle_id, $session_id, $this->site_id, NULL, $this->invalidation_period);
            }
        }
    }