Ejemplo n.º 1
0
 protected static function registerAnnotationLoader(array $namespaces)
 {
     AnnotationRegistry::reset();
     AnnotationRegistry::registerLoader(function ($class) use($namespaces) {
         foreach ($namespaces as $namespace) {
             if (strpos($class, $namespace) === 0) {
                 spl_autoload_call($class);
                 return class_exists($class, false);
             }
         }
     });
 }
Ejemplo n.º 2
0
 /**
  * Test that calling boot() registers our annotation in the AnnotationRegistry.
  *
  * @return void
  */
 public function testBootRegistersAnnotationInLoader()
 {
     $bundle = new TensideCoreBundle();
     AnnotationRegistry::reset();
     $this->assertFalse(AnnotationRegistry::loadAnnotationClass('Tenside\\CoreBundle\\Annotation\\ApiDescription'));
     $this->assertFalse(class_exists('Tenside\\CoreBundle\\Annotation\\ApiDescription', false));
     $bundle->boot();
     $this->assertFalse(AnnotationRegistry::loadAnnotationClass('NonExistant\\Annotation'));
     // Ensure the class does not get loaded by requiring another annotation.
     $this->assertFalse(class_exists('Tenside\\CoreBundle\\Annotation\\ApiDescription', false));
     $this->assertTrue(AnnotationRegistry::loadAnnotationClass('Tenside\\CoreBundle\\Annotation\\ApiDescription'));
     $this->assertTrue(class_exists('Tenside\\CoreBundle\\Annotation\\ApiDescription', false));
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     $definitions = array();
     $reader = $this->getAnnotationReader();
     // Clear the annotation loaders of any previous annotation classes.
     AnnotationRegistry::reset();
     // Register the namespaces of classes that can be used for annotations.
     AnnotationRegistry::registerLoader('class_exists');
     // Search for classes within all PSR-0 namespace locations.
     foreach ($this->getPluginNamespaces() as $namespace => $dirs) {
         foreach ($dirs as $dir) {
             if (file_exists($dir)) {
                 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS));
                 foreach ($iterator as $fileinfo) {
                     if ($fileinfo->getExtension() == 'php') {
                         $sub_path = $iterator->getSubIterator()->getSubPath();
                         $sub_path = $sub_path ? str_replace('/', '\\', $sub_path) . '\\' : '';
                         $class = $namespace . '\\' . str_replace('/', '\\', $this->pluginManagerDefinition['directory']) . '\\' . $sub_path . $fileinfo->getBasename('.php');
                         // The filename is already known, so there is no need to find the
                         // file. However, StaticReflectionParser needs a finder, so use a
                         // mock version.
                         $finder = MockFileFinder::create($fileinfo->getPathName());
                         $parser = new StaticReflectionParser($class, $finder, TRUE);
                         if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
                             $this->prepareAnnotationDefinition($annotation, $class);
                             $definitions[$annotation->getId()] = $annotation->get();
                         }
                     }
                 }
             }
         }
     }
     // Don't let annotation loaders pile up.
     AnnotationRegistry::reset();
     return $definitions;
 }
 /**
  * {@inheritdoc}
  */
 public function getDefinitions()
 {
     $definitions = array();
     $reader = $this->getAnnotationReader();
     // Clear the annotation loaders of any previous annotation classes.
     AnnotationRegistry::reset();
     // Register the namespaces of classes that can be used for annotations.
     AnnotationRegistry::registerLoader('class_exists');
     // Search for classes within all PSR-0 namespace locations.
     foreach ($this->getPluginNamespaces() as $namespace => $dirs) {
         foreach ($dirs as $dir) {
             if (file_exists($dir)) {
                 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS));
                 foreach ($iterator as $fileinfo) {
                     if ($fileinfo->getExtension() == 'php') {
                         if ($cached = $this->fileCache->get($fileinfo->getPathName())) {
                             if (isset($cached['id'])) {
                                 // Explicitly unserialize this to create a new object instance.
                                 $definitions[$cached['id']] = unserialize($cached['content']);
                             }
                             continue;
                         }
                         $sub_path = $iterator->getSubIterator()->getSubPath();
                         $sub_path = $sub_path ? str_replace(DIRECTORY_SEPARATOR, '\\', $sub_path) . '\\' : '';
                         $class = $namespace . '\\' . $sub_path . $fileinfo->getBasename('.php');
                         // The filename is already known, so there is no need to find the
                         // file. However, StaticReflectionParser needs a finder, so use a
                         // mock version.
                         $finder = MockFileFinder::create($fileinfo->getPathName());
                         $parser = new BaseStaticReflectionParser($class, $finder, FALSE);
                         /** @var $annotation \Drupal\Component\Annotation\AnnotationInterface */
                         if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
                             $this->prepareAnnotationDefinition($annotation, $class, $parser);
                             $id = $annotation->getId();
                             $content = $annotation->get();
                             $definitions[$id] = $content;
                             // Explicitly serialize this to create a new object instance.
                             $this->fileCache->set($fileinfo->getPathName(), ['id' => $id, 'content' => serialize($content)]);
                         } else {
                             // Store a NULL object, so the file is not reparsed again.
                             $this->fileCache->set($fileinfo->getPathName(), [NULL]);
                         }
                     }
                 }
             }
         }
     }
     // Don't let annotation loaders pile up.
     AnnotationRegistry::reset();
     return $definitions;
 }
Ejemplo n.º 5
0
 private function registerCommands()
 {
     if ($this->container->hasParameter('drupal.commands')) {
         $consoleCommands = $this->container->getParameter('drupal.commands');
     } else {
         $consoleCommands = array_keys($this->container->findTaggedServiceIds('drupal.command'));
         $this->container->setParameter('console.warning', 'application.site.errors.settings');
     }
     //        // @TODO add auto-discovery of chain files
     //        $chainCommands['create:bulk:data'] = [
     //            'file' => '/Users/jmolivas/.console/chain/create-data.yml'
     //        ];
     //
     //        foreach ($chainCommands as $name => $chainCommand) {
     //            $file = $chainCommand['file'];
     //            $command = new ChainRegister($name, $file);
     //            $this->add($command);
     //        }
     $serviceDefinitions = [];
     $annotationValidator = null;
     if ($this->container->hasParameter('console.service_definitions')) {
         $serviceDefinitions = $this->container->getParameter('console.service_definitions');
         /**
          * @var AnnotationValidator $annotationValidator
          */
         $annotationValidator = $this->container->get('console.annotation_validator');
     }
     $aliases = $this->container->get('console.configuration_manager')->getConfiguration()->get('application.commands.aliases') ?: [];
     foreach ($consoleCommands as $name) {
         // Some commands call AnnotationRegistry::reset,
         // we need to ensure the AnnotationRegistry is correctly defined.
         AnnotationRegistry::reset();
         AnnotationRegistry::registerLoader([\Drupal::service('class_loader'), "loadClass"]);
         if (!$this->container->has($name)) {
             continue;
         }
         if ($annotationValidator) {
             if (!($serviceDefinition = $serviceDefinitions[$name])) {
                 continue;
             }
             if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) {
                 continue;
             }
         }
         try {
             $command = $this->container->get($name);
         } catch (\Exception $e) {
             continue;
         }
         if (!$command) {
             continue;
         }
         if (method_exists($command, 'setTranslator')) {
             $command->setTranslator($this->container->get('console.translator_manager'));
         }
         if (method_exists($command, 'setContainer')) {
             $command->setContainer($this->container->get('service_container'));
         }
         if (array_key_exists($command->getName(), $aliases)) {
             $commandAliases = $aliases[$command->getName()];
             if (!is_array($commandAliases)) {
                 $commandAliases = [$commandAliases];
             }
             $command->setAliases($commandAliases);
         }
         $this->add($command);
     }
 }