Skip to content

Commit

Permalink
Merge pull request #23 from wilr/master
Browse files Browse the repository at this point in the history
fix: resolve regression with customBasePath (#22)
  • Loading branch information
stevie-mayhew authored Jun 26, 2024
2 parents 54ecfc2 + d8ced26 commit fbcd588
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 14 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

jobs:
ci:
name: CI
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
with:
dynamic_matrix: false
extra_jobs: |
- php: '8.1'
db: mysql80
phpunit: true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/public
/vendor
composer.lock
.phpunit.result.cache
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
"require": {
"silverstripe/framework": "^4.0 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^9"
},
"scripts": {
"test": "phpunit"
},
"autoload": {
"psr-4": {
"StevieMayhew\\SilverStripeSVG\\": "src/"
"StevieMayhew\\SilverStripeSVG\\": "src/",
"StevieMayhew\\SilverStripeSVG\\Tests\\": "tests/"
}
},
"config": {
Expand Down
10 changes: 10 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
</rule>
</ruleset>
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
<coverage cacheDirectory="./"
includeUncoveredFiles="true"
processUncoveredFiles="true"
ignoreDeprecatedCodeUnits="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
43 changes: 30 additions & 13 deletions src/SVGTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ class SVGTemplate extends ViewableData
/**
* @var string
*/
private $custom_base_path;
private $customBasePath;

/**
* @var array
*/
private $extra_classes = [];
private $extraClasses = [];

/**
* @var array
Expand All @@ -89,9 +89,9 @@ public function __construct($name, $id = '')
{
$this->name = $name;
$this->id = $id;
$this->extra_classes = $this->config()->get('default_extra_classes') ?: [];
$this->extra_classes[] = 'svg-' . $this->name;
$this->subfolders = array();
$this->extraClasses = $this->config()->get('default_extra_classes') ?: [];
$this->extraClasses[] = 'svg-' . $this->name;
$this->subfolders = [];
$this->out = new DOMDocument();
$this->out->formatOutput = true;
}
Expand Down Expand Up @@ -154,7 +154,8 @@ public function size($width, $height)
*/
public function customBasePath($path)
{
$this->custom_base_path = trim($path, DIRECTORY_SEPARATOR);
$this->customBasePath = trim($path, DIRECTORY_SEPARATOR);

return $this;
}

Expand All @@ -164,7 +165,8 @@ public function customBasePath($path)
*/
public function extraClass($class)
{
$this->extra_classes[] = $class;
$this->extraClasses[] = $class;

return $this;
}

Expand All @@ -175,13 +177,18 @@ public function extraClass($class)
public function addSubfolder($folder)
{
$this->subfolders[] = trim($folder, DIRECTORY_SEPARATOR);

return $this;
}


public function isRemoteSvg(): bool
{
if (strpos($this->name, 'https://') === 0 || strpos($this->name, 'http://') === 0 || strpos($this->name, 'data:image') === 0) {
if ((strpos($this->name, '://') !== false || strpos($this->name, '//') === 0)) {
return true;
}

if (strpos($this->name, 'data:image') === 0) {
return true;
}

Expand Down Expand Up @@ -240,8 +247,8 @@ private function process($filePath)
$root->setAttribute('height', $this->height);
}

if ($this->extra_classes) {
$root->setAttribute('class', implode(' ', $this->extra_classes));
if ($this->extraClasses) {
$root->setAttribute('class', implode(' ', $this->extraClasses));
}

foreach ($out->getElementsByTagName('svg') as $element) {
Expand Down Expand Up @@ -269,18 +276,28 @@ public function forTemplate()
return DBField::create_field('HTMLText', $this->process($this->name));
}

$path = $this->fullSvgPathForTemplate();

return DBField::create_field('HTMLText', $this->process($path));
}


public function fullSvgPathForTemplate()
{
$basePath = $this->customBasePath ?? $this->config()->get('base_path');
$parts = [
BASE_PATH,
$this->config()->get('base_path')
$basePath
];

foreach ($this->subfolders as $subfolder) {
$parts[] = $subfolder;
}

$parts[] = (strpos($this->name, ".") === false) ? $this->name . '.' . $this->config()->get('extension') : $this->name;
$extension = $this->config()->get('extension');
$parts[] = (strpos($this->name, ".") === false) ? $this->name . '.' . $extension : $this->name;
$path = Controller::join_links(array_filter($parts));

return DBField::create_field('HTMLText', $this->process($path));
return $path;
}
}
41 changes: 41 additions & 0 deletions tests/SVGTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace StevieMayhew\SilverStripeSVG\Tests;

use SilverStripe\Dev\SapphireTest;
use StevieMayhew\SilverStripeSVG\SVGTemplate;

class SVGTemplateTest extends SapphireTest
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

// Set the base path to the test SVGs
SVGTemplate::config()->set('base_path', 'tests');
}


public function testFullSvgPathForTemplate()
{
$svgTemplate = new SVGTemplate('logo.svg');
$this->assertStringEndsWith('logo.svg', $svgTemplate->fullSvgPathForTemplate());

$svgTemplate = $svgTemplate->customBasePath('custom');
$this->assertStringEndsWith('custom/logo.svg', $svgTemplate->fullSvgPathForTemplate());
}


public function testIsRemoteSvg()
{
$svgTemplate = new SVGTemplate('logo.svg');
$this->assertFalse($svgTemplate->isRemoteSvg());

$svgTemplate = new SVGTemplate('https://example.com/logo.svg');
$this->assertTrue($svgTemplate->isRemoteSvg());


$svgTemplate = new SVGTemplate('//example.com/logo.svg');
$this->assertTrue($svgTemplate->isRemoteSvg());
}
}
1 change: 1 addition & 0 deletions tests/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fbcd588

Please sign in to comment.