lotgd / core

Core functionality for Legend of the Green Dragon, a text-based RPG game.
GNU Affero General Public License v3.0
152 stars 15 forks source link

Feature/extendable models #107

Closed Vassyli closed 6 years ago

Vassyli commented 6 years ago

This branch contains the code to make models GameAware by making use of doctrine's postLoad event. It combines an interface (GameAwareInterface) with a trait (GameAware). Models implementing GameAwareInterface automatically get access to the public method setGame and getGame, both potentially needed for raising events within properties.

Furthermore, by combining a lotgd.yml configuration and annotations, this patch allows the easy runtime-modification of models by using the __call magic function. Example:

lotgd.yml:

modelExtensions:
  - "LotGD\\Core\\Tests\\FakeModule\\Models\\UserTestExtension"

UserTestExtension:

/**
 * @Extension(of="LotGD\Core\Tests\FakeModule\Models\UserEntity")
 */
class UserTestExtension
{
    /**
     * @ExtensionMethod(as="getNameAsArray")
     */
    public static function returnNameAsArrayForUser(UserEntity $user): array
    {
        $g = $user->getGame();

        if ($g !== null) {
            return [$user->getName()];
        } else {
            return [];
        }
    }
}

This allows me to write $userEntity->getNameAsArray();. Extentable models must implement the ExtendableModelInterface and are advised to use the ExtendableModel trait.