public function registerInstance($MODULE_NAME, $name, &$obj) { $name = strtolower($name); $this->logger->log('DEBUG', "Registering instance name '{$name}' for module '{$MODULE_NAME}'"); if (Registry::instanceExists($name)) { $this->logger->log('WARN', "Instance with name '{$name}' already registered--replaced with new instance"); } $obj->moduleName = $MODULE_NAME; Registry::setInstance($name, $obj); // register settings annotated on the class $reflection = new ReflectionAnnotatedClass($obj); foreach ($reflection->getProperties() as $property) { if ($property->hasAnnotation('Setting')) { $this->settingManager->add($MODULE_NAME, $property->getAnnotation('Setting')->value, $property->getAnnotation('Description')->value, $property->getAnnotation('Visibility')->value, $property->getAnnotation('Type')->value, $obj->{$property->name}, @$property->getAnnotation('Options')->value, @$property->getAnnotation('Intoptions')->value, @$property->getAnnotation('AccessLevel')->value, @$property->getAnnotation('Help')->value); } } // register commands, subcommands, and events annotated on the class $commands = array(); $subcommands = array(); foreach ($reflection->getAllAnnotations() as $annotation) { if ($annotation instanceof DefineCommand) { if (!$annotation->command) { $this->logger->log('WARN', "Cannot parse @DefineCommand annotation in '{$name}'."); } $command = $annotation->command; $definition = array('channels' => $annotation->channels, 'defaultStatus' => $annotation->defaultStatus, 'accessLevel' => $annotation->accessLevel, 'description' => $annotation->description, 'help' => $annotation->help, 'handlers' => array()); list($parentCommand, $subCommand) = explode(" ", $command, 2); if ($subCommand) { $definition['parentCommand'] = $parentCommand; $subcommands[$command] = $definition; } else { $commands[$command] = $definition; } // register command alias if defined if ($annotation->alias) { $this->commandAlias->register($MODULE_NAME, $command, $annotation->alias); } } } foreach ($reflection->getMethods() as $method) { if ($method->hasAnnotation('Setup')) { $this->setupHandlers[] = array($name, $method->name); } else { if ($method->hasAnnotation('HandlesCommand')) { $commandName = $method->getAnnotation('HandlesCommand')->value; $methodName = $method->name; $handlerName = "{$name}.{$method->name}"; if (isset($commands[$commandName])) { $commands[$commandName]['handlers'][] = $handlerName; } else { if (isset($subcommands[$commandName])) { $subcommands[$commandName]['handlers'][] = $handlerName; } else { $this->logger->log('WARN', "Cannot handle command '{$commandName}' as it is not defined with @DefineCommand in '{$name}'."); } } } else { if ($method->hasAnnotation('Event')) { $this->eventManager->register($MODULE_NAME, $method->getAnnotation('Event')->value, $name . '.' . $method->name, @$method->getAnnotation('Description')->value, @$method->getAnnotation('Help')->value, @$method->getAnnotation('DefaultStatus')->value); } } } } foreach ($commands as $command => $definition) { $this->commandManager->register($MODULE_NAME, $definition['channels'], implode(',', $definition['handlers']), $command, $definition['accessLevel'], $definition['description'], $definition['help'], $definition['defaultStatus']); } foreach ($subcommands as $subCommand => $definition) { $this->subcommandManager->register($MODULE_NAME, $definition['channels'], implode(',', $definition['handlers']), $subCommand, $definition['accessLevel'], $definition['parentCommand'], $definition['description'], $definition['help'], $definition['defaultStatus']); } }
public function testRegistryExceptionAlreadyInitialized() { $registry = Registry::getInstance(); try { Registry::setClassName('anyclass'); $this->fail('Expected exception, because we cannot initialize the registry if it is already initialized.'); } catch (RuntimeException $e) { $this->assertContains('Registry is already initialized', $e->getMessage()); } try { Registry::setInstance(new Registry()); $this->fail('Expected exception, because we cannot initialize the registry if it is already initialized.'); } catch (RuntimeException $e) { $this->assertContains('Registry is already initialized', $e->getMessage()); } }
private function createGlobalInstances() { $newInstances = Registry::getNewInstancesInDir("./core"); foreach ($newInstances as $name => $className) { Registry::setInstance($name, new $className()); } }