Skip to content

Commit

Permalink
Merge pull request #1 from blueo/pulls/allow-create-other-files
Browse files Browse the repository at this point in the history
allow creating other Assets with findOrMakeAsset
  • Loading branch information
adrhumphreys authored Nov 17, 2020
2 parents 9d51f70 + 7110810 commit a0df224
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ You can create assets really easily like so:
\AdrHumphreys\Fixtures\ReferenceManager::findOrMakeAsset('my-asset-id', 'file/path.jpg');
```

This will create the image, and store it with a reference of `my-asset-id`. You can also pass through as a third argument an array of params e.g. `['Title' => 'my asset title']` this will be translated to `$image->Title = 'my asset title'` so is case-sensitive. The function will also return the stored image.
This will create the image, and store it with a reference of `my-asset-id`. You can also pass through as a third argument an array of params e.g. `['Title' => 'my asset title']` this will be translated to `$image->Title = 'my asset title'` so is case-sensitive. The function will also return the stored image. The fourth argument allows you to specify another Asset type eg SilverStripe\Assets\File.

You can then access the asset through `$this->getByReference('my-asset-id')`

Expand Down
41 changes: 28 additions & 13 deletions src/ReferenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AdrHumphreys\Fixtures;

use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
Expand All @@ -28,30 +29,44 @@ public static function getByReference(string $identifier): ?object
return self::singleton()->references[$identifier] ?? null;
}

/*
* Helper to find or make an asset easily
* This will load a local file into an asset, write and publish it
* defaults to Image
*
* Expected usage:
* ReferenceManager::findOrMakeAsset(
* 'my-cool-asset',
* './app/mycoolfile.pdf',
* ['title' => 'My cool asset'],
* File::class
* );
*/
public static function findOrMakeAsset(
string $identifier,
string $path,
array $params = []
): Image {
$existingImage = self::getByReference($identifier);
array $params = [],
string $className = Image::class
): File {
$existingAsset = self::getByReference($identifier);

if ($existingImage instanceof Image) {
return $existingImage;
if ($existingAsset instanceof $className) {
return $existingAsset;
}

$image = Image::create();
$image->setFromLocalFile($path);
$asset = Injector::inst()->create($className);
$asset->setFromLocalFile($path);

foreach ($params as $param => $value) {
$image->$param = $value;
$asset->$param = $value;
}

$image->write();
$image->publishRecursive();
$asset->write();
$asset->publishRecursive();

self::addReference($identifier, $image);
self::addReference($identifier, $asset);

return $image;
return $asset;
}

/*
Expand All @@ -76,7 +91,7 @@ public static function findOrMakeDataObject(
return $currentRef;
}

$object = Injector::inst()->createWithArgs($className, []);
$object = Injector::inst()->create($className);

foreach ($params as $key => $param) {
$object->$key = $param;
Expand Down

0 comments on commit a0df224

Please sign in to comment.