Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Charcoal\Admin\Action\Object;

use Charcoal\Object\RevisionsManager;
use Exception;
use InvalidArgumentException;
// From PSR-7
use Pimple\Container;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
// From 'charcoal-object'
use Charcoal\Object\ObjectRevisionInterface;
use Charcoal\Object\RevisionableInterface;
// From 'charcoal-admin'
use Charcoal\Admin\AdminAction;
use Charcoal\Admin\Ui\ObjectContainerInterface;
Expand Down Expand Up @@ -45,6 +46,15 @@ class RevertRevisionAction extends AdminAction implements ObjectContainerInterfa
*/
protected $revNum;

private RevisionsManager $revisionService;

protected function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->revisionService = $container->get('revisions/manager');
}

/**
* Retrieve the list of parameters to extract from the HTTP request.
*
Expand All @@ -62,9 +72,9 @@ protected function validDataFromRequest()
/**
* Set the revision number to restore.
*
* @param integer $revNum The revision number to load.
* @throws InvalidArgumentException If the given revision is invalid.
* @param integer $revNum The revision number to load.
* @return ObjectContainerInterface Chainable
* @throws InvalidArgumentException If the given revision is invalid.
*/
protected function setRevNum($revNum)
{
Expand All @@ -91,8 +101,8 @@ public function revNum()
}

/**
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @param ResponseInterface $response A PSR-7 compatible Response instance.
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @param ResponseInterface $response A PSR-7 compatible Response instance.
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
Expand All @@ -107,19 +117,11 @@ public function run(RequestInterface $request, ResponseInterface $response)
'{{ errorMessage }}' => $failMessage
]);

$obj = $this->obj();
if (!($obj instanceof RevisionableInterface)) {
$this->setSuccess(false);

$this->addFeedback('error', strtr('{{ model }} does not support revisions', [
'{{ model }}' => $this->getSingularLabelFromObj($obj),
]));

return $response->withStatus(400);
}

$obj = $this->obj();
$revNum = $this->revNum();
$revision = $obj->revisionNum($revNum);
$this->revisionService->setModel($obj);

$revision = $this->revisionService->revisionFromNumber($revNum);
if (!$revision['id']) {
$this->setSuccess(false);

Expand All @@ -139,7 +141,7 @@ public function run(RequestInterface $request, ResponseInterface $response)
return $response->withStatus(404);
}

$result = $obj->revertToRevision($revNum);
$result = $this->revisionService->revertToRevision($revNum);

if ($result) {
$doneMessage = $translator->translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface ObjectRevisionsInterface
/**
* @return \Charcoal\Object\ObjectRevisionInterface[]
*/
public function objectRevisions();
public function objectRevisions(): array;
}
11 changes: 8 additions & 3 deletions packages/admin/src/Charcoal/Admin/Ui/ObjectRevisionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Charcoal\Factory\FactoryInterface;
// From 'charcoal-object'
use Charcoal\Object\ObjectRevisionInterface;
use Charcoal\Object\RevisionsManager;

/**
* An implementation, as Trait, of the {@see \Charcoal\Admin\Ui\ObjectRevisionsInterface}.
Expand All @@ -15,7 +16,7 @@ trait ObjectRevisionsTrait
/**
* @return ObjectRevisionInterface[]
*/
public function objectRevisions()
public function objectRevisions(): array
{
if (!$this->objType() || !$this->objId()) {
return [];
Expand All @@ -24,7 +25,9 @@ public function objectRevisions()
$obj = $this->modelFactory()->create($this->objType());
$obj->setId($this->objId());

$lastRevision = $obj->latestRevision();
$this->revisionService()->setModel($obj);

$lastRevision = $this->revisionService()->latestRevision();
$propLabel = '<span title="%1$s">%2$s</code>';

$callback = function (ObjectRevisionInterface &$revision) use ($lastRevision, $obj, $propLabel) {
Expand Down Expand Up @@ -67,7 +70,7 @@ public function objectRevisions()
$revision->allowRevert = ($lastRevision['revNum'] !== $revision['revNum']);
};

return $obj->allRevisions($callback);
return $this->revisionService()->allRevisions($callback);
}

/**
Expand All @@ -88,4 +91,6 @@ abstract public function objId();
* @return FactoryInterface
*/
abstract protected function modelFactory();

abstract protected function revisionService(): RevisionsManager;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Charcoal\Admin\Widget\FormGroup;

// From 'pimple/pimple'
use Charcoal\Object\RevisionsManager;
use Pimple\Container;
// From 'charcoal-core'
use Charcoal\Model\ModelFactoryTrait;
Expand Down Expand Up @@ -37,6 +38,12 @@ class ObjectRevisionsFormGroup extends AbstractFormGroup implements
*/
public $widgetId;

private RevisionsManager $revisionService;

protected function revisionService(): RevisionsManager
{
return $this->revisionService;
}

/**
* @param string $widgetId The widget identifier.
Expand Down Expand Up @@ -110,6 +117,7 @@ protected function setDependencies(Container $container)
parent::setDependencies($container);

$this->setModelFactory($container['model/factory']);
$this->revisionService = $container['revisions/manager'];

$this->objType = $container['request']->getParam('obj_type');
$this->objId = $container['request']->getParam('obj_id');
Expand Down
17 changes: 14 additions & 3 deletions packages/admin/src/Charcoal/Admin/Widget/FormSidebarWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Charcoal\Admin\Widget;

use Charcoal\Object\RevisionableInterface;
use Charcoal\Object\RevisionsManager;
use Charcoal\User\AuthAwareInterface;
use InvalidArgumentException;
// From Pimple
Expand Down Expand Up @@ -163,6 +163,8 @@ class FormSidebarWidget extends AdminWidget implements
*/
private $requiredGlobalAclPermissions = [];

private RevisionsManager $revisionService;

/**
* @param array|ArrayInterface $data Class data.
* @return FormSidebarWidget Chainable
Expand Down Expand Up @@ -552,8 +554,10 @@ public function isObjRevisionable()
return $this->isObjRevisionable;
}

if ($obj instanceof RevisionableInterface && $obj['revisionEnabled']) {
$this->isObjRevisionable = !!count($obj->allRevisions());
$this->revisionService->setModel($obj);

if ($this->revisionService->revisionEnabled()) {
$this->isObjRevisionable = !!count($this->revisionService->allRevisions());
}
}
}
Expand Down Expand Up @@ -894,4 +898,11 @@ protected function isAssoc(array $array)

return !!array_filter($array, 'is_string', ARRAY_FILTER_USE_KEY);
}

protected function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->revisionService = $container['revisions/manager'];
}
}
16 changes: 16 additions & 0 deletions packages/admin/src/Charcoal/Admin/Widget/ObjectRevisionsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Charcoal\Admin\AdminWidget;
use Charcoal\Admin\Ui\ObjectRevisionsInterface;
use Charcoal\Admin\Ui\ObjectRevisionsTrait;
use Charcoal\Object\RevisionsManager;
use Pimple\Container;

/**
* Class ObjectRevisionWidget
Expand All @@ -27,6 +29,20 @@ class ObjectRevisionsWidget extends AdminWidget implements
*/
protected $objId;

private RevisionsManager $revisionService;

protected function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->revisionService = $container['revisions/manager'];
}

protected function revisionService(): RevisionsManager
{
return $this->revisionService;
}

/**
* @return boolean
*/
Expand Down
1 change: 1 addition & 0 deletions packages/app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"charcoal/config": "^4.1",
"charcoal/event": "^3.2",
"charcoal/factory": "^4.1",
"charcoal/object": "^4.1",
"charcoal/translator": "^4.1",
"charcoal/view": "^4.1",
"monolog/monolog": "^1.17",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Charcoal\App\Template\TemplateInterface;
use Charcoal\App\Template\WidgetInterface;
use Charcoal\App\Template\WidgetBuilder;
use Charcoal\Object\RevisionServiceProvider;
use Charcoal\View\Twig\DebugHelpers as TwigDebugHelpers;
use Charcoal\View\Twig\HelpersInterface as TwigHelpersInterface;
use Charcoal\View\Twig\UrlHelpers as TwigUrlHelpers;
Expand Down Expand Up @@ -78,6 +79,7 @@ public function register(Container $container)
$container->register(new ScriptServiceProvider());
$container->register(new TranslatorServiceProvider());
$container->register(new ViewServiceProvider());
$container->register(new RevisionServiceProvider());

$this->registerKernelServices($container);
$this->registerHandlerServices($container);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"form": {
"type": "charcoal/admin/widget/object-form",
"form_ident": "default",
"target_type": "charcoal/object/object-revision"
"obj_type": "charcoal/object/object-revision"
}
},
"layout": {
Expand All @@ -57,7 +57,7 @@
"form": {
"type": "charcoal/admin/widget/table",
"collection_ident": "default",
"target_type": "charcoal/object/object-revision"
"obj_type": "charcoal/object/object-revision"
}
},
"layout": {
Expand Down
9 changes: 0 additions & 9 deletions packages/object/src/Charcoal/Object/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Charcoal\Object\ContentInterface;
use Charcoal\Object\AuthorableInterface;
use Charcoal\Object\AuthorableTrait;
use Charcoal\Object\RevisionableInterface;
use Charcoal\Object\RevisionableTrait;
use Charcoal\Object\TimestampableInterface;
use Charcoal\Object\TimestampableTrait;

Expand All @@ -26,11 +24,9 @@
class Content extends AbstractModel implements
AuthorableInterface,
ContentInterface,
RevisionableInterface,
TimestampableInterface
{
use AuthorableTrait;
use RevisionableTrait;
use TranslatorAwareTrait;
use TimestampableTrait;

Expand Down Expand Up @@ -197,11 +193,6 @@ protected function preUpdate(array $properties = null)
{
parent::preUpdate($properties);

// Content is revisionable
if ($this['revisionEnabled']) {
$this->generateRevision();
}

// Timestampable propertiees
$this->setLastModified('now');

Expand Down
30 changes: 30 additions & 0 deletions packages/object/src/Charcoal/Object/GenerateRevisionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Charcoal\Object;

use Charcoal\Event\AbstractEventListener;
use Charcoal\Model\ModelInterface;
use Pimple\Container;

/**
* Listener
*/
class GenerateRevisionListener extends AbstractEventListener
{
protected RevisionsManager $revisionService;

public function __invoke(object $event)
Comment thread
JoelAlphonso marked this conversation as resolved.
{
/** @var ModelInterface $model */
$model = $event->getObject();

$this->revisionService->setModel($model)->generateRevision();
}

public function setDependencies(Container $container)
{
parent::setDependencies($container);

$this->revisionService = $container->get('revisions/manager');
}
}
Loading