From acf7b8e0fe3eb2c75b9ce30f9e4a3ec507c800bc Mon Sep 17 00:00:00 2001 From: Andrei Shapiro Date: Thu, 30 Sep 2021 10:45:22 +0000 Subject: [PATCH] refactor: register event, ignore 'requiresRight' annotation --- migrations/Version202109300806032234_tao.php | 56 ++++++++++++++++++++ models/classes/routing/ActionEnforcer.php | 11 +++- scripts/install/RegisterEvents.php | 32 ++++++----- 3 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 migrations/Version202109300806032234_tao.php diff --git a/migrations/Version202109300806032234_tao.php b/migrations/Version202109300806032234_tao.php new file mode 100644 index 0000000000..881353a6fc --- /dev/null +++ b/migrations/Version202109300806032234_tao.php @@ -0,0 +1,56 @@ +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); + } +} diff --git a/models/classes/routing/ActionEnforcer.php b/models/classes/routing/ActionEnforcer.php index 03acfda411..ec82ddd9da 100644 --- a/models/classes/routing/ActionEnforcer.php +++ b/models/classes/routing/ActionEnforcer.php @@ -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; @@ -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) { diff --git a/scripts/install/RegisterEvents.php b/scripts/install/RegisterEvents.php index 9c617671ac..7d9835a1a2 100644 --- a/scripts/install/RegisterEvents.php +++ b/scripts/install/RegisterEvents.php @@ -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); } }