public function dump(EventsMap $map) { $result = "<?php\n"; $result .= "use Intaro\\RuleEngineBundle\\Event\\Mapper\\EventMap;\n"; $result .= "return new Intaro\\RuleEngineBundle\\Event\\Mapper\\EventsMap(\n"; $result .= " array(\n"; foreach ($map->getEventMaps() as $eventMap) { $result .= " '" . $eventMap->getName() . "' => new EventMap("; $result .= "'" . $eventMap->getName() . "','" . $eventMap->getClass() . "','" . $eventMap->getType() . "',array("; $i = 0; foreach ($eventMap->getGetters() as $method => $getterMeta) { $result .= ($i > 0 ? ', ' : '') . "'{$method}' => array("; $result .= "'field' => '" . $getterMeta['field'] . "'"; $result .= ",'type' => '" . $getterMeta['type'] . "'"; $result .= ",'data' => " . ($getterMeta['data'] ? 'true' : 'false') . ""; $result .= ")"; $i++; } $result .= "), array("; $i = 0; foreach ($eventMap->getSetters() as $method => $setterMeta) { $result .= ($i > 0 ? ', ' : '') . "'{$method}' => array("; $result .= "'field' => '" . $setterMeta['field'] . "'"; $result .= ",'type' => '" . $setterMeta['type'] . "'"; $result .= ",'required' => " . ($setterMeta['required'] ? 'true' : 'false') . ""; $result .= ")"; $i++; } $result .= ")),\n"; } $result .= " )\n"; $result .= ");\n"; return $result; }
/** * Loads from annotations from a directory. * * @param string $path A directory path * @param string $type The resource type * * @return EventsMap A event map * * @throws \InvalidArgumentException When annotations can't be parsed */ public function load($path, $type = null) { $dir = $this->locator->locate($path); $map = new EventsMap(); $map->addResource(new DirectoryResource($dir, '/Event\\.php$/')); $files = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY)); usort($files, function (\SplFileInfo $a, \SplFileInfo $b) { return (string) $a > (string) $b ? 1 : -1; }); foreach ($files as $file) { if (!$file->isFile() || 'Event.php' !== substr($file->getFilename(), -9)) { continue; } if ($class = $this->findClass($file)) { $refl = new \ReflectionClass($class); if ($refl->isAbstract()) { continue; } if (!$refl->getParentClass() || $refl->getParentClass()->getName() != 'Symfony\\Component\\EventDispatcher\\Event') { continue; } $map->merge($this->loader->load($class, $type)); } } return $map; }
public function merge(EventsMap $map) { $this->resources = array_merge($this->resources, $map->getResources()); foreach ($map->getEventMaps() as $name => $eventMap) { $this->addEventMap($eventMap); } }
/** * 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); $map = new EventsMap(); if ($class = $this->findClass($path)) { $map->addResource(new FileResource($path)); $map->merge($this->loader->load($class, $type)); } return $map; }
private function loadMap() { 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()) { $map = new EventsMap(); foreach ($this->options['bundles'] as $bundle) { $refl = new \ReflectionClass($bundle); $dir = dirname($refl->getFileName()); if (file_exists($dir) && is_dir($dir)) { $map->merge($this->loader->load($dir)); } } $dumper = new $this->options['dumper_class'](); $cache->write($dumper->dump($map), $map->getResources()); } $this->map = (include $cache); }
/** * Loads from annotations from a class. * * @param string $class A class name * @param string $type The resource type * * @return EventsMap A event map * * @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)); } $map = new EventsMap(); $map->addResource(new FileResource($class->getFileName())); $eventMap = null; foreach ($this->reader->getClassAnnotations($class) as $annot) { if ($annot instanceof $this->annotationClassAction) { $eventMap = new EventMap($annot->getName(), $class->getName(), $this->annotationClassAction); continue; } if ($annot instanceof $this->annotationClassCause) { $eventMap = new EventMap($annot->getName(), $class->getName(), $this->annotationClassCause); continue; } } if ($eventMap) { $getters = array(); $setters = array(); foreach ($class->getMethods() as $method) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { if ($annot instanceof $this->annotationMethodGetter) { $getter = array('type' => $annot->getType(), 'data' => $annot->isData()); if ($annot->getField()) { $getter['field'] = $annot->getField(); } else { $field = $method->getName(); if (strncmp($field, 'get', 3) === 0 && strlen($field) > 3) { $field = lcfirst(substr($field, 3)); } elseif (strncmp($field, 'is', 2) === 0 && strlen($field) > 2) { $field = lcfirst(substr($field, 2)); } $getter['field'] = $field; } $getters[$method->getName()] = $getter; } if ($annot instanceof $this->annotationMethodSetter) { $setter = array('required' => $annot->isRequired(), 'type' => $annot->getType()); if ($annot->getField()) { $setter['field'] = $annot->getField(); } else { $field = $method->getName(); if (strncmp($field, 'set', 3) === 0 && strlen($field) > 3) { $field = lcfirst(substr($field, 3)); } $setter['field'] = $field; } $setters[$method->getName()] = $setter; } } } $eventMap->setGetters($getters); $eventMap->setSetters($setters); $map->addEventMap($eventMap); } return $map; }