Fractal component loader plugin for Craft cms
<?php | |
namespace Craft; | |
class FractalPlugin extends BasePlugin | |
{ | |
public function init() | |
{ | |
craft()->templates->getTwig()->setLoader(new FractalTemplateLoader()); | |
} | |
public function getName() | |
{ | |
return Craft::t('Fractal'); | |
} | |
public function getVersion() | |
{ | |
return '0.1.0'; | |
} | |
public function getDeveloper() | |
{ | |
return 'Clearleft'; | |
} | |
public function getDeveloperUrl() | |
{ | |
return 'http://clearleft.com'; | |
} | |
public function hasCpSection() | |
{ | |
return false; | |
} | |
} | |
class FractalTemplateLoader implements \Twig_LoaderInterface | |
{ | |
// Public Methods | |
// ========================================================================= | |
/** | |
* Checks if a template exists. | |
* | |
* @param string $name | |
* | |
* @return bool | |
*/ | |
public function exists($name) | |
{ | |
return craft()->templates->doesTemplateExist($name); | |
} | |
/** | |
* Gets the source code of a template. | |
* | |
* @param string $name The name of the template to load, or a StringTemplate object. | |
* | |
* @throws Exception | |
* @return string The template source code. | |
*/ | |
public function getSource($name) | |
{ | |
if (is_string($name)) | |
{ | |
$template = $this->_findTemplate($name); | |
if (IOHelper::isReadable($template)) | |
{ | |
return IOHelper::getFileContents($template); | |
} | |
else | |
{ | |
throw new Exception(Craft::t('Tried to read the template at {path}, but could not. Check the permissions.', array('path' => $template))); | |
} | |
} | |
else | |
{ | |
return $name->template; | |
} | |
} | |
/** | |
* Gets the cache key to use for the cache for a given template. | |
* | |
* @param string $name The name of the template to load, or a StringTemplate object. | |
* | |
* @return string The cache key (the path to the template) | |
*/ | |
public function getCacheKey($name) | |
{ | |
if (is_string($name)) | |
{ | |
return $this->_findTemplate($name); | |
} | |
else | |
{ | |
return $name->cacheKey; | |
} | |
} | |
/** | |
* Returns whether the cached template is still up-to-date with the latest template. | |
* | |
* @param string $name The template name, or a StringTemplate object. | |
* @param int $time The last modification time of the cached template | |
* | |
* @return bool | |
*/ | |
public function isFresh($name, $time) | |
{ | |
// If this is a CP request and a DB update is needed, force a recompile. | |
if (craft()->request->isCpRequest() && craft()->updates->isCraftDbMigrationNeeded()) | |
{ | |
return false; | |
} | |
if (is_string($name)) | |
{ | |
$sourceModifiedTime = IOHelper::getLastTimeModified($this->_findTemplate($name)); | |
return $sourceModifiedTime && $sourceModifiedTime->getTimestamp() <= $time; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
// Private Methods | |
// ========================================================================= | |
/** | |
* Returns the path to a given template, or throws a TemplateLoaderException. | |
* | |
* @param $name | |
* | |
* @throws TemplateLoaderException | |
* @return string $name | |
*/ | |
private function _findTemplate($name) | |
{ | |
if (strpos($name, '@') === 0) | |
{ | |
$mappingPath = defined('FRACTAL_COMPONENTS_MAP') ? FRACTAL_COMPONENTS_MAP : CRAFT_BASE_PATH . '../components-map.json'; | |
if (IOHelper::isReadable($mappingPath)) | |
{ | |
$mappings = json_decode(IOHelper::getFileContents($mappingPath)); | |
if ($mappings->$name) { | |
if (strpos($mappings->$name, '/') !== 0) { | |
$template = realpath(CRAFT_BASE_PATH . '../') . '/' . $mappings->$name; | |
} else { | |
$template = $mappings->$name; | |
} | |
} | |
} | |
else | |
{ | |
throw new Exception(Craft::t('Could not read Fractal mappings file at %s.', array('path' => FRACTAL_COMPONENTS_MAP))); | |
} | |
} | |
else | |
{ | |
$template = craft()->templates->findTemplate($name); | |
} | |
if (!$template) | |
{ | |
throw new TemplateLoaderException($name); | |
} | |
return $template; | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
How do I utilize this version, as we haven't upgraded to Craft 3 yet? |
This comment has been minimized.
This comment has been minimized.
We have converted this into a Craft 3 plugin here - https://github.com/ournameismud/fractal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Hello,
Any thoughts/pointers on converting this to work with craft 3?
Cheers