devel_wizard-2.x-dev/src/Spell/SpellParamConverter.php
src/Spell/SpellParamConverter.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Spell;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Symfony\Component\Routing\Route;
/**
* Converts Spell ID into a Spell instance.
*
* @code
* # my_module_01.routing.yml
* my_module_01.route_01:
* path: '/my_module_01/foo/{spell}'
* options:
* parameters:
* spell:
* type: 'devel_wizard:spell'
* defaults: {}
* requirements: {}
* @endcode
*/
class SpellParamConverter implements ParamConverterInterface {
protected SpellManagerInterface $spellManager;
public function __construct(SpellManagerInterface $spellManager) {
$this->spellManager = $spellManager;
}
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route) {
return ($definition['type'] ?? NULL) === 'devel_wizard:spell';
}
/**
* {@inheritdoc}
*/
public function convert($value, $definition, $name, array $defaults) {
/* @noinspection PhpUnhandledExceptionInspection */
return $this->spellManager->hasDefinition($value) ?
$this->spellManager->createInstance($value)
: NULL;
}
}
