RainLab\Pages\Classes\SnippetManager::findByCodeOrComponent PHP Method

findByCodeOrComponent() public method

This method is used internally by the system.
public findByCodeOrComponent ( Cms\Classes\Theme $theme, string $code, $componentClass, boolean $allowCaching = false ) : array
$theme Cms\Classes\Theme Specifies a parent theme.
$code string Specifies the snippet code.
$allowCaching boolean Specifies whether caching is allowed for the call.
return array Returns an array of Snippet objects.
    public function findByCodeOrComponent($theme, $code, $componentClass, $allowCaching = false)
    {
        if (!$allowCaching) {
            // If caching is not allowed, list all available snippets,
            // find the snippet in the list and return it.
            $snippets = $this->listSnippets($theme);
            foreach ($snippets as $snippet) {
                if ($componentClass && $snippet->getComponentClass() == $componentClass) {
                    return $snippet;
                }
                if ($snippet->code == $code) {
                    return $snippet;
                }
            }
            return null;
        }
        // If caching is allowed, and the requested snippet is a partial snippet,
        // try to load the partial name from the cache and initialize the snippet
        // from the partial.
        if (!strlen($componentClass)) {
            $map = $this->getPartialSnippetMap($theme);
            if (!array_key_exists($code, $map)) {
                return null;
            }
            $partialName = $map[$code];
            $partial = Partial::loadCached($theme, $partialName);
            if (!$partial) {
                return null;
            }
            $snippet = new Snippet();
            $snippet->initFromPartial($partial);
            return $snippet;
        } else {
            // If the snippet is a component snippet, initialize it
            // from the component
            if (!class_exists($componentClass)) {
                throw new SystemException(sprintf('The snippet component class %s is not found.', $componentClass));
            }
            $snippet = new Snippet();
            $snippet->initFromComponentInfo($componentClass, $code);
            return $snippet;
        }
    }