/**
  * @return MessageCatalogue
  * @throws \Exception
  */
 public function extract()
 {
     if (!empty($this->removingTwigVisitor)) {
         $this->removingTwigVisitor->setEnabled(false);
     }
     if (!empty($this->defaultApplyingTwigVisitor)) {
         $this->defaultApplyingTwigVisitor->setEnabled(false);
     }
     $finder = Finder::create()->in($this->directory);
     foreach ($this->excludedDirs as $dir) {
         $finder->exclude($dir);
     }
     foreach ($this->excludedNames as $name) {
         $finder->notName($name);
     }
     if ($this->pattern) {
         $finder->name($this->pattern);
     }
     $curTwigLoader = $this->twig->getLoader();
     $this->twig->setLoader(new \Twig_Loader_String());
     try {
         $catalogue = new MessageCatalogue();
         foreach ($finder as $file) {
             $visitingMethod = 'visitFile';
             $visitingArgs = array($file, $catalogue);
             $this->logger->debug(sprintf('Parsing file "%s"', $file));
             if (false !== ($pos = strrpos($file, '.'))) {
                 $extension = substr($file, $pos + 1);
                 if ('php' === $extension) {
                     try {
                         $ast = $this->phpParser->parse(file_get_contents($file));
                     } catch (Error $ex) {
                         throw new \RuntimeException(sprintf('Could not parse "%s": %s', $file, $ex->getMessage()), $ex->getCode(), $ex);
                     }
                     $visitingMethod = 'visitPhpFile';
                     $visitingArgs[] = $ast;
                 } elseif ('twig' === $extension) {
                     $visitingMethod = 'visitTwigFile';
                     $visitingArgs[] = $this->twig->parse($this->twig->tokenize(file_get_contents($file), (string) $file));
                 }
             }
             foreach ($this->visitors as $visitor) {
                 call_user_func_array(array($visitor, $visitingMethod), $visitingArgs);
             }
         }
         if (null !== $curTwigLoader) {
             $this->twig->setLoader($curTwigLoader);
         }
         if (!empty($this->removingTwigVisitor)) {
             $this->removingTwigVisitor->setEnabled(true);
         }
         if (!empty($this->defaultApplyingTwigVisitor)) {
             $this->defaultApplyingTwigVisitor->setEnabled(true);
         }
         return $catalogue;
     } catch (\Exception $ex) {
         if (null !== $curTwigLoader) {
             $this->twig->setLoader($curTwigLoader);
         }
         throw $ex;
     }
 }