Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added before load event for objects, documents and assets #20

Open
wants to merge 10 commits into
base: 11.x
Choose a base branch
from
10 changes: 10 additions & 0 deletions lib/Event/AssetEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ final class AssetEvents
*/
const POST_DELETE_FAILURE = 'pimcore.asset.postDeleteFailure';

/**
* Arguments:
* - params | array | contains the values that were passed to getById() as the second parameter
*
* @Event("Pimcore\Event\Model\AssetPreLoadEvent")
*
* @var string
*/
const PRE_LOAD = 'pimcore.asset.preLoad';

/**
* Arguments:
* - params | array | contains the values that were passed to getById() as the second parameter
Expand Down
10 changes: 10 additions & 0 deletions lib/Event/DataObjectEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ final class DataObjectEvents
*/
const POST_DELETE_FAILURE = 'pimcore.dataobject.postDeleteFailure';

/**
* Arguments:
* - params | array | contains the values that were passed to getById() as the second parameter
*
* @Event("Pimcore\Event\Model\DataObjectPreLoadEvent")
*
* @var string
*/
const PRE_LOAD = 'pimcore.dataobject.preLoad';

/**
* Arguments:
* - params | array | contains the values that were passed to getById() as the second parameter
Expand Down
10 changes: 10 additions & 0 deletions lib/Event/DocumentEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ final class DocumentEvents
*/
const POST_DELETE_FAILURE = 'pimcore.document.postDeleteFailure';

/**
* Arguments:
* - params | array | contains the values that were passed to getById() as the second parameter
*
* @Event("Pimcore\Event\Model\DocumentPreLoadEvent")
*
* @var string
*/
const PRE_LOAD = 'pimcore.document.preLoad';

/**
* Arguments:
* - params | array | contains the values that were passed to getById() as the second parameter
Expand Down
58 changes: 58 additions & 0 deletions lib/Event/Model/AssetPreLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Event\Model;

use Pimcore\Event\Traits\ArgumentsAwareTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Contracts\EventDispatcher\Event;

class AssetPreLoadEvent extends Event implements ElementEventInterface
{
use ArgumentsAwareTrait;

protected ?Asset $asset;

/**
* AssetEvent constructor.
*
* @param array $arguments additional parameters (e.g. "versionNote" for the version note)
*/
public function __construct(?Asset $asset, array $arguments = [])
{
$this->asset = $asset;
$this->arguments = $arguments;
}

public function getAsset(): Asset
{
if ($this->asset === null) {
throw new NotFoundException();
}
return $this->asset;
}

public function setAsset(?Asset $asset): void
{
$this->asset = $asset;
}

public function getElement(): Asset
{
return $this->getAsset();
}
}
57 changes: 57 additions & 0 deletions lib/Event/Model/DataObjectPreLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Event\Model;

use Pimcore\Event\Traits\ArgumentsAwareTrait;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Contracts\EventDispatcher\Event;

class DataObjectPreLoadEvent extends Event implements ElementEventInterface
{
use ArgumentsAwareTrait;

protected ?AbstractObject $object;

/**
* DataObjectEvent constructor.
*
*/
public function __construct(?AbstractObject $object, array $arguments = [])
{
$this->object = $object;
$this->arguments = $arguments;
}

public function getObject(): AbstractObject
{
if ($this->object === null) {
throw new NotFoundException();
}
return $this->object;
}

public function setObject(?AbstractObject $object): void
{
$this->object = $object;
}

public function getElement(): AbstractObject
{
return $this->getObject();
}
}
57 changes: 57 additions & 0 deletions lib/Event/Model/DocumentPreLoadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Event\Model;

use Pimcore\Event\Traits\ArgumentsAwareTrait;
use Pimcore\Model\Document;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Contracts\EventDispatcher\Event;

class DocumentPreLoadEvent extends Event implements ElementEventInterface
{
use ArgumentsAwareTrait;

protected ?Document $document;

/**
* DocumentEvent constructor.
*
*/
public function __construct(?Document $document, array $arguments = [])
{
$this->document = $document;
$this->arguments = $arguments;
}

public function getDocument(): Document
{
if ($this->document === null) {
throw new NotFoundException();
}
return $this->document;
}

public function setDocument(?Document $document): void
{
$this->document = $document;
}

public function getElement(): Document
{
return $this->getDocument();
}
}
15 changes: 14 additions & 1 deletion models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Pimcore\Event\AssetEvents;
use Pimcore\Event\FrontendEvents;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\AssetPreLoadEvent;
use Pimcore\File;
use Pimcore\Helper\TemporaryFileHelperTrait;
use Pimcore\Loader\ImplementationLoader\Exception\UnsupportedException;
Expand Down Expand Up @@ -247,6 +248,10 @@ public static function getById(int|string $id, array $params = []): ?static

try {
$asset->getDao()->getById($id);
// fire pre load event
$preLoadEvent = new AssetPreLoadEvent($asset, ['params' => $params]);
\Pimcore::getEventDispatcher()->dispatch($preLoadEvent, AssetEvents::PRE_LOAD);
$asset = $preLoadEvent->getAsset();

$className = \Pimcore::getContainer()->get('pimcore.class.resolver.asset')->resolve($asset->getType());
/** @var Asset $newAsset */
Expand All @@ -267,6 +272,14 @@ public static function getById(int|string $id, array $params = []): ?static
$asset = null;
}
} else {
try {
// fire pre load event
$preLoadEvent = new AssetPreLoadEvent($asset, ['params' => $params]);
\Pimcore::getEventDispatcher()->dispatch($preLoadEvent, AssetEvents::PRE_LOAD);
$asset = $preLoadEvent->getAsset();
} catch (NotFoundException $e) {
return null;
}
RuntimeCache::set($cacheKey, $asset);
}

Expand All @@ -278,7 +291,7 @@ public static function getById(int|string $id, array $params = []): ?static
} else {
$asset = null;
}

/** @var ?static $asset */
return $asset;
}

Expand Down
16 changes: 15 additions & 1 deletion models/DataObject/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Db;
use Pimcore\Event\DataObjectEvents;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\DataObjectPreLoadEvent;
use Pimcore\Logger;
use Pimcore\Model;
use Pimcore\Model\DataObject;
Expand Down Expand Up @@ -242,6 +243,11 @@
$object = self::getModelFactory()->build($className);
RuntimeCache::set($cacheKey, $object);
$object->getDao()->getById($id);
// fire pre load event
$preLoadEvent = new DataObjectPreLoadEvent($object, ['params' => $params]);
\Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DataObjectEvents::PRE_LOAD);
$object = $preLoadEvent->getObject();

$object->__setDataVersionTimestamp($object->getModificationDate());

Service::recursiveResetDirtyMap($object);
Expand All @@ -261,10 +267,18 @@
return null;
}
} else {
try {
// fire pre load event
$preLoadEvent = new DataObjectPreLoadEvent($object, ['params' => $params]);
\Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DataObjectEvents::PRE_LOAD);
$object = $preLoadEvent->getObject();
} catch (Model\Exception\NotFoundException $e) {
return null;
}
RuntimeCache::set($cacheKey, $object);
}

if (!$object || !static::typeMatch($object)) {

Check failure on line 281 in models/DataObject/AbstractObject.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, lowest, false)

Negated boolean expression is always false.

Check failure on line 281 in models/DataObject/AbstractObject.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Negated boolean expression is always false.

Check failure on line 281 in models/DataObject/AbstractObject.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Negated boolean expression is always false.
return null;
}

Expand All @@ -272,7 +286,7 @@
new DataObjectEvent($object, ['params' => $params]),
DataObjectEvents::POST_LOAD
);

/** @var ?static $object */
return $object;
}

Expand Down
15 changes: 14 additions & 1 deletion models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Event\DocumentEvents;
use Pimcore\Event\FrontendEvents;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Event\Model\DocumentPreLoadEvent;
use Pimcore\Logger;
use Pimcore\Model\Document\Hardlink\Wrapper\WrapperInterface;
use Pimcore\Model\Document\Listing;
Expand Down Expand Up @@ -211,6 +212,10 @@

try {
$document->getDao()->getById($id);
// fire pre load event
$preLoadEvent = new DocumentPreLoadEvent($document, ['params' => $params]);
\Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DocumentEvents::PRE_LOAD);
$document = $preLoadEvent->getDocument();
} catch (NotFoundException $e) {
return null;
}
Expand All @@ -235,10 +240,18 @@

\Pimcore\Cache::save($document, $cacheKey);
} else {
try {
// fire pre load event
$preLoadEvent = new DocumentPreLoadEvent($document, ['params' => $params]);
\Pimcore::getEventDispatcher()->dispatch($preLoadEvent, DocumentEvents::PRE_LOAD);
$document = $preLoadEvent->getDocument();
} catch (NotFoundException $e) {
return null;
}
RuntimeCache::set($cacheKey, $document);
}

if (!$document || !static::typeMatch($document)) {

Check failure on line 254 in models/Document.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, lowest, false)

Negated boolean expression is always false.

Check failure on line 254 in models/Document.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Negated boolean expression is always false.

Check failure on line 254 in models/Document.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1, lowest, false)

Negated boolean expression is always false.
return null;
}

Expand All @@ -246,7 +259,7 @@
new DocumentEvent($document, ['params' => $params]),
DocumentEvents::POST_LOAD
);

/** @var ?static $document */
return $document;
}

Expand Down
Loading