Source of file FontAwesomeBackend.php
Size: 7,970 Bytes - Last Modified: 2021-12-23T10:36:49+00:00
/var/www/docs.ssmods.com/process/src/src/Backends/FontAwesomeBackend.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 | <?php /** * This file is part of SilverWare. * * PHP version >=5.6.0 * * For full copyright and license information, please view the * LICENSE.md file that was distributed with this source code. * * @package SilverWare\FontIcons\Backends * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-font-icons */ namespace SilverWare\FontIcons\Backends; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Flushable; use SilverStripe\Core\Injector\Injector; use SilverStripe\View\ArrayData; use SilverWare\FontIcons\Interfaces\FontIconBackend; use Symfony\Component\Yaml\Yaml; /** * An implementation of the font icon backend interface for Font Awesome. * * @package SilverWare\FontIcons\Backends * @author Colin Tucker <colin@praxis.net.au> * @copyright 2017 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-font-icons */ class FontAwesomeBackend implements FontIconBackend, Flushable { /** * Defines the source URL used to obtain icon data. * * @var string */ protected $source = 'https://raw.githubusercontent.com/FortAwesome/Font-Awesome/{version}/src/icons.yml'; /** * An array of identifiers mapped to the equivalent stylesheet class names. * * @var array */ protected $classes = []; /** * Defines the version of Font Awesome in use. * * @var string */ protected $version; /** * Clears the font icon cache upon flush. * * @return void */ public static function flush() { self::cache()->clear(); } /** * Answers the cache object. * * @return CacheInterface */ public static function cache() { return Injector::inst()->get(CacheInterface::class . '.FontIconCache'); } /** * Defines the value of the source attribute. * * @param string $source * * @return $this */ public function setSource($source) { $this->source = (string) $source; return $this; } /** * Answers the value of the source attribute. * * @return string */ public function getSource() { return $this->source; } /** * Defines the value of the classes attribute. * * @param array $classes * * @return $this */ public function setClasses($classes) { $this->classes = (array) $classes; return $this; } /** * Answers the value of the classes attribute. * * @return array */ public function getClasses() { return $this->classes; } /** * Defines the value of the version attribute. * * @param string $version * * @return $this */ public function setVersion($version) { $this->version = (string) $version; return $this; } /** * Answers the value of the version attribute. * * @return stringh */ public function getVersion() { return $this->version; } /** * Answers the tag name to use for icons within the field. * * @return string */ public function getFieldTagName() { return 'i'; } /** * Answers the classes to use for icons within the field. * * @return string */ public function getFieldClasses() { return 'fa fa-fw fa-{value}'; } /** * Answers a font icon tag with the given class names and optional color. * * @param string $classNames * @param string $color * * @return DBHTMLText */ public function getTag($classNames, $color = null) { return ArrayData::create([ 'ClassNames' => $classNames, 'Color' => $color ])->renderWith(sprintf('%s\Tag', self::class)); } /** * Answers the class name for the specified identifier. * * @param string $identifier * @param array $args * * @return string */ public function getClassName($identifier, $args = []) { if (isset($this->classes[$identifier])) { return $args ? vsprintf($this->classes[$identifier], $args) : $this->classes[$identifier]; } } /** * Answers an associative array of icon IDs mapped to icon names. * * @return array */ public function getIcons() { // Initialise: $icons = []; // Iterate Grouped Icons: foreach ($this->getGroupedIcons() as $name => $group) { foreach ($group as $id => $icon) { if (!isset($icons[$id])) { $icons[$id] = isset($icon['name']) ? $icon['name'] : $id; } } } // Sort Icons by Key: ksort($icons); // Answer Icons: return $icons; } /** * Answers an array of icons grouped into their respective categories. * * @return array */ public function getGroupedIcons() { // Answer Cached Icons (if available): if ($icons = self::cache()->get($this->getCacheKey())) { return $icons; } // Initialise: $icons = []; // Parse Icon Source Data: $data = Yaml::parse($this->getSourceData()); // Build Icon Groups: if (isset($data['icons'])) { foreach ($data['icons'] as $icon) { foreach ($icon['categories'] as $category) { // Create Category Array: if (!isset($icons[$category])) { $icons[$category] = []; } // Create Icon Element: $icons[$category][$icon['id']] = [ 'name' => $icon['name'], 'unicode' => $icon['unicode'] ]; } } } // Sort Icons by Group: ksort($icons); // Sort Icon Groups by Name: foreach ($icons as &$group) { uasort($group, function ($a, $b) { return strcasecmp($a['name'], $b['name']); }); } // Store Icons in Cache: self::cache()->set($this->getCacheKey(), $icons); // Answer Grouped Icons: return $icons; } /** * Answers the key used with the cache. * * @return string */ public function getCacheKey() { return sprintf('font-awesome-%s', $this->version); } /** * Answers the source URL including the desired version. * * @return string */ public function getSourceURL() { return str_replace('{version}', $this->getSourceVersion(), $this->getSource()); } /** * Answers the source version. * * @return string */ public function getSourceVersion() { return preg_match('/^[0-9.]+$/', $this->version) ? sprintf('v%s', $this->version) : $this->version; } /** * Answers the icon data from the source URL. * * @return string */ public function getSourceData() { return file_get_contents($this->getSourceURL()); } } |