-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sabina Talipova
committed
Dec 7, 2023
1 parent
7689bc4
commit 37769f9
Showing
23 changed files
with
427 additions
and
358 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
/* global document */ | ||
/* eslint-disable */ | ||
import registerComponents from './registerComponents'; | ||
import registerQueries from './registerQueries'; | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
registerComponents(); | ||
registerQueries(); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Services; | ||
|
||
use InvalidArgumentException; | ||
use SilverStripe\Core\ClassInfo; | ||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\LinkField\Models\Link; | ||
|
||
class LinkTypeService | ||
{ | ||
use Injectable; | ||
|
||
/** | ||
* Generate all link types that are subclasses of Link::class | ||
*/ | ||
public static function generateAllLinkTypes(): array | ||
{ | ||
$typeDefinitions = ClassInfo::subclassesFor(Link::class); | ||
|
||
$result = array(); | ||
foreach ($typeDefinitions as $class) { | ||
if (is_subclass_of($class, Link::class)) { | ||
$type = Injector::inst()->get($class)->getShortCode(); | ||
$result[$type] = $class; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Return a Link instance by key | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function byKey(string $key): ?Link | ||
{ | ||
$typeDefinitions = static::generateAllLinkTypes(); | ||
$definition = $typeDefinitions[$key] ?? null; | ||
|
||
if (!$definition) { | ||
return null; | ||
} | ||
|
||
return static::definitionToType($definition); | ||
} | ||
|
||
/** | ||
* Return a key for link type by classname | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function keyByClassName(string $classname): ?string | ||
{ | ||
$typeDefinitions = static::generateAllLinkTypes(); | ||
|
||
foreach ($typeDefinitions as $key => $class) { | ||
if ($class === $classname) { | ||
return $key; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function definitionToType(string $className): Link | ||
{ | ||
if (!$className && !($className instanceof Link)) { | ||
throw new InvalidArgumentException( | ||
_t( | ||
'LinkField.NO_CLASSNAME', | ||
'"{class}": All types should reference a valid classname', | ||
['class' => static::class], | ||
sprintf('%s: All types should reference a valid classname', static::class), | ||
), | ||
); | ||
} | ||
|
||
/** @var Link $type */ | ||
$type = Injector::inst()->get($className); | ||
|
||
if (!$type instanceof Link) { | ||
throw new InvalidArgumentException( | ||
_t( | ||
'LinkField.INVALID_TYPENAME', | ||
'"{class}": {typename} is not a valid link type', | ||
[ | ||
'class' => static::class, | ||
'typename' => $className, | ||
], | ||
sprintf('%s: %s is not a valid link type', static::class, $className), | ||
), | ||
); | ||
} | ||
|
||
return $type; | ||
} | ||
} |
Oops, something went wrong.