/** * Returns a map with all annotation found for the given annotation name. The * key of the map is the class name and the value is an instance of the annotation * class. * * @param string $annotationName * @param array $targetPackageNames * @return Customweb_IAnnotation[] */ public function find($annotationName, $targetPackageNames = null) { try { Customweb_Core_Util_Class::loadLibraryClassByName($annotationName); } catch (Customweb_Core_Exception_ClassNotFoundException $e) { } $rs = array(); $candiates = $this->findTargetClassCandidates($annotationName, $targetPackageNames); if (strstr($annotationName, '_') !== false) { $alternativeAnnotationName = substr($annotationName, strrpos($annotationName, '_') + 1); $candiates = array_merge($candiates, $this->findTargetClassCandidates($alternativeAnnotationName, $targetPackageNames)); } foreach ($candiates as $candidate) { if (strpos($candidate, '::') === false) { Customweb_Core_Util_Class::loadLibraryClassByName($candidate); $reflection = new Customweb_Annotation_ReflectionAnnotatedClass($candidate); if ($reflection->hasAnnotation($annotationName)) { $rs[$reflection->getName()] = $reflection->getAnnotation($annotationName); } } else { $candiateClass = substr($candidate, 0, strpos($candidate, '::')); $candidateMethod = substr($candidate, strlen($candiateClass) + 2); Customweb_Core_Util_Class::loadLibraryClassByName($candiateClass); $reflectionClass = new Customweb_Annotation_ReflectionAnnotatedClass($candiateClass); $reflectionMethod = $reflectionClass->getMethod($candidateMethod); if ($reflectionMethod->hasAnnotation($annotationName)) { $rs[$candidate] = $reflectionMethod->getAnnotation($annotationName); } } } return $rs; }
/** * Creates a bean object. This object can be used to create an instance of the * class defined with this bean. * * @param string $beanId * @param string $className * @throws Exception * @return Customweb_DependencyInjection_Bean_Generic */ public static function createBeanInstance($beanId, $className) { if (empty($beanId)) { $beanId = $className; } Customweb_Core_Util_Class::loadLibraryClassByName('Customweb_DependencyInjection_Bean_Provider_Annotation_Inject'); $dependencies = array(); Customweb_Core_Util_Class::loadLibraryClassByName($className); $reflector = new Customweb_Annotation_ReflectionAnnotatedClass($className); foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { $annotations = $method->getAllAnnotations(); if ($method->isConstructor() || $method->hasAnnotation('Customweb_DependencyInjection_Bean_Provider_Annotation_Inject')) { if ($method->isConstructor() && !$method->hasAnnotation('Customweb_DependencyInjection_Bean_Provider_Annotation_Inject')) { $injects = self::getInjectsFromMethod($method); } else { $annotation = $method->getAnnotation('Customweb_DependencyInjection_Bean_Provider_Annotation_Inject'); /* @var $annotation Customweb_DependencyInjection_Bean_Provider_Annotation_Inject */ $injects = $annotation->getInjects(); if (!is_array($injects) || count($injects) <= 0) { $injects = self::getInjectsFromMethod($method); } elseif (count($injects) !== $method->getNumberOfParameters()) { throw new Exception("Invalid annotation 'Inject' on method '" . $method->getName() . "' on class '" . $className . "'. The number of inject arguments do not match the number of arguments."); } } $dependencies[] = new Customweb_DependencyInjection_Bean_Generic_DefaultDependency($method->getName(), $injects); } } return new Customweb_DependencyInjection_Bean_Generic($beanId, $className, $dependencies); }
public function __construct($className, $name) { $classReflector = new Customweb_Annotation_ReflectionAnnotatedClass($className); $this->name = $name; $this->className = $className; // TODO: Make hasMethod etc. also working with parant classes. if ($classReflector->hasMethod('get' . Customweb_Core_Util_String::ucFirst($name))) { $this->getMethodReflector = new Customweb_Annotation_ReflectionAnnotatedMethod($className, 'get' . Customweb_Core_Util_String::ucFirst($name)); } if ($classReflector->hasMethod('set' . Customweb_Core_Util_String::ucFirst($name))) { $this->setMethodReflector = new Customweb_Annotation_ReflectionAnnotatedMethod($className, 'set' . Customweb_Core_Util_String::ucFirst($name)); } if ($classReflector->hasProperty($name)) { $this->propertyReflector = new Customweb_Annotation_ReflectionAnnotatedProperty($className, $name); } }
protected function createActionInvocationContainer(Customweb_Core_Http_IRequest $request, ReflectionMethod $method, $controllerObject) { $container = parent::createActionInvocationContainer($request, $method, $controllerObject); if ($this->isActionMethodContainParameterTransaction($method)) { $controllerReflection = new Customweb_Annotation_ReflectionAnnotatedClass($controllerObject); $ids = null; foreach ($controllerReflection->getMethods(ReflectionMethod::IS_PUBLIC) as $controllerMethod) { if ($controllerMethod->hasAnnotation('Customweb_Payment_Endpoint_Annotation_ExtractionMethod')) { $ids = $controllerMethod->invoke($controllerObject, $request); break; } } if (is_array($ids)) { if (!isset($ids['id'])) { throw new Exception("The extraction method does not return an array with an index 'id'."); } if (!isset($ids['key'])) { throw new Exception("The extraction method does not return an array with an index 'key'."); } $transaction = null; if ($ids['key'] == Customweb_Payment_Endpoint_Annotation_ExtractionMethod::PAYMENT_ID_KEY) { $this->getTransactionHandler()->beginTransaction(); $this->databaseTransactionActive = true; $transaction = $this->getTransactionHandler()->findTransactionByPaymentId($ids['id']); } else { if ($ids['key'] == Customweb_Payment_Endpoint_Annotation_ExtractionMethod::TRANSACTION_ID_KEY) { $this->getTransactionHandler()->beginTransaction(); $this->databaseTransactionActive = true; $transaction = $this->getTransactionHandler()->findTransactionByTransactionId($ids['id']); } else { if ($ids['key'] == Customweb_Payment_Endpoint_Annotation_ExtractionMethod::EXTERNAL_TRANSACTION_ID_KEY) { $this->getTransactionHandler()->beginTransaction(); $this->databaseTransactionActive = true; $transaction = $this->getTransactionHandler()->findTransactionByTransactionExternalId($ids['id']); } else { throw new Exception("Invalid value for 'key' provided."); } } } $container->addBean(new Customweb_DependencyInjection_Bean_Object($transaction)); } else { throw new Exception(Customweb_I18n_Translation::__("The controller class '@controller' does not provide any method with annotation 'Customweb_Payment_Endpoint_Annotation_ExtractionMethod' and valid output.", array('@controller' => get_class($controllerObject)))); } } return $container; }
/** * This method returns a map of actions on the given class. The key is the action name and * the value is the method. * * @param string $controllerClassName Controller class name * @return array */ protected function getActions($controllerClassName) { Customweb_Core_Util_Class::loadLibraryClassByName('Customweb_Mvc_Controller_Annotation_Action'); $key = strtolower($controllerClassName); if (!isset($this->actions[$key])) { $this->actions[$key] = array(); $reflector = new Customweb_Annotation_ReflectionAnnotatedClass($controllerClassName); foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if ($method->hasAnnotation('Customweb_Mvc_Controller_Annotation_Action')) { $annotation = $method->getAnnotation('Customweb_Mvc_Controller_Annotation_Action'); if ($annotation instanceof Customweb_Mvc_Controller_Annotation_Action) { $actionName = $annotation->getName(); if (empty($actionName)) { $actionName = $method->getName(); } $this->actions[$key][$actionName] = $method->getName(); } } } } return $this->actions[$key]; }