Skip to content

Commit

Permalink
Asset::getContents() + support assets from the svg() Twig function
Browse files Browse the repository at this point in the history
resolves #2838
  • Loading branch information
brandonkelly committed May 3, 2018
1 parent eb4ec1c commit 99bd7d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

## Unreleased

### Added
- Added `craft\elements\Asset::getContents()`.

### Changed
- Edit User pages will now warn editors when leaving the page with unsaved changes. ([#2832](https://github.com/craftcms/cms/issues/2832))
- Modules are once again loaded before plugins, so they have a chance to register Twig initialization events before a plugin initializes Twig. ([#2831](https://github.com/craftcms/cms/issues/2831))
- `craft\helpers\FileHelper::isSvg()` now returns `true` for files with an `image/svg` MIME type (missing the `+xml`). ([#2837](https://github.com/craftcms/cms/pull/2837))
- The `svg()` Twig function now accepts assets to be passed directly into it. ([#2838](https://github.com/craftcms/cms/pull/2838))

### Fixed
- Fixed an error that could occur in the Plugin Store.
Expand Down
17 changes: 16 additions & 1 deletion src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use craft\elements\actions\ReplaceFile;
use craft\elements\db\AssetQuery;
use craft\elements\db\ElementQueryInterface;
use craft\errors\AssetException;
use craft\errors\AssetTransformException;
use craft\errors\FileException;
use craft\errors\VolumeObjectNotFoundException;
Expand Down Expand Up @@ -893,15 +894,29 @@ public function getCopyOfFile(): string
}

/**
* Get a stream of the actual file.
* Returns a stream of the actual file.
*
* @return resource
* @throws InvalidConfigException if [[volumeId]] is missing or invalid
* @throws AssetException if a stream could not be created
*/
public function getStream()
{
return $this->getVolume()->getFileStream($this->getPath());
}

/**
* Returns the file’s contents.
*
* @return string
* @throws InvalidConfigException if [[volumeId]] is missing or invalid
* @throws AssetException if a stream could not be created
*/
public function getContents(): string
{
return stream_get_contents($this->getStream());
}

/**
* Return whether the Asset has a URL.
*
Expand Down
18 changes: 14 additions & 4 deletions src/web/twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\base\MissingComponentInterface;
use craft\elements\Asset;
use craft\elements\db\ElementQuery;
use craft\helpers\ArrayHelper;
use craft\helpers\DateTimeHelper;
Expand Down Expand Up @@ -806,16 +807,25 @@ public function shuffleFunction($arr)
/**
* Returns the (sanitized) contents of a given SVG file, namespacing any of its IDs in the process.
*
* @param string $svg The SVG file path or contents
* @param string|Asset $svg An SVG asset, a file path, or XML data
* @param bool $sanitize Whether the file should be sanitized first
* @return \Twig_Markup|string
*/
public function svgFunction(string $svg, bool $sanitize = true)
public function svgFunction($svg, bool $sanitize = true)
{
// If we can't find an <svg> tag, it's probably a file path
if (stripos($svg, '<svg') === false) {
if ($svg instanceof Asset) {
try {
$svg = $svg->getContents();
} catch (\Throwable $e) {
Craft::error("Could not get the contents of {$svg->getPath()}: {$e->getMessage()}", __METHOD__);
Craft::$app->getErrorHandler()->logException($e);
return '';
}
} else if (stripos($svg, '<svg') === false) {
// No <svg> tag, so it's probably a file path
$svg = Craft::getAlias($svg);
if (!is_file($svg) || !FileHelper::isSvg($svg)) {
Craft::warning("Could not get the contents of {$svg}: The file doesn't exist", __METHOD__);
return '';
}
$svg = file_get_contents($svg);
Expand Down

0 comments on commit 99bd7d7

Please sign in to comment.