Last active
August 26, 2021 15:10
-
-
Save allmarkedup/72afed8e97c9b2e8c61c36b798ae1870 to your computer and use it in GitHub Desktop.
Fractal component loader plugin for Craft cms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We have converted this into a Craft 3 plugin here - https://github.com/ournameismud/fractal