/** * Notes de Marc : onFilterController est un membre de classe qui est automatiquement * appelé par le moteur symfony2 (la classe doit avoir été déclarée en Service et doit être * instancié dans le Container de symfony) en amont de chaque traitement de requête * Dans ce cas particulier, le filtre va employer un autre Service, déclaré dans * 'GaiaService.php' pour tester la présence de certains codes qui sont la preuve de * l'autorisation de l'originaire de la requête. * @param FilterControllerEvent $event * @return type * @throws \Exception */ public function onFilterController(FilterControllerEvent $event) { // Marc inserted this to test if it's being used throw new \Exception('SUCCES : Nous sommes dans onFilterController !', 8701); //Return if no controller if (!is_array($controller = $event->getController())) { return; } list($object, $method) = $controller; // the controller could be a proxy, e.g. when using the JMSSecuriyExtraBundle or JMSDiExtraBundle $className = ClassUtils::getClass($object); $reflectionClass = new \ReflectionClass($className); $reflectionMethod = $reflectionClass->getMethod($method); $allAnnotations = $this->reader->getMethodAnnotations($reflectionMethod); $gaiaAnnotations = array_filter($allAnnotations, function ($annotation) { return $annotation instanceof RequiresGaia; }); foreach ($gaiaAnnotations as $gaiaAnnotation) { // ... verify // throw an exception, catch it using an exception listener // then in the exception listener, create a RedirectResponse or something // and set it on the $event object $gaia = $this->gaia->getGaiaValues('gaia'); if (strlen($gaia) === 0) { throw new \Exception('GAIA non détecté !', 8701); } if ($gaiaAnnotation->value === 'admin' && $this->security->isAdmin() === false || $gaiaAnnotation->value === 'admin_app' && $this->security->isAdminOrAdminApp() === false) { throw new \Exception("Vous n'avez pas les droits nécessaires !", 8702); } } }
/** * @group performance */ public function testCachedReadPerformanceWithFileCache() { $method = $this->getMethod(); // prime cache $reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir()); $reader->getMethodAnnotations($method); $time = microtime(true); for ($i = 0, $c = 500; $i < $c; $i++) { $reader = new FileCacheReader(new AnnotationReader(), sys_get_temp_dir()); $reader->getMethodAnnotations($method); clearstatcache(); } $time = microtime(true) - $time; $this->printResults('cached reader (file)', $time, $c); }
/** * @param ReflectionClass $class * @return array */ public function read(ReflectionClass $class) { /** * @var DrupalCommand $definition */ $definitions = $this->reader->getClassAnnotations($class); $dependencies = []; foreach ($definitions as $definition) { if ($definition instanceof DrupalCommand) { foreach ($definition->dependencies as $dependency) { $dependencies[] = $dependency; } } } return $dependencies; }
/** * Listen kernel.controller event for find assigned annotation * * @param FilterControllerEvent $event * @return null */ public function onKernelController(FilterControllerEvent $event) { $controller = $event->getController(); if (!is_array($controller)) { return; } $action = new ReflectionMethod($controller[0], $controller[1]); $annotation = $this->annotationReader->getMethodAnnotation($action, $this->annotationClass); if (get_class($annotation) != $this->annotationClass) { return; } $newController = $this->resolver->resolve($annotation); if (!empty($newController)) { $event->setController($newController); } }
public function addPlugin(PluginModule $pluginModule, $plugin_service_id) { $pluginModule->setServiceId($plugin_service_id); $name = $pluginModule->getName(); $rc1 = new \ReflectionObject($pluginModule); $parent = $rc1->getParentClass(); if (!$parent || self::PM_BASE_CLASS === $parent->getName() || !is_subclass_of($pluginModule, self::PM_BASE_CLASS)) { throw new \Exception(sprintf("sf.bbs.plugin.module invalid name(%s) on class(%s) file(%s) must extends %s\\*", $name, $rc1->getName(), $rc1->getFileName(), self::PM_BASE_CLASS)); } if (!preg_match('/^[a-z][a-z0-9\\_]+$/', $name)) { throw new \Exception(sprintf("sf.bbs.plugin.module invalid name(%s) on class(%s) file(%s)", $name, $rc1->getName(), $rc1->getFileName())); } if (isset($this->_modules[$name])) { $rc2 = new \ReflectionObject($this->_modules[$name]); throw new \Exception(sprintf("sf.bbs.plugin.module(%s) duplicate for class(%s,%s), file(%s,%s)", $name, $rc1->getName(), $rc2->getName(), $rc1->getFileName(), $rc2->getFileName())); } $type = $pluginModule->getType(); if ($type && !isset($this->_plugin_modules_types[$type])) { throw new \Exception(sprintf("sf.bbs.plugin.module(name: %s,\n class: %s,\n file: %s) \n type(%s) is invalid, available values ( %s ) ", $name, $rc1->getName(), $rc1->getFileName(), json_encode($type), join(', ', array_keys($this->_plugin_modules_types)))); } /** * @var $m \ReflectionMethod */ foreach ($rc1->getMethods() as $m) { $as = $this->_reader->getMethodAnnotations($m); $_as = array(); foreach ($as as $_an) { if ($_an instanceof \Symforce\DiscuzBundle\Annotation\AbstractAnnotation) { $_an_class = get_class($_an); if ($_an instanceof \Symforce\DiscuzBundle\Annotation\AbstractMultiple) { $key = $this->getMultipleKeyValue(); if (isset($_as[$_an_class][$key])) { throw new \Exception(sprintf("class:%s method:%s, file: %s, line:%s annotations(%s) duplicate(%s)", $rc1->getName(), $m->getName(), $rc1->getFileName(), $m->getStartLine(), $_an_class, $key)); } $_as[$_an_class][$key] = $_an; } else { if (isset($_as[$_an_class])) { throw new \Exception(sprintf("class:%s method:%s, file: %s, line:%s annotations(%s) duplicate", $rc1->getName(), $m->getName(), $rc1->getFileName(), $m->getStartLine(), $_an_class)); } $_as[$_an_class] = $_an; } } } if (empty($_as)) { continue; } foreach ($_as as $_an) { $_an->checkMethod($m); } if (isset($_as[self::PM_ANNOTATION_ACTION])) { $pluginModule->addAction($_as[self::PM_ANNOTATION_ACTION], $m); } if (isset($_as[self::PM_ANNOTATION_EMBED])) { $_an = $_as[self::PM_ANNOTATION_EMBED]; if ($_an instanceof \Symforce\DiscuzBundle\Annotation\Embed) { $_embed_helper = new EmbedHelper($_an, $m, $pluginModule); if (isset($this->_embed_helpers[$_an->type][$_an->method])) { $_other_embed_helper = $this->_embed_helpers[$_an->type][$_an->method][0]; if ($_embed_helper->code !== $_other_embed_helper->code) { throw new \Exception(sprintf("@(%s, method=%s) parameters conflict(%s, %s)", $_an_class, $_an->method, \Dev::getMethodDeclaring($m), \Dev::getMethodDeclaring($_other_embed_helper->method))); } } $this->_embed_helpers[$_an->type][$_an->method][] = $_embed_helper; } } } $bundle_name = preg_replace('/^(\\/.+)\\/([A-Z]\\w+)\\/([A-Z]\\w+Bundle)\\/.+$/', '\\2\\3', $rc1->getFileName()); $pluginModule->setBundleName($bundle_name); $this->_modules[$name] = $pluginModule; $pluginModule->setContainer($this->_container); $pluginModule->onLoad($this); }