PHP code example of serlo-org / athene2-versioning
1. Go to this page and download the library: Download serlo-org/athene2-versioning library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
serlo-org / athene2-versioning example snippets
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;
}
}
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;
}
}
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',
]
]
]
// ...
];
// 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();
$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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.