Adds a context to the cache. If the cache is full, discards a batch of
older entries.

@static
@param context $context New context to add
@return void
179037
protected static function cache_add(context $context) {
        if (isset(self::$cache_contextsbyid[$context->id])) {
            // already cached, no need to do anything - this is relatively cheap, we do all this because count() is slow
            return;
        }

        if (self::$cache_count >= CONTEXT_CACHE_MAX_SIZE) {
            $i = 0;
            foreach(self::$cache_contextsbyid as $ctx) {
                $i++;
                if ($i <= 100) {
                    // we want to keep the first contexts to be loaded on this page, hopefully they will be needed again later
                    continue;
                }
                if ($i > (CONTEXT_CACHE_MAX_SIZE / 3)) {
                    // we remove oldest third of the contexts to make room for more contexts
                    break;
                }
                unset(self::$cache_contextsbyid[$ctx->id]);
                unset(self::$cache_contexts[$ctx->contextlevel][$ctx->instanceid]);
                self::$cache_count--;
            }
        }

        self::$cache_contexts[$context->contextlevel][$context->instanceid] = $context;
        self::$cache_contextsbyid[$context->id] = $context;
        self::$cache_count++;
    }
1242435
public function setContext(array $context)
    {
        $context = array_merge($this->getDefaultContext(), $context);

        // Always save to property in service for fallback
        $this->context = $context;

        // Try to save the context in session,
        if ($this->session) {
            $this->session->set(static::SESSION_KEY, $context);
            $this->session->save();
        }
    }
1261748
public function setContextLocale($context, $locale)
    {
        if (isset($this->contextLocales[$context]) && $this->contextLocales[$context] == $locale) {
            return;
        }
        $this->contextLocales[$context] = $locale;
        if ($context === $this->activeContext) {
            $this->currentLocaleChanged($locale);
        }
    }
508321
protected function _actionAdd(AnCommandContext $context)
    {
        $data = $context->data;

        return $this->getRepository()->create($data->toArray());
    }
18739
protected function _actionAddapp(AnCommandContext $context)
    {
        $data = $context->data;
        $this->getItem()->components->insert($data->app);
        $this->commit();
    }
1072997
protected function _beforeUpdate(DatabaseContextInterface $context)
    {
        if(isset($this->order) && $this->hasProperty('ordering')) {
            $this->order($this->order);
        }
    }
662223
public function setContext(array $context)
    {
        $this->clearContext();
        foreach ($context as $key => $value) {
            $this->setContextValue($key, $value);
        }
    }
1188023
public function setContext($context)
    {
        if (!is_array($context) && !$context instanceof \ArrayObject && !$context instanceof \Iterator) {
            throw new EventException('Unexpected value type for context', EventException::INVALID_CONTEXT);
        }

        $context = Collection::cast($context);

        $this->context = $context;

        return $this;
    }
998636
protected function _afterEntityInsert(AnCommandContext $context)
    {
        $this->votee->getRepository()
                    ->getBehavior('votable')
                    ->resetStats(array($this->votee));
    }
516992
protected function _afterEntityInsert(AnCommandContext $context)
    {
        $this->parent->getRepository()
                     ->getBehavior('commentable')
                     ->resetStats(array($this->parent));
        $this->parent->execute('after.comment', array('comment' => $this));
    }
1335166
public function context(ContextInterface $context)
    {
        if (!empty($this->vary)) {
            $this->varyValue = $context->get($this->vary);
        }
    }
745095
protected static function cache_remove(context $context) {
        if (!isset(self::$cache_contextsbyid[$context->id])) {
            // not cached, no need to do anything - this is relatively cheap, we do all this because count() is slow
            return;
        }
        unset(self::$cache_contexts[$context->contextlevel][$context->instanceid]);
        unset(self::$cache_contextsbyid[$context->id]);

        self::$cache_count--;

        if (self::$cache_count < 0) {
            self::$cache_count = 0;
        }
    }
144845
public function initializeContext(Context $context)
    {
        if ($context instanceof ResourceContext) {
            $reflector = new ReflectionClass(get_class($context));

            if ($this->builder->getLoader() instanceof FileResourceLoader) {
                if ($this->builder->getLoader()->getBasePath() == null) {
                    $this->builder->getLoader()->setBasePath(dirname($reflector->getFileName()));
                }
            }

            if ($context->getResourceFactory()) {
                $this->builder->setFactory($context->getResourceFactory());
            }

            $context->setResourceBuilder($this->builder);
        }
    }
1155657
protected function _beforeUpdate(DatabaseContext $context)
    {
        //Get the modified columns
        $modified  = $this->getTable()->filter($this->getProperties(true));

        if(!empty($modified)) {
            $this->_beforeInsert($context);
        }
    }
1171619
protected function _beforeFetch(ModelContextInterface $context)
    {
        $model = $context->getSubject();

        if ($model instanceof ModelDatabase && !$context->state->isUnique())
        {
            $state = $context->state;
            $limit = $state->limit;

            if ($limit)
            {
                $offset = $state->offset;
                $total  = $this->count();

                if ($offset !== 0 && $total !== 0)
                {
                    // Recalculate the offset if it is set to the middle of a page.
                    if ($offset % $limit !== 0) {
                        $offset -= ($offset % $limit);
                    }

                    // Recalculate the offset if it is higher than the total
                    if ($offset >= $total) {
                        $offset = floor(($total - 1) / $limit) * $limit;
                    }

                    $state->offset = $offset;
                }

                $context->query->limit($limit, $offset);
            }
        }
    }
406173
private function expoundContext(&$context)
    {
        foreach ($context as $key => &$value) {
            if (is_array($value) || is_object($value)) {
                $this->expoundContext($value);
            }
            $t    = &$context;
            $keys = explode('.', $key);
            if (count($keys) > 1) {
                foreach ($keys as $subKey) {
                    if (!isset($t[$subKey])) {
                        $t[$subKey] = [];
                    }
                    $t = &$t[$subKey];
                }
                $t = $value;
                unset($context[$key]);
            }
        }
    }
403943
public function setContext($context)
    {
        $contexts = array(self::CONTEXT_CREDITCARD, self::CONTEXT_TOKEN);
        if (!in_array($context, $contexts)) {
            throw new RuntimeException(
                sprintf("Invalid validation context '%s'", $context)
            );
        }

        $this->context = $context;
    }