PHP code example of hostnet / entity-revision-component

1. Go to this page and download the library: Download hostnet/entity-revision-component 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/ */

    

hostnet / entity-revision-component example snippets


use Acme\Bundle\AcmeBundle\AcmeRevisionFactory;
use Hostnet\Component\EntityRevision\Resolver\RevisionResolver;
use Hostnet\Component\EntityTracker\Listener\EntityChangedListener;
use Hostnet\Component\EntityTracker\Provider\EntityMetadataProvider;
use Hostnet\Component\EntityTracker\Provider\EntityMutationMetadataProvider;

/* @var $em \Doctrine\ORM\EntityManager */
$event_manager = $em->getEventManager();

// setup r,
    $logger
);

// the resolver is used to find the correct attribute
$revision_resolver = new RevisionResolver($metadata_provider);

// this factory will provide the revision and as author Henk
$revision_factory = new AcmeRevisionFactory('Henk');

// creating the revision listener
$revision_listener = new RevisionListener($revision_resolver, $revision_factory);

$event_manager->addEventListener('preFlush', $entity_changed_listener);
$event_manager->addEventListener('postFlush', $revision_listener);
$event_manager->addEventListener('entityChanged', $revision_listener);



use Doctrine\ORM\Mapping as ORM;
use Hostnet\Component\EntityRevision\RevisionInterface;

#[ORM\Entity]
class Revision implements RevisionInterface
{
    #[ORM\...]
    private ?string $author;

    #[ORM\...]
    private \DateTimeInterface $created_at;

    public function __construct(?string $author, \DateTimeInterface $created_at)
    {
        $this->author     = $author;
        $this->created_at = $created_at;
    }

    public function getUser(): ?string
    {
        return $this->author;
    }

    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->created_at;
    }
}



use Doctrine\ORM\Mapping as ORM;
use Hostnet\Component\EntityRevision\Attributes\Revision as RevisionAttribute;
use Hostnet\Component\EntityRevision\RevisionableInterface;
use Hostnet\Component\EntityRevision\RevisionInterface;

#[ORM\Entity]
#[RevisionAttribute]
class MyEntity implements RevisionableInterface
{
    /**
     * The current revision of the contact person
     */
    #[ORM\ManyToOne(targetEntity: Revision::class)]
    #[ORM\JoinColumn(name: 'revision_id', referencedColumnName: 'id')]
    private ?RevisionInterface $revision = null;

    public function setRevision(RevisionInterface $revision): static
    {
        $this->revision = $revision;

        return $this;
    }

    public function getRevision(): ?RevisionInterface
    {
        return $this->revision;
    }
}



namespace Acme\Bundle\AcmeBundle;

use Hostnet\Component\EntityRevision\Factory\RevisionFactoryInterface;
use Hostnet\Component\EntityRevision\RevisionInterface;

class AcmeRevisionFactory implements RevisionFactoryInterface
{
    public function __construct(private string $author)
    {
    }

    public function createRevision(\DateTime $created_at): RevisionInterface
    {
        return new Revision($this->author, $created_at);
    }
}


// imagine all instances to be Revisionable
$user->setName('Henk');
$address->setNumber('42');
$em->flush();
var_dump($user->getRevision() === $address->getRevision()); // true