Source of file ReferenceManager.php
Size: 2,557 Bytes - Last Modified: 2022-02-21T10:00:44+00:00
/var/www/docs.ssmods.com/process/src/src/ReferenceManager.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 | <?php declare(strict_types=1); namespace AdrHumphreys\Fixtures; use SilverStripe\Assets\File; use SilverStripe\Assets\Image; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataObject; class ReferenceManager { use Injectable; /** * @var array */ private $references = []; public static function addReference(string $identifier, object $reference): void { self::singleton()->references[$identifier] = $reference; } 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 = [], string $className = Image::class ): File { $existingAsset = self::getByReference($identifier); if ($existingAsset instanceof $className) { return $existingAsset; } $asset = Injector::inst()->create($className); $asset->setFromLocalFile($path); foreach ($params as $param => $value) { $asset->$param = $value; } $asset->write(); $asset->publishRecursive(); self::addReference($identifier, $asset); return $asset; } /* * Helper to find or make an objects easily * This will write the object but not publish it * * Expected usage: * ReferenceManager::findOrMakeDataObject( * 'my-cool-asset', * SilverStripe\Assets\Image::class, * ['title' => 'My cool asset'] * ); */ public static function findOrMakeDataObject( string $identifier, string $className, array $params ): ?object { $currentRef = self::getByReference($identifier); if ($currentRef !== null) { return $currentRef; } $object = Injector::inst()->create($className); foreach ($params as $key => $param) { $object->$key = $param; } $object->write(); self::addReference($identifier, $object); return $object; } } |