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

Better application registering and events dispatching #392

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 1 addition & 4 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@


$app = \OC::$server->query(Application::class);
$app->registerNavigation();
$app->registerFilesNavigation();
$app->registerFilesPlugin();

Comment on lines -37 to -40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this in here, as it seems that the app.php is loaded only when the app itself is needed.

Copy link
Contributor Author

@tortuetorche tortuetorche Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During my tests, I saw the listeners (registered in this file) triggered twice each time, so this pull-request fix this issue.
And for the render() method who merge all render methods in one, I borrowed the Talk application

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as it seems that the app.php is loaded only when the app itself is needed.

This should not be the case and therefor it's fine when everything is done in a register method.
But it can be replaced soon for Nextcloud 20:
nextcloud/server#20865

$app->register();
44 changes: 27 additions & 17 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

namespace OCA\Circles\AppInfo;

use OC;
use OCA\Circles\Api\v1\Circles;
use OCA\Circles\Notification\Notifier;
use OCA\Circles\Service\ConfigService;
Expand All @@ -39,6 +38,7 @@
use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Util;

require_once __DIR__ . '/../../appinfo/autoload.php';
Expand All @@ -56,22 +56,35 @@ class Application extends App {
/** @var IAppContainer */
private $container;

/** @var \OC\ServerServer */
private $server;

/** @var IEventDispatcher */
private $dispatcher;

/**
* @param array $params
*/
public function __construct(array $params = array()) {
parent::__construct(self::APP_NAME, $params);
}

public function register()
{
$this->container = $this->getContainer();
$this->server = $this->container->getServer();
$this->dispatcher = $this->server->query(IEventDispatcher::class);

$manager = OC::$server->getNotificationManager();
$manager = $this->server->getNotificationManager();
$manager->registerNotifierService(Notifier::class);

$this->registerNavigation();
$this->registerFilesNavigation();
$this->registerFilesPlugin();
$this->registerHooks();
$this->registerDavHooks();
}


/**
* Register Hooks
*/
Expand All @@ -91,7 +104,7 @@ public function registerHooks() {
public function registerNavigation() {
/** @var ConfigService $configService */
try {
$configService = OC::$server->query(ConfigService::class);
$configService = $this->server->query(ConfigService::class);
} catch (QueryException $e) {
return;
}
Expand All @@ -104,8 +117,8 @@ public function registerNavigation() {
->getNavigationManager();
$appManager->add(
function() {
$urlGen = OC::$server->getURLGenerator();
$navName = OC::$server->getL10N(self::APP_NAME)
$urlGen = $this->server->getURLGenerator();
$navName = $this->server->getL10N(self::APP_NAME)
->t('Circles');

return [
Expand All @@ -121,8 +134,7 @@ function() {
}

public function registerFilesPlugin() {
$eventDispatcher = OC::$server->getEventDispatcher();
$eventDispatcher->addListener(
$this->dispatcher->addListener(
'OCA\Files::loadAdditionalScripts',
function() {
Circles::addJavascriptAPI();
Expand All @@ -143,7 +155,7 @@ public function registerFilesNavigation() {
$appManager = FilesApp::getNavigationManager();
$appManager->add(
function() {
$l = OC::$server->getL10N('circles');
$l = $this->server->getL10N('circles');

return [
'id' => 'circlesfilter',
Expand All @@ -160,24 +172,22 @@ function() {
public function registerDavHooks() {
try {
/** @var ConfigService $configService */
$configService = OC::$server->query(ConfigService::class);
$configService = $this->server->query(ConfigService::class);
if (!$configService->isContactsBackend()) {
return;
}

/** @var DavService $davService */
$davService = OC::$server->query(DavService::class);
$davService = $this->server->query(DavService::class);
} catch (QueryException $e) {
return;
}

$event = OC::$server->getEventDispatcher();
$this->dispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, [$davService, 'onAppEnabled']);
$this->dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', [$davService, 'onCreateCard']);
$this->dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', [$davService, 'onUpdateCard']);
$this->dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', [$davService, 'onDeleteCard']);

$event->addListener(ManagerEvent::EVENT_APP_ENABLE, [$davService, 'onAppEnabled']);
$event->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', [$davService, 'onCreateCard']);
$event->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', [$davService, 'onUpdateCard']);
$event->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', [$davService, 'onDeleteCard']);
}

}

2 changes: 1 addition & 1 deletion lib/Service/DavService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
use OCP\App\ManagerEvent;
use OCP\Federation\ICloudIdManager;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\EventDispatcher\GenericEvent;


/**
Expand Down
10 changes: 5 additions & 5 deletions lib/Service/EventsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\GenericEvent;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

class EventsService {

Expand All @@ -65,7 +65,7 @@ class EventsService {
/** @var IURLGenerator */
private $urlGenerator;

/** @var EventDispatcher */
/** @var IEventDispatcher */
private $eventDispatcher;

/** @var CirclesRequest */
Expand All @@ -90,7 +90,7 @@ class EventsService {
* @param INotificationManager $notificationManager
* @param IUserManager $userManager
* @param IURLGenerator $urlGenerator
* @param EventDispatcher $eventDispatcher
* @param IEventDispatcher $eventDispatcher
* @param CirclesRequest $circlesRequest
* @param MembersRequest $membersRequest
* @param ConfigService $configService
Expand All @@ -99,7 +99,7 @@ class EventsService {
public function __construct(
$userId, ITimeFactory $time, IActivityManager $activityManager,
INotificationManager $notificationManager, IUserManager $userManager, IURLGenerator $urlGenerator,
EventDispatcher $eventDispatcher, CirclesRequest $circlesRequest, MembersRequest $membersRequest,
IEventDispatcher $eventDispatcher, CirclesRequest $circlesRequest, MembersRequest $membersRequest,
ConfigService $configService, MiscService $miscService
) {
$this->userId = $userId;
Expand Down