Skip to content

Commit

Permalink
refactor: register event, ignore 'requiresRight' annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
shpran committed Sep 30, 2021
1 parent 9046d66 commit acf7b8e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
56 changes: 56 additions & 0 deletions migrations/Version202109300806032234_tao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\tao\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\oatbox\event\EventManager;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use oat\tao\model\ParamConverter\Event\ParamConverterEvent;
use oat\tao\model\ParamConverter\EventListener\ParamConverterListener;

final class Version202109300806032234_tao extends AbstractMigration
{
public function getDescription(): string
{
return 'Attach ParamConverterEvent to EventManager.';
}

public function up(Schema $schema): void
{
$eventManager = $this->getEventManager();
$eventManager->attach(ParamConverterEvent::class, [ParamConverterListener::class, 'handleEvent']);
$this->getServiceLocator()->register(EventManager::SERVICE_ID, $eventManager);
}

public function down(Schema $schema): void
{
$eventManager = $this->getEventManager();
$eventManager->detach(ParamConverterEvent::class, [ParamConverterListener::class, 'handleEvent']);
$this->getServiceLocator()->register(EventManager::SERVICE_ID, $eventManager);
}

private function getEventManager(): EventManager
{
return $this->getServiceLocator()->getContainer()->get(EventManager::SERVICE_ID);
}
}
11 changes: 9 additions & 2 deletions models/classes/routing/ActionEnforcer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use oat\tao\model\action\CommonModuleInterface;
use oat\oatbox\service\ServiceManagerAwareTrait;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use oat\oatbox\service\ServiceManagerAwareInterface;
use oat\tao\model\Middleware\MiddlewareRequestHandler;
use oat\tao\model\accessControl\data\DataAccessControl;
Expand Down Expand Up @@ -325,7 +324,15 @@ private function applyParamConverters(

private function getParamConverters(ReflectionMethod $reflectionMethod): array
{
AnnotationRegistry::registerLoader('class_exists');
/**
* Ignore 'requiresRight' annotation as we don't have such annotation class in TAO.
*
* TODO Create annotation class and use it in all places instead of non-existing `requiresRight`
*/
AnnotationReader::addGlobalIgnoredName('requiresRight');

// Autoload 'ParamConverter' annotation.
class_exists(ParamConverter::class);
$annotations = (new AnnotationReader())->getMethodAnnotations($reflectionMethod);

return array_filter($annotations, static function ($annotation) {
Expand Down
32 changes: 18 additions & 14 deletions scripts/install/RegisterEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,38 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA;
*
* Copyright (c) 2020-2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types = 1);

namespace oat\tao\scripts\install;

use oat\oatbox\reporting\Report;
use oat\oatbox\event\EventManager;
use oat\oatbox\extension\InstallAction;
use oat\generis\model\OntologyAwareTrait;
use common_ext_event_ExtensionInstalled;
use oat\tao\model\migrations\MigrationsService;
use oat\tao\model\ParamConverter\Event\ParamConverterEvent;
use oat\tao\model\ParamConverter\EventListener\ParamConverterListener;

/**
* Class RegisterEvents
* @package oat\tao\scripts\install
*/
class RegisterEvents extends InstallAction
{
use OntologyAwareTrait;

public function __invoke($params)
{
/** @var EventManager $eventManager */
$eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID);
$eventManager->attach(\common_ext_event_ExtensionInstalled::class, [MigrationsService::SERVICE_ID, 'extensionInstalled']);
$this->getServiceManager()->register(EventManager::SERVICE_ID, $eventManager);
$eventManager = $this->getEventManager();
$eventManager->attach(
common_ext_event_ExtensionInstalled::class,
[MigrationsService::SERVICE_ID, 'extensionInstalled']
);
$eventManager->attach(ParamConverterEvent::class, [ParamConverterListener::class, 'handleEvent']);
$this->getServiceLocator()->register(EventManager::SERVICE_ID, $eventManager);

return new \common_report_Report(\common_report_Report::TYPE_SUCCESS, 'Events registered');
return Report::createSuccess('Events registered');
}

private function getEventManager(): EventManager
{
return $this->getServiceLocator()->getContainer()->get(EventManager::SERVICE_ID);
}
}

0 comments on commit acf7b8e

Please sign in to comment.