/**
  * 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;
 }
Esempio n. 2
0
 public function dump(SecurityPolicyRules $rules)
 {
     $result = "<?php\n";
     $result .= "return new Intaro\\TwigSandboxBundle\\SecurityPolicy\\SecurityPolicyRules(\n";
     $result .= "    array(\n";
     foreach ($rules->getMethods() as $entity => $methods) {
         $result .= "        '{$entity}' => array(\n";
         if (sizeof($methods)) {
             $result .= "            '" . implode("', '", $methods) . "'\n";
         }
         $result .= "        ),\n";
     }
     $result .= "    ),\n";
     $result .= "    array(\n";
     foreach ($rules->getProperties() as $entity => $properties) {
         $result .= "        '{$entity}' => array(\n";
         if (sizeof($properties)) {
             $result .= "            '" . implode("', '", $properties) . "'\n";
         }
         $result .= "        ),\n";
     }
     $result .= "    )\n";
     $result .= ");\n";
     return $result;
 }
 /**
  * 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;
 }
 public function merge(SecurityPolicyRules $rules)
 {
     $this->resources = array_merge($this->resources, $rules->getResources());
     foreach ($rules->getMethods() as $class => $methods) {
         foreach ($methods as $method) {
             $this->addMethod($class, $method);
         }
     }
     foreach ($rules->getProperties() as $class => $properties) {
         foreach ($properties as $property) {
             $this->addProperty($class, $property);
         }
     }
 }
 public function getPolicyRules()
 {
     if ($this->rules) {
         return $this->rules;
     }
     if (null === $this->options['cache_dir'] || null === $this->options['cache_filename']) {
         throw new \RuntimeException('Options "cache_dir" and "cache_filename" must be defined.');
     }
     $cache = new ConfigCache($this->options['cache_dir'] . '/' . $this->options['cache_filename'] . '.php', $this->options['debug']);
     if (!$cache->isFresh()) {
         $rules = new SecurityPolicyRules();
         foreach ($this->options['bundles'] as $bundle) {
             $refl = new \ReflectionClass($bundle);
             $dir = dirname($refl->getFileName()) . '/Entity';
             if (file_exists($dir) && is_dir($dir)) {
                 $rules->merge($this->loader->load($dir));
             }
         }
         $dumper = new $this->options['dumper_class']();
         $cache->write($dumper->dump($rules), $rules->getResources());
     }
     $this->rules = (include $cache);
     return $this->rules;
 }