diff options
author | marvin-borner@live.com | 2018-04-16 21:09:05 +0200 |
---|---|---|
committer | marvin-borner@live.com | 2018-04-16 21:09:05 +0200 |
commit | cf14306c2b3f82a81f8d56669a71633b4d4b5fce (patch) | |
tree | 86700651aa180026e89a66064b0364b1e4346f3f /main/app/sprinkles/core/src/Twig | |
parent | 619b01b3615458c4ed78bfaeabb6b1a47cc8ad8b (diff) |
Main merge to user management system - files are now at /main/public/
Diffstat (limited to 'main/app/sprinkles/core/src/Twig')
-rwxr-xr-x | main/app/sprinkles/core/src/Twig/CacheHelper.php | 58 | ||||
-rwxr-xr-x | main/app/sprinkles/core/src/Twig/CoreExtension.php | 124 |
2 files changed, 182 insertions, 0 deletions
diff --git a/main/app/sprinkles/core/src/Twig/CacheHelper.php b/main/app/sprinkles/core/src/Twig/CacheHelper.php new file mode 100755 index 0000000..14aea49 --- /dev/null +++ b/main/app/sprinkles/core/src/Twig/CacheHelper.php @@ -0,0 +1,58 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Core\Twig; + +use Interop\Container\ContainerInterface; +use Illuminate\Filesystem\Filesystem; + +/** + * Provides helper function to delete the Twig cache directory + * + * @author Alex Weissman (https://alexanderweissman.com) + */ +class CacheHelper +{ + + /** + * @var ContainerInterface The global container object, which holds all your services. + */ + protected $ci; + + /** + * Constructor. + * + * @param ContainerInterface $ci The global container object, which holds all your services. + */ + public function __construct(ContainerInterface $ci) + { + $this->ci = $ci; + } + + /** + * Function that delete the Twig cache directory content + * + * @access public + * @return bool true/false if operation is successfull + */ + public function clearCache() + { + // Get location + $path = $this->ci->locator->findResource('cache://twig', true, true); + + // Get Filesystem instance + $fs = new FileSystem; + + // Make sure directory exist and delete it + if ($fs->exists($path)) { + return $fs->deleteDirectory($path, true); + } + + // It's still considered a success if directory doesn't exist yet + return true; + } +} diff --git a/main/app/sprinkles/core/src/Twig/CoreExtension.php b/main/app/sprinkles/core/src/Twig/CoreExtension.php new file mode 100755 index 0000000..6a89d12 --- /dev/null +++ b/main/app/sprinkles/core/src/Twig/CoreExtension.php @@ -0,0 +1,124 @@ +<?php +/** + * UserFrosting (http://www.userfrosting.com) + * + * @link https://github.com/userfrosting/UserFrosting + * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) + */ +namespace UserFrosting\Sprinkle\Core\Twig; + +use Interop\Container\ContainerInterface; +use UserFrosting\Sprinkle\Core\Util\Util; + +/** + * Extends Twig functionality for the Core sprinkle. + * + * @author Alex Weissman (https://alexanderweissman.com) + */ +class CoreExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface +{ + + /** + * @var ContainerInterface The global container object, which holds all your services. + */ + protected $services; + + /** + * Constructor. + * + * @param ContainerInterface $services The global container object, which holds all your services. + */ + public function __construct(ContainerInterface $services) + { + $this->services = $services; + } + + /** + * Get the name of this extension. + * + * @return string + */ + public function getName() + { + return 'userfrosting/core'; + } + + /** + * Adds Twig functions `getAlerts` and `translate`. + * + * @return array[\Twig_SimpleFunction] + */ + public function getFunctions() + { + return array( + // Add Twig function for fetching alerts + new \Twig_SimpleFunction('getAlerts', function ($clear = true) { + if ($clear) { + return $this->services['alerts']->getAndClearMessages(); + } else { + return $this->services['alerts']->messages(); + } + }), + new \Twig_SimpleFunction('translate', function ($hook, $params = array()) { + return $this->services['translator']->translate($hook, $params); + }, [ + 'is_safe' => ['html'] + ]) + ); + } + + /** + * Adds Twig filters `unescape`. + * + * @return array[\Twig_SimpleFilter] + */ + public function getFilters() + { + return array( + /** + * Converts phone numbers to a standard format. + * + * @param String $num A unformatted phone number + * @return String Returns the formatted phone number + */ + new \Twig_SimpleFilter('phone', function ($num) { + return Util::formatPhoneNumber($num); + }), + new \Twig_SimpleFilter('unescape', function ($string) { + return html_entity_decode($string); + }) + ); + } + + /** + * Adds Twig global variables `site` and `assets`. + * + * @return array[mixed] + */ + public function getGlobals() + { + // CSRF token name and value + $csrfNameKey = $this->services->csrf->getTokenNameKey(); + $csrfValueKey = $this->services->csrf->getTokenValueKey(); + $csrfName = $this->services->csrf->getTokenName(); + $csrfValue = $this->services->csrf->getTokenValue(); + + $csrf = [ + 'csrf' => [ + 'keys' => [ + 'name' => $csrfNameKey, + 'value' => $csrfValueKey + ], + 'name' => $csrfName, + 'value' => $csrfValue + ] + ]; + + $site = array_replace_recursive($this->services->config['site'], $csrf); + + return [ + 'site' => $site, + 'assets' => $this->services->assets + ]; + } +} |