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;
}
}