/** * * @param String $filename réal path of the php file to scan ex: /home/project/src/org/foo/bar.php * @param String $namespace the name space of this file ex: org\foo * @return void * @throws IocException */ protected function scanFile($filename, $namespace) { if (!is_file($filename)) { \org\equinox\utils\Logger::error("({$filename}) is not a file!"); throw new IocException("({$filename}) is not a file!"); } include_once $filename; $name = basename($filename); $className = $namespace . '\\' . substr($name, 0, strpos($name, '.')); if (class_exists($className)) { $ref = new \ReflectionAnnotatedClass($className); $listAnnotation = $ref->getAllAnnotations(); if (empty($listAnnotation)) { return; // this class is not a Component ! } $bean = new BeanDefinition(); $bean->setClassName($className); foreach ($listAnnotation as $annotation) { if ($annotation instanceof annotations\ClassAnnotation) { $annotation->doBean($bean, $className); } } if ($bean->getId() == null) { throw new IocException("Equinox annotation found but no @Component annotation found (mandatory) in {$className}"); } $listProperties = $ref->getProperties(); foreach ($listProperties as $property) { $this->doProperty($bean, $property); } $listMethod = $ref->getMethods(); foreach ($listMethod as $method) { $this->doMethod($bean, $method); } $this->context->addBeanDefinition($bean); } }
public function testMultipleAnnotationsOnClassWithRestriction() { $reflection = new ReflectionAnnotatedClass('MultiExample'); $annotations = $reflection->getAllAnnotations('FirstAnnotation'); $this->assertEqual(count($annotations), 2); $this->assertEqual($annotations[0]->value, 1); $this->assertEqual($annotations[1]->value, 2); $this->assertIsA($annotations[0], 'FirstAnnotation'); $this->assertIsA($annotations[1], 'FirstAnnotation'); }
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']); } }