diff --git a/src/DataObjectEventListener.php b/src/DataObjectEventListener.php index a12feba..b15a1f0 100644 --- a/src/DataObjectEventListener.php +++ b/src/DataObjectEventListener.php @@ -2,9 +2,13 @@ namespace ArchiPro\Silverstripe\EventDispatcher; +use ArchiPro\EventDispatcher\ListenerProvider; use ArchiPro\Silverstripe\EventDispatcher\Event\DataObjectEvent; use ArchiPro\Silverstripe\EventDispatcher\Event\Operation; +use ArchiPro\Silverstripe\EventDispatcher\Service\EventService; use Closure; +use SilverStripe\Core\Injector\Injectable; +use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataObject; /** @@ -16,6 +20,8 @@ */ class DataObjectEventListener { + use Injectable; + /** * Creates a new DataObject event listener. * @@ -31,6 +37,19 @@ public function __construct( $this->operations = $operations ?? Operation::cases(); } + /** + * Registers this listener with the given provider. + * + * If no provider is provided, the global EventService will be used. + */ + public function selfRegister(ListenerProvider|EventService $provider = null): void + { + if (empty($provider)) { + $provider = Injector::inst()->get(EventService::class); + } + $provider->addListener(DataObjectEvent::class, $this); + } + /** * Handles a DataObject event. * diff --git a/src/Event/DataObjectEvent.php b/src/Event/DataObjectEvent.php index b2cf07e..29d562d 100644 --- a/src/Event/DataObjectEvent.php +++ b/src/Event/DataObjectEvent.php @@ -2,6 +2,7 @@ namespace ArchiPro\Silverstripe\EventDispatcher\Event; +use SilverStripe\Core\Extension; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Member; @@ -118,17 +119,11 @@ public function getObject(bool $useVersion = false): ?DataObject return null; } - $object = DataObject::get_by_id($this->objectClass, $this->objectID); - - // If we want the specific version and the object is versioned - if ($useVersion && $this->version && $object && $object->hasExtension(Versioned::class)) { - /** @var Versioned|DataObject $object */ - return $object->Version == $this->version - ? $object - : $object->Versions()->byID($this->version); + if (!$useVersion || empty($this->version)) { + return DataObject::get_by_id($this->objectClass, $this->objectID, false); } - return $object; + return Versioned::get_version($this->objectClass, $this->objectID, $this->version); } /** diff --git a/tests/php/Event/AbstractDataObjectEventTest.php b/tests/php/Event/AbstractDataObjectEventTest.php deleted file mode 100644 index 47ec31a..0000000 --- a/tests/php/Event/AbstractDataObjectEventTest.php +++ /dev/null @@ -1,43 +0,0 @@ - ['old' => null, 'new' => 'New Page']] - ); - - $this->assertEquals(1, $event->getObjectID()); - $this->assertEquals('Page', $event->getObjectClass()); - $this->assertEquals('create', $event->getAction()); - $this->assertArrayHasKey('Title', $event->getChanges()); - } - - public function testJsonSerialization(): void - { - $event = new DataObjectWriteEvent( - 1, - 'Page', - 'create', - ['Title' => ['old' => null, 'new' => 'New Page']] - ); - - $json = json_encode($event); - $data = json_decode($json, true); - - $this->assertArrayHasKey('id', $data); - $this->assertArrayHasKey('class', $data); - $this->assertArrayHasKey('action', $data); - $this->assertArrayHasKey('changes', $data); - $this->assertArrayHasKey('timestamp', $data); - } -} diff --git a/tests/php/Event/DataObjectVersionEventTest.php b/tests/php/Event/DataObjectVersionEventTest.php deleted file mode 100644 index c4a2eed..0000000 --- a/tests/php/Event/DataObjectVersionEventTest.php +++ /dev/null @@ -1,42 +0,0 @@ - ['old' => 'Old Title', 'new' => 'New Title']] - ); - - $this->assertEquals(1, $event->getObjectID()); - $this->assertEquals('Page', $event->getObjectClass()); - $this->assertEquals('publish', $event->getAction()); - $this->assertEquals(2, $event->getVersion()); - } - - public function testVersionJsonSerialization(): void - { - $event = new DataObjectVersionEvent( - 1, - 'Page', - 'publish', - 2, - [] - ); - - $json = json_encode($event); - $data = json_decode($json, true); - - $this->assertArrayHasKey('version', $data); - $this->assertEquals(2, $data['version']); - } -} diff --git a/tests/php/Extension/EventDispatchExtensionTest.php b/tests/php/Extension/EventDispatchExtensionTest.php index 474b988..13cc8f2 100644 --- a/tests/php/Extension/EventDispatchExtensionTest.php +++ b/tests/php/Extension/EventDispatchExtensionTest.php @@ -30,15 +30,12 @@ public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - $service = Injector::inst()->get(EventService::class); - - // Add listener that captures events - $service->addListener(DataObjectEvent::class, new DataObjectEventListener( + DataObjectEventListener::create( function (DataObjectEvent $event) { static::$events[] = $event; }, [SimpleDataObject::class, VersionedDataObject::class] - )); + )->selfRegister(); } protected function setUp(): void