Skip to content

Register own datasource

Rello edited this page Jul 4, 2024 · 6 revisions

External applications can register their own datasource via an event listener.

The app must register DatasourceEvent and implement IDatasource to deliver the data.

/lib/AppInfo/Application.php

use OCA\Analytics\Datasource\DatasourceEvent;
use OCA\xxx\Listener\AnalyticsDatasourceListener;
...

public function register(IRegistrationContext $context): void
{
    $context->registerEventListener(DatasourceEvent::class, AnalyticsDatasourceListener::class);
}

/lib/Listener/Listener.php

<?php
declare(strict_types=1);

namespace OCA\xxx\Listener;

use OCA\xxx\Analytics\Datasource;
use OCA\Analytics\Datasource\DatasourceEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

class AnalyticsDatasourceListener implements IEventListener {
    public function handle(Event $event): void {
        if (!($event instanceof DatasourceEvent)) {
            // Unrelated
            return;
        }
        $event->registerDatasource(Datasource::class);
    }
}

/lib/Analytics/Datasource.php

must implement IDatasource;

Example

implementation examples:

Forms; Tables;