serlo-org / athene2-versioning
为 Athene2 提供版本组件的 Zend Framework 2 模块
Requires
- php: >=5.4
- doctrine/common: ~2.4
- doctrine/doctrine-module: ~0.8
- doctrine/doctrine-orm-module: ~0.8
- serlo-org/athene2-common: dev-master
- zendframework/zend-authentication: ~2.3
- zendframework/zend-eventmanager: ~2.3
- zendframework/zend-filter: ~2.3
- zendframework/zend-form: ~2.3
- zendframework/zend-mvc: ~2.3
- zendframework/zend-paginator: ~2.3
- zendframework/zend-servicemanager: ~2.3
- zendframework/zend-stdlib: ~2.3
- zendframework/zend-validator: ~2.3
- zf-commons/zfc-rbac: 2.3
Requires (Dev)
- phpunit/phpunit: ~3.7
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: 1.4.*
- zendframework/zendframework: ~2.2
This package is not auto-updated.
Last update: 2020-03-02 04:28:28 UTC
README
安装
athene2-versioning 仅通过 Composer 官方支持安装。有关 Composer 文档,请参阅 getcomposer.org。
安装模块
$ php composer.phar require serlo-org/athene2-versioning:~2.0
使用版本模块
版本模块使您能够管理包含修订版本的仓库。每个仓库有 n 个修订版本和一个或零个 HEAD 修订版本。HEAD 修订版本是该仓库的当前修订版本。版本管理器的默认实现是 Doctrine 友好!
功能
- Doctrine 实现(使用 ObjectManager)
- 与 zfc-rbac 捆绑以实现授权
- 事件
了解其工作原理
版本模块由一个 Versioning\Manager\VersioningManager
组成,该模块实现了 Versioning\Manager\VersioningManagerInterface
。它管理实现 Versioning\Entity\RepositoryInterface
和 Versioning\Entity\RevisionInterface
的模型或实体。
让我们实现这些实体接口!
您可以在这里找到示例实现!
RevisionInterface
<?php use Athene2\Versioning\Entity\RepositoryInterface; use Athene2\Versioning\Entity\RevisionInterface; use ZfcRbac\Identity\IdentityInterface; /** * Class Revision * * @author Aeneas Rekkas */ class Revision implements RevisionInterface { /** * @var mixed */ protected $id; /** * @var RepositoryInterface */ protected $repository; /** * @var */ protected $author; /** * @var bool */ protected $trashed = false; /** * @var array */ protected $data = []; /** * @param mixed $id * @return void */ public function setId($id) { $this->id = $id; } /** * {@inheritDoc} */ public function getId() { return $this->id; } /** * {@inheritDoc} */ public function getRepository() { return $this->repository; } /** * {@inheritDoc} */ public function setRepository(RepositoryInterface $repository) { $this->repository = $repository; } /** * {@inheritDoc} */ public function setAuthor(IdentityInterface $author) { $this->author = $author; } /** * {@inheritDoc} */ public function getAuthor() { return $this->author; } /** * {@inheritDoc} */ public function setTrashed($trash) { $this->trashed = (bool)$trash; } /** * {@inheritDoc} */ public function isTrashed() { return $this->trashed; } /** * {@inheritDoc} */ public function set($key, $value) { $this->data[$key] = $value; } /** * {@inheritDoc} */ public function get($key) { return isset($this->data[$key]) ? $this->data[$key] : null; } }
RepositoryInterface
<?php use Athene2\Versioning\Entity\RepositoryInterface; use Athene2\Versioning\Entity\RevisionInterface; class Repository implements RepositoryInterface { /** * @var array|RevisionInterface[] */ protected $revisions = []; /** * @var null|RevisionInterface */ protected $head = null; /** * @var mixed */ protected $id; /** * {@inheritDoc} */ public function addRevision(RevisionInterface $revision) { $this->revisions[$revision->getId()] = $revision; } /** * {@inheritDoc} */ public function createRevision() { return new Revision(); } /** * {@inheritDoc} */ public function getCurrentRevision() { return $this->head; } /** * {@inheritDoc} */ public function getId() { return $this->id; } /** * {@inheritDoc} */ public function getRevisions() { return $this->revisions; } /** * {@inheritDoc} */ public function hasCurrentRevision() { return null !== $this->head; } /** * {@inheritDoc} */ public function removeRevision(RevisionInterface $revision) { unset($this->revisions[$revision->getId()]); } /** * {@inheritDoc} */ public function setCurrentRevision(RevisionInterface $revision) { $this->head = $revision; } }
嗯,这并不难,对吧?
使用 RepositoryManager
默认的 RepositoryManager
实现捆绑了 Doctrine、ZF2 EventManager 和 zfc-rbac。
设置权限
不应允许每个人都提交、拒绝和接受修订版本,对吧?因此,RepositoryManager
可以通过 zfc-rbac 处理权限!
要设置权限,您需要在您的 module.config.php 中添加一些配置数据。
return [ // ... 'versioning' => [ 'permissions' => [ // Use the classname of the revision class // In the example above the namespace is missing, therefore the classname is only "Revision". // This could be also "MyModule\Entity\Revision" 'Revision' => [ // There are three actions which need authentication: // 'commit' gets checked when you call "commitRevision" ModuleOptions::KEY_PERMISSION_COMMIT => 'revision.create', // 'checkout' gets checked when you call "checkoutRevision" ModuleOptions::KEY_PERMISSION_CHECKOUT => 'revision.checkout', // 'reject' gets checked when you call "rejectRevision" ModuleOptions::KEY_PERMISSION_REJECT => 'revision.trash' // Name the permissions whatever you like. Just be aware that they are registered in zfc-rbac! // ModuleOptions::KEY_PERMISSION_COMMIT => 'mymodule.entity.revision.commit', ] ] ] // ... ];
重要:修订版本始终作为上下文对象传递给 zfc-rbac,以用于例如断言!
创建一个新的修订版本并填充数据!
// Let's create a repository first $repository = new Repository(); // Now we need the RepositoryManager $repositoryManager = $serviceManager->get('Athene2\Versioning\Manager\VersioningManager'); // Let's create our first revision! $revision = $repositoryManager->commitRevision($repository, ['foo' => 'bar'], 'I added some stuff'); // And check it out (set as HEAD / current revision) // We can also add a short message, why we checked out this revision! $repositoryManager->checkoutRevision($repository, $revision, 'That\'s a nice reason, isn\'t it?'); // Now, let's make those changes persistent! $repositoryManager->flush();
删除修订版本
有人犯了一个错误?只需拒绝修订版本即可!
// Now we need the RepositoryManager $repositoryManager = $serviceManager->get('Athene2\Versioning\Manager\VersioningManager'); $revision = $repositoryManager->rejectRevision($repository, 5, 'Sorry but there are too many mistakes!'); // Now, let's make those changes persistent! $repositoryManager->flush();
检出修订版本
你同意某项修订吗?快来查看一下吧!
// Now we need the RepositoryManager $repositoryManager = $serviceManager->get('Athene2\Versioning\Manager\VersioningManager'); $revision = $repositoryManager->checkoutRevision($repository, 5, 'Fine job!'); // Now, let's make those changes persistent! $repositoryManager->flush();
事件挂钩
VersioningManager 在失败和成功时都会触发事件
$eventManager = $repositoryManager->getEventManager(); $eventManager->attach(VersioningEvent::COMMIT, function(VersioningEvent $event) { echo "I just committed a new revision with a cool message: " . $event->getMessage(); }); $eventManager->attach(VersioningEvent::COMMIT_UNAUTHORIZED, function(VersioningEvent $event) { echo "I just committed a new revision but didn't have the rights to do so!"; }); $repositoryManager->commitRevision($repository, $data, $message);
还有其他可用的事件,例如
VersioningEvent::COMMIT
和VersioningEvent::COMMIT_UNAUTHORIZED
VersioningEvent::REJECT
和VersioningEvent::REJECT_UNAUTHORIZED
VersioningEvent::CHECKOUT
和VersioningEvent::CHECKOUT_UNAUTHORIZED
待完成
还有更多的事情要做
- 一个美观的UI来管理你的仓库
- RESTful API
- 更好的文档