pierstoval / character-manager
游戏角色管理器的骨架
Requires
- php: >=8.1
- symfony/config: ^6.2|^7.1
- symfony/dependency-injection: ^6.2|^7.1
- symfony/framework-bundle: ^6.2|^7.1
- symfony/http-foundation: ^6.2|^7.1
- symfony/http-kernel: ^6.2|^7.1
- symfony/routing: ^6.2|^7.1
Requires (Dev)
- doctrine/annotations: ^1.13|^2.0
- doctrine/doctrine-bundle: ^1.12|^2.2
- doctrine/orm: ^2.8|^3.0
- symfony/browser-kit: ^6.2|^7.1
- symfony/console: ^6.2|^7.1
- symfony/css-selector: ^6.2|^7.1
- symfony/dom-crawler: ^6.2|^7.1
- symfony/phpunit-bridge: ^6.2|^7.1
- symfony/translation: ^6.2|^7.1
- symfony/twig-bundle: ^6.2|^7.1
- symfony/yaml: ^6.2|^7.1
Suggests
- doctrine/doctrine-bundle: To inject the object manager in steps and map your Character entities with the ORM
- doctrine/orm: To inject the object manager in steps and map your Character entities with the ORM
- symfony/translation: To translate the messages instead of using the default keys
- symfony/twig-bundle: To use Twig templating engine in your actions
README
角色管理器包
此包旨在提供可定制的骨架,用于根据代表角色创建每个步骤的动作列表创建角色。
您可以从配置和服务中配置您的步骤。
设置
-
安装包
如果您使用的是带有Flex的Symfony,您只需执行composer require pierstoval/character-manager
即可。
如果您没有Flex,请参考以下步骤。 -
将其添加到您的Kernel中
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // Sorry for the very long name. new Pierstoval\Bundle\CharacterManagerBundle\PierstovalCharacterManagerBundle(), ]; } }
-
创建一个
Character
类,用于您的第一个角色管理器<?php namespace App\Entity; use Pierstoval\Bundle\CharacterManagerBundle\Model\CharacterInterface; class Character implements CharacterInterface { // Implement interface methods }
-
提示:您还可以扩展抽象类
Pierstoval\Bundle\CharacterManagerBundle\Entity\Character
,该类已实现不同的方法,并且已经是一个Doctrine ORM实体,您只需添加自己的id
属性即可。 -
加载路由文件
generator_steps: resource: "@PierstovalCharacterManagerBundle/Resources/config/routing.xml" prefix: /character_generator/ # Or any prefix you like
此路由文件非常重要,因为它是处理角色生成的地方。
注意:当使用多个角色管理器时,您可以在URL前缀中添加{manager}
路由选项。 -
您的基础设置已经完成!
现在您必须创建您的步骤动作,以便您能够生成一个角色
角色生成
步骤动作
要生成角色,您需要所谓的步骤动作类。
一个生成步骤 = 一个类。
每个类都必须实现StepActionInterface
,但您也可以扩展实现接口并添加酷逻辑的抽象类AbstractStepAction
,这样您只需实现execute()
方法。
您可以定义它为一个简单的类,如下所示
pierstoval_character_manager: managers: default: character_class: 'App\Entity\Character' steps: step_01: action: App\Step\Step01
您也可以引用已存在的服务
pierstoval_character_manager: managers: default: character_class: 'App\Entity\Character' steps: step_01: action: app.steps.step_1 services: app.steps.step_1: class: App\Step\Step01 arguments: - ...
注意:您应该知道,所有未设置为服务的action classes
将被定义为服务,自动注入并设置为私有,因为这对于ActionRegistry
来说强制性的。
但是,您的服务将保持不变,以保持与您自己的逻辑的一致性。
💠 魔法
关于步骤的重要事项
步骤配置参考
pierstoval_character_manager:
managers:
# Prototype
name:
character_class: ~ # Required
steps:
# Prototype
name:
# Can be a class or a service. Must implement StepActionInterface or extend abstract Action class.
action: ~ # Required
label: ''
# Steps that the current step may depend on. If step is not set in session, will throw an exception.
dependencies: []
# When this step will be updated, it will clear values for specified steps.
# Only available for the abstract class
onchange_clear: []
- 步骤名称必须对每个角色管理器是唯一的。您可以在应用程序中引用它,所以请确保它足够详细,并且比“步骤1”,“步骤2”等更具有信息性。
- 步骤顺序很重要!步骤编号从1开始,如果您更改步骤的顺序,整个顺序都会改变。在使用
AbstractStepAction::goToStep($stepNumber)
方法时(见下文),请记住这一点。 - 仅在抽象类
AbstractStepAction
中处理onchange_clear
参数,但您可以在StepActionInterface::execute()
方法中手动实现它,例如。
AbstractStepAction
类
这是一个基本动作的示例
<?php namespace App\Step; use Pierstoval\Bundle\CharacterManagerBundle\Action\AbstractStepAction; use Symfony\Component\HttpFoundation\Response; class Step1 extends AbstractStepAction { /** * {@inheritdoc} */ public function execute(): Response { // Implement your step logic here. return new Response('This step is now rendered'); } }
注入到AbstractStepAction类中的内容
当您定义了所有角色管理器的配置后,StepsPass
将处理它们并执行某些操作
-
检查您的动作是否是扩展
StepActionInterface
的现有类。
如果存在且未定义为服务,则- 将其定义为服务
- 将服务设置为
private
、autowired
和lazy
。
如果它已经定义为服务,它将不会执行以下步骤之外的操作
-
对于所有操作,现在应该将它们定义为服务,以便编译器通过它们处理
- 如果它扩展了
AbstractStepAction
类,它也会注入- 如果可用,路由器(通过
RouterInterface
) - 如果可用,实体管理器(实际上它注入了
ObjectManager
,因此与ORM和ODM都兼容)。 - 翻译器(它应该已经通过
TranslatorInterface
可用,即使框架中未启用)。 - 如果可用,Twig环境。
- 如果可用,路由器(通过
- 将步骤配置注入
character_class
选项- 从
StepActionResolver
检索的Step
对象 - 从这个管理器检索的所有步骤,再次从
StepActionResolver
获取。它们主要用于在抽象动作类中管理goToStep()
和nextStep()
方法。 - 将动作添加到容器中注册的
ActionRegistryInterface
服务。
- 如果它扩展了
并且抽象类也有很酷的新方法。
构造函数
首先,你必须知道AbstractStepAction没有构造函数。
然后,你可以自由地拥有自己的构造函数,而不必依赖父类的逻辑,并且可以通过自动装配在构造函数中注入所有必要的服务和参数。
抽象类仅添加一些好用的东西(如果有人没有扩展它,请给我发消息,我想知道你为什么不想扩展它),并且这个酷逻辑位于其他方法中。
所以你可以自由地实现自己的构造函数,特别是如果你将动作定义为服务的话
注入的服务
如果你将步骤动作定义为服务并扩展抽象类,你将能够访问四个服务
/** @var EntityManager */ $this->em; /** @var Twig\Environment */ $this->twig; /** @var RouterInterface */ $this->router; /** @var TranslatorInterface */ $this->translator;
大多数时候,你不需要很多其他东西,但如果你需要其他东西,只需将arguments:
或calls:
选项添加到你的服务定义中。
AbstractStepAction类的酷方法
抽象类添加了一些管理步骤的酷方法
<?php /** @var $this \Pierstoval\Bundle\CharacterManagerBundle\Action\AbstractStepAction */ // Get the character property for specified step name (or current step by default) $this->getCharacterProperty($stepName = null); // Returns a RedirectResponse object to the next step $this->nextStep(); // Returns a RedirectResponse object to the specified step, but by number, not by name $this->goToStep($stepNumber); // A cool tool to use flash messages. By default, the "type" is "error". // All flash messages are translated and the translation domain is set in the StepAction::$translationDomain static property. // Of course you can override the translation domain if you create your own abstract StepAction class and override this var. $this->flashMessage($msg, $type = null, array $msgParams = []); // Updates the current character step value and sets it to $value // This is the method that makes sure that "onchange_clear" steps are cleared with a simple "unset()" in the session $this->updateCharacterStep($value);
路线图
“待办事项”列表
- 添加一个工厂、服务或其他东西,其目标将是将最终步骤提交转换为适当的
Character
对象。显然是抽象的,因为它必须由用户手动实现。 - 尝试找到一种方法来减少控制器类的大小
- 为不同的游戏创建大量的角色管理器,这样我们就可以找到这个包的缺陷并去除它们!(欢迎帮助 😁)
许可
该项目在MIT许可下发布。有关更多信息,请参阅许可文件。