/** * Loads from annotations from a class. * * @param string $class A class name * @param string $type The resource type * * @return SecurityPolicyRules A Rules instance * * @throws \InvalidArgumentException When annotations can't be parsed */ public function load($class, $type = null) { if (!class_exists($class)) { throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); } $class = new \ReflectionClass($class); if ($class->isAbstract()) { throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class)); } $rules = new SecurityPolicyRules(); $rules->addResource(new FileResource($class->getFileName())); foreach ($class->getMethods() as $method) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { if ($annot instanceof $this->annotationClass) { $methodName = $method->getName(); $rules->addMethod($class->getName(), $methodName); } } } foreach ($class->getProperties() as $property) { foreach ($this->reader->getPropertyAnnotations($property) as $annot) { if ($annot instanceof $this->annotationClass) { $rules->addProperty($class->getName(), $property->getName()); } } } return $rules; }
/** * Loads from annotations from a file. * * @param string $file A PHP file path * @param string $type The resource type * * @return SecurityPolicyRules A Rules instance * * @throws \InvalidArgumentException When annotations can't be parsed */ public function load($file, $type = null) { $path = $this->locator->locate($file); $rules = new SecurityPolicyRules(); if ($class = $this->findClass($path)) { $rules->addResource(new FileResource($path)); $rules->merge($this->loader->load($class, $type)); } return $rules; }