Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-rainville committed Nov 6, 2024
0 parents commit faef9b0
Show file tree
Hide file tree
Showing 16 changed files with 658 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
tests:
name: PHP ${{ matrix.php }} - Silverstripe ${{ matrix.silverstripe }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['8.1']
silverstripe: ['4.13', '5.0']

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mbstring, intl, pdo, mysql
coverage: xdebug

- name: Install dependencies
run: |
composer require --no-update silverstripe/framework:^${{ matrix.silverstripe }}
composer install --no-interaction --no-progress
- name: Run PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --diff

- name: Run PHPStan
run: vendor/bin/phpstan analyse

- name: Run PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/vendor/
.phpunit.result.cache
.php-cs-cache
.env
.idea/
.vscode/
*.swp
*.swo
.DS_Store
32 changes: 32 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

$config = new PhpCsFixer\Config();

return $config
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_align' => true,
'phpdoc_order' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_trim' => true,
'phpdoc_var_without_name' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_quote' => true,
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
])
->setFinder($finder);
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Silverstripe Events Module

This module provides PSR-14 Event Dispatcher integration for Silverstripe CMS with automatic event dispatching for DataObject CRUD operations and versioning actions.

## Installation
10 changes: 10 additions & 0 deletions _config/events.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
Name: events
After:
- '#coreservices'
---
SilverStripe\Core\Injector\Injector:
ArchiPro\Silverstripe\EventDispatcher\Service\EventService:
constructor:
dispatcher: '%$Psr\EventDispatcher\EventDispatcherInterface'
listenerProvider: '%$Psr\EventDispatcher\ListenerProviderInterface'
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "archipro/silverstripe-event-dispatcher",
"description": "PSR-14 Event Dispatcher integration for Silverstripe CMS",
"type": "silverstripe-vendormodule",
"license": "MIT",
"require": {
"php": "^8.1",
"silverstripe/framework": "^4.13 || ^5.0",
"silverstripe/versioned": "^1.13 || ^2.0",
"psr/event-dispatcher": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^1.10",
"symbiote/silverstripe-phpstan": "^1.0"
},
"autoload": {
"psr-4": {
"ArchiPro\\Silverstripe\\EventDispatcher\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ArchiPro\\Silverstripe\\EventDispatcher\\Tests\\": "tests/"
}
},
"scripts": {
"lint": "php-cs-fixer fix --dry-run --diff",
"lint-fix": "php-cs-fixer fix",
"analyse": "phpstan analyse",
"test": "phpunit"
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"expose": []
}
}
14 changes: 14 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
parameters:
level: 8
paths:
- src
- tests
excludePaths:
- vendor/*
silverstripe:
checkUnusedViewVariables: false
ignoreErrors:
- '#Access to an undefined property .+::\$owner#'
- '#Call to an undefined method .+::hasExtension\(\)#'
includes:
- vendor/symbiote/silverstripe-phpstan/phpstan.neon
52 changes: 52 additions & 0 deletions src/Event/AbstractDataObjectEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\Event;

use JsonSerializable;

/**
* Base class for all DataObject-related events.
*
* Provides common functionality for events that are triggered by DataObject operations.
* All events are serializable to JSON for easy logging and external system integration.
*/
abstract class AbstractDataObjectEvent implements JsonSerializable
{
public function __construct(
protected readonly int $objectID,
protected readonly string $objectClass,
protected readonly string $action,
protected readonly array $changes = []
) {}

public function getObjectID(): int
{
return $this->objectID;
}

public function getObjectClass(): string
{
return $this->objectClass;
}

public function getAction(): string
{
return $this->action;
}

public function getChanges(): array
{
return $this->changes;
}

public function jsonSerialize(): array
{
return [
'id' => $this->objectID,
'class' => $this->objectClass,
'action' => $this->action,
'changes' => $this->changes,
'timestamp' => time(),
];
}
}
13 changes: 13 additions & 0 deletions src/Event/DataObjectDeleteEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\Event;

/**
* Event class for DataObject deletion operations.
*
* This event is fired before a DataObject is permanently deleted from the database.
* It allows listeners to perform cleanup or logging operations before the deletion occurs.
*/
class DataObjectDeleteEvent extends AbstractDataObjectEvent
{
}
27 changes: 27 additions & 0 deletions src/Event/DataObjectEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace YourVendor\Events\Event;

use SilverStripe\ORM\DataObject;

class DataObjectEvent
{
private DataObject $dataObject;
private string $action;

public function __construct(DataObject $dataObject, string $action)
{
$this->dataObject = $dataObject;
$this->action = $action;
}

public function getDataObject(): DataObject
{
return $this->dataObject;
}

public function getAction(): string
{
return $this->action;
}
}
37 changes: 37 additions & 0 deletions src/Event/DataObjectVersionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\Event;

/**
* Event class for versioning-related actions on DataObjects.
*
* This event is fired when versioning actions occur, such as:
* - Publishing
* - Unpublishing
* - Archiving
* - Restoring
*/
class DataObjectVersionEvent extends AbstractDataObjectEvent
{
public function __construct(
int $objectID,
string $objectClass,
string $action,
private readonly ?int $version,
array $changes = []
) {
parent::__construct($objectID, $objectClass, $action, $changes);
}

public function getVersion(): ?int
{
return $this->version;
}

public function jsonSerialize(): array
{
return array_merge(parent::jsonSerialize(), [
'version' => $this->version,
]);
}
}
16 changes: 16 additions & 0 deletions src/Event/DataObjectWriteEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ArchiPro\Silverstripe\EventDispatcher\Event;

/**
* Event class for DataObject write operations.
*
* This event is fired when a DataObject is:
* - Created (first write)
* - Updated (subsequent writes)
*
* The event includes all changes made to the DataObject during the write operation.
*/
class DataObjectWriteEvent extends AbstractDataObjectEvent
{
}
Loading

0 comments on commit faef9b0

Please sign in to comment.