Skip to content

Commit

Permalink
Added automatic activity watching
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Feb 9, 2024
1 parent a4fe18d commit 1c82586
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Chronicler.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function isRecorded(Record $record, string $period = null): bool
}


public function createRecord(string $event, string $message, iterable $params = []): RecordBuilder
public function createRecord(string $event, string $message, array $params = []): RecordBuilder
{
return (new RecordBuilder($this))
->withMessage($message)
Expand Down
11 changes: 10 additions & 1 deletion src/DI/NestorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace JuniWalk\Nestor\DI;

use JuniWalk\Nestor\Chronicler;
use JuniWalk\Nestor\Entity\Subscribers\ActivitySubscriber;
use Nette\DI\CompilerExtension;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
Expand All @@ -18,6 +19,7 @@ public function getConfigSchema(): Schema
{
return Expect::structure([
'entityName' => Expect::string()->required(),
'watchActivity' => Expect::bool(false),
]);
}

Expand All @@ -31,6 +33,13 @@ public function loadConfiguration()
$config = $this->getConfig();

$builder->addDefinition($this->prefix('chronicler'))
->setFactory(Chronicler::class, (array) $config);
->setFactory(Chronicler::class, [$config->entityName]);

if (!$config->watchActivity) {
return;
}

$builder->addDefinition($this->prefix('activitySubscriber'))
->setFactory(ActivitySubscriber::class);
}
}
37 changes: 37 additions & 0 deletions src/Entity/Attributes/ActivityOverride.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2024
* @license MIT License
*/

namespace JuniWalk\Nestor\Entity\Attributes;

use Attribute;
use JuniWalk\Nestor\Enums\Strategy;

#[Attribute(Attribute::TARGET_PROPERTY)]
class ActivityOverride
{
public function __construct(
private readonly Strategy $strategy,
private readonly mixed $value = null,
) {
}


public function process(array $changes, string $fieldName): array
{
switch ($this->strategy) {
case Strategy::Conceal:
$changes[$fieldName] = $this->value;
break;

case Strategy::Ignore:
unset($changes[$fieldName]);
break;
}

return $changes;
}
}
15 changes: 15 additions & 0 deletions src/Entity/Attributes/TargetIgnore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2024
* @license MIT License
*/

namespace JuniWalk\Nestor\Entity\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class TargetIgnore
{
}
58 changes: 45 additions & 13 deletions src/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,55 @@
namespace JuniWalk\Nestor\Entity;

use DateTime;
use Doctrine\ORM\EntityManagerInterface as EntityManager;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Proxy\Proxy;
use JuniWalk\Nestor\Enums\Type;
use JuniWalk\Utils\Arrays;
use JuniWalk\Utils\Enums\Color;
use JuniWalk\Utils\Format;
use JuniWalk\Utils\Json;
use Nette\Application\UI\Control;
use Nette\Localization\Translator;

#[ORM\MappedSuperclass]
abstract class Record
{
use Tools\Identifier;
use Tools\Ownership;

#[ORM\Column(type: 'string', length: 16, enumType: Type::class)]
protected Type $type = Type::Log;
private Type $type = Type::Log;

#[ORM\Column(type: 'string', length: 64)]
protected string $event;
private string $event;

#[ORM\Column(type: 'string')]
protected string $message;
private string $message;

#[ORM\Column(type: 'string', nullable: true, options: ['default' => null])]
protected ?string $target = null;
private ?string $target = null;

#[ORM\Column(type: 'integer', nullable: true, options: ['default' => null])]
protected ?int $targetId = null;
private ?int $targetId = null;

#[ORM\Column(type: 'datetimetz')]
protected DateTime $date;
private DateTime $date;

#[ORM\Column(type: 'string', length: 16, enumType: Color::class)]
protected Color $level = Color::Secondary;
private Color $level = Color::Secondary;

#[ORM\Column(type: 'boolean')]
protected bool $isFinished = false;
private bool $isFinished = false;

#[ORM\Column(type: 'json', nullable: true)]
protected ?array $params = null;
private ?array $params = null;

#[ORM\Column(type: 'text', nullable: true)]
protected ?string $note = null;
private ?string $note = null;

#[ORM\Column(type: 'string', length: 8, nullable: true)]
protected ?string $hash = null;
private ?string $hash = null;


final public function __construct(string $event, string $message)
Expand Down Expand Up @@ -119,10 +125,20 @@ public function getMessageFormatted(): string
}


public function getMessageTranslated(Translator $translator): string
{
return $translator->translate($this->message, Arrays::flatten($this->params));
}


public function setTarget(object $target, ?int $targetId = null): void
{
$this->targetId = $target->getId() ?? $targetId;
if (method_exists($target, 'getId')) {
$targetId ??= $target->getId();
}

$this->target = $target::class;
$this->targetId = $targetId;

if ($target instanceof Proxy) {
$this->target = get_parent_class($target);
Expand All @@ -142,6 +158,12 @@ public function getTargetId(): ?int
}


public function createTarget(EntityManager $entityManager): Proxy
{
return $entityManager->getReference($this->target, $this->targetId);
}


public function setDate(DateTime $date): void
{
$this->date = clone $date;
Expand Down Expand Up @@ -237,8 +259,18 @@ public function getHash(): string
}


abstract public function createLink(Control $control): ?string


protected function createUniqueHash(): string
{
return substr(sha1((string) $this), 0, 8);
return $this->hash ??= substr(sha1((string) $this), 0, 8);
}


#[ORM\PreFlush]
public function onPreFlush(PreFlushEventArgs $event): void
{
$this->createUniqueHash();
}
}
Loading

0 comments on commit 1c82586

Please sign in to comment.