Source of file SVGTemplate.php
Size: 4,681 Bytes - Last Modified: 2021-12-23T10:38:16+00:00
/var/www/docs.ssmods.com/process/src/src/SVGTemplate.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 | <?php namespace StevieMayhew\SilverStripeSVG; use DOMDocument; use SilverStripe\View\ViewableData; /** * Class SVGTemplate */ class SVGTemplate extends ViewableData { /** * The base path to your SVG location * * @config * @var string */ private static $base_path = 'mysite/svg/'; /** * @config * @var string */ private static $extension = 'svg'; /** * @config * @var array */ private static $default_extra_classes = array(); /** * @var string */ private $path; /** * @var string */ private $fill; /** * @var string */ private $stroke; /** * @var string */ private $width; /** * @var string */ private $height; /** * @var string */ private $custom_base_path; /** * @var array */ private $extraClasses; /** * @var array */ private $subfolders; /** * @param string $name * @param string $id */ public function __construct($name, $id = '') { $this->name = $name; $this->id = $id; $this->extra_classes = $this->stat('default_extra_classes'); $this->extra_classes[] = 'svg-'.$this->name; $this->subfolders = array(); $this->out = new DOMDocument(); $this->out->formatOutput = true; } /** * @param $color * @return $this */ public function fill($color) { $this->fill = $color; return $this; } /** * @param $color * @return $this */ public function stroke($color) { $this->stroke = $color; return $this; } /** * @param $width * @return $this */ public function width($width) { $this->width = $width; return $this; } /** * @param $height * @return $this */ public function height($height) { $this->height = $height; return $this; } /** * @param $width * @param $height * @return $this */ public function size($width, $height) { $this->width($width); $this->height($height); return $this; } /** * @param $class * @return $this */ public function customBasePath($path) { $this->custom_base_path = trim($path, DIRECTORY_SEPARATOR); return $this; } /** * @param $class * @return $this */ public function extraClass($class) { $this->extra_classes[] = $class; return $this; } /** * @param $class * @return $this */ public function addSubfolder($folder) { $this->subfolders[] = trim($folder, DIRECTORY_SEPARATOR); return $this; } /** * @param $filePath * @return string */ private function process($filePath) { if (!file_exists($filePath)) { return false; } $out = new DOMDocument(); $out->load($filePath); if (!is_object($out) || !is_object($out->documentElement)) { return false; } $root = $out->documentElement; if ($this->fill) { $root->setAttribute('fill', $this->fill); } if ($this->stroke) { $root->setAttribute('stroke', $this->stroke); } if ($this->width) { $root->setAttribute('width', $this->width); } if ($this->height) { $root->setAttribute('height', $this->height); } if ($this->extra_classes) { $root->setAttribute('class', implode(' ', $this->extra_classes)); } foreach ($out->getElementsByTagName('svg') as $element) { if ($this->id) { $element->setAttribute('id', $this->id); } else { if ($element->hasAttribute('id')) { $element->removeAttribute('id'); } } } $out->normalizeDocument(); return $out->saveHTML(); } /** * @return string */ public function forTemplate() { $path = BASE_PATH . DIRECTORY_SEPARATOR; $path .= ($this->custom_base_path) ? $this->custom_base_path : $this->stat('base_path'); $path .= DIRECTORY_SEPARATOR; foreach($this->subfolders as $subfolder) { $path .= $subfolder . DIRECTORY_SEPARATOR; } $path .= (strpos($this->name, ".") === false) ? $this->name . '.' . $this->stat('extension') : $this->name; return $this->process($path); } } |