作者:pixel-cooker
项目:JMSTranslationBundl
protected final function getDocParser()
{
$docParser = new DocParser();
$docParser->setImports(array('desc' => 'JMS\\TranslationBundle\\Annotation\\Desc', 'meaning' => 'JMS\\TranslationBundle\\Annotation\\Meaning', 'ignore' => 'JMS\\TranslationBundle\\Annotation\\Ignore'));
$docParser->setIgnoreNotImportedAnnotations(true);
return $docParser;
}
作者:eltondia
项目:Relogi
public function testIssueWithNamespacesOrImports()
{
$docblock = "@Entity";
$parser = new DocParser();
$annots = $parser->parse($docblock);
$this->assertEquals(1, count($annots));
$this->assertInstanceOf("Entity", $annots[0]);
$this->assertEquals(1, count($annots));
}
作者:niki
项目:JMSTranslationBundl
private function extract($directory)
{
$twig = new \Twig_Environment();
$twig->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$twig->addExtension(new TranslationExtension($translator));
$docParser = new DocParser();
$docParser->setImports(array('desc' => 'JMS\\TranslationBundle\\Annotation\\Desc', 'meaning' => 'JMS\\TranslationBundle\\Annotation\\Meaning', 'ignore' => 'JMS\\TranslationBundle\\Annotation\\Ignore'));
$docParser->setIgnoreNotImportedAnnotations(true);
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$extractor = new FileExtractor($twig, new NullLogger(), array(new DefaultPhpFileExtractor($docParser), new TranslationContainerExtractor(), new TwigFileExtractor($twig), new ValidationExtractor($factory), new FormExtractor($docParser)));
$extractor->setDirectory($directory);
return $extractor->extract();
}
作者:dkplu
项目:reflection
public function scanForAnnotations(string $docBlock, $context, array $imports) : Annotations
{
// Hack to ensure an attempt to autoload an annotation class is made
AnnotationRegistry::registerLoader(function ($class) {
return (bool) class_exists($class);
});
$imports = array_combine(array_map(function ($string) {
return strtolower($string);
}, array_keys($imports)), array_values($imports));
$parser = new DocParser();
$parser->setIgnoreNotImportedAnnotations(true);
$parser->setImports($imports);
return new Annotations($parser->parse($docBlock, $context));
}
作者:nikom
项目:phpunit-arrang
/**
* @return AnnotationReader
*/
public static function getAnnotationReader()
{
if (self::$annotationReader !== null) {
return self::$annotationReader;
}
//For old AnnotationReader (<=1.2.7)
//For new (>1.2.7) version of AnnotationReader, we can give a DocParser since a3c2928912eeb5dc5678352f22c378173def16b6
$parser = new DocParser();
$parser->setIgnoreNotImportedAnnotations(true);
self::$annotationReader = new AnnotationReader($parser);
//For old version of AnnotationReader (<=1.2.7) , we have to specify manually all ignored annotations
foreach (self::$ignoredAnnotationNames as $ignoredAnnotationName) {
self::$annotationReader->addGlobalIgnoredName($ignoredAnnotationName);
}
return self::$annotationReader;
}
作者:niki
项目:JMSTranslationBundl
private function extract($file, FormExtractor $extractor = null)
{
if (!is_file($file = __DIR__ . '/Fixture/' . $file)) {
throw new RuntimeException(sprintf('The file "%s" does not exist.', $file));
}
$file = new \SplFileInfo($file);
if (null === $extractor) {
$docParser = new DocParser();
$docParser->setImports(array('desc' => 'JMS\\TranslationBundle\\Annotation\\Desc', 'meaning' => 'JMS\\TranslationBundle\\Annotation\\Meaning', 'ignore' => 'JMS\\TranslationBundle\\Annotation\\Ignore'));
$docParser->setIgnoreNotImportedAnnotations(true);
$extractor = new FormExtractor($docParser);
}
$lexer = new \PHPParser_Lexer(file_get_contents($file));
$parser = new \PHPParser_Parser();
$ast = $parser->parse($lexer);
$catalogue = new MessageCatalogue();
$extractor->visitPhpFile($file, $catalogue, $ast);
return $catalogue;
}
作者:dracon
项目:forked-php-orm-benchmar
/**
* @group performance
*/
public function testDocParsePerformance()
{
$imports = array('ignorephpdoc' => 'Annotations\\Annotation\\IgnorePhpDoc', 'ignoreannotation' => 'Annotations\\Annotation\\IgnoreAnnotation', 'route' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation\\Route', 'template' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation\\Template', '__NAMESPACE__' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures');
$ignored = array('access', 'author', 'copyright', 'deprecated', 'example', 'ignore', 'internal', 'link', 'see', 'since', 'tutorial', 'version', 'package', 'subpackage', 'name', 'global', 'param', 'return', 'staticvar', 'static', 'var', 'throws', 'inheritdoc');
$parser = new DocParser();
$method = $this->getMethod();
$methodComment = $method->getDocComment();
$classComment = $method->getDeclaringClass()->getDocComment();
$time = microtime(true);
for ($i = 0, $c = 200; $i < $c; $i++) {
$parser = new DocParser();
$parser->setImports($imports);
$parser->setIgnoredAnnotationNames($ignored);
$parser->parse($methodComment);
$parser->parse($classComment);
}
$time = microtime(true) - $time;
$this->printResults('doc-parser', $time, $c);
}
作者:AntoineLemair
项目:JMSTranslationBundl
private function extract($directory)
{
$twig = new \Twig_Environment();
$twig->addExtension(new SymfonyTranslationExtension($translator = new IdentityTranslator(new MessageSelector())));
$twig->addExtension(new TranslationExtension($translator));
$loader = new \Twig_Loader_Filesystem(realpath(__DIR__ . "/Fixture/SimpleTest/Resources/views/"));
$twig->setLoader($loader);
$docParser = new DocParser();
$docParser->setImports(array('desc' => 'JMS\\TranslationBundle\\Annotation\\Desc', 'meaning' => 'JMS\\TranslationBundle\\Annotation\\Meaning', 'ignore' => 'JMS\\TranslationBundle\\Annotation\\Ignore'));
$docParser->setIgnoreNotImportedAnnotations(true);
//use correct factory class depending on whether using Symfony 2 or 3
if (class_exists('Symfony\\Component\\Validator\\Mapping\\Factory\\LazyLoadingMetadataFactory')) {
$metadataFactoryClass = 'Symfony\\Component\\Validator\\Mapping\\Factory\\LazyLoadingMetadataFactory';
} else {
$metadataFactoryClass = 'Symfony\\Component\\Validator\\Mapping\\ClassMetadataFactory';
}
$factory = new $metadataFactoryClass(new AnnotationLoader(new AnnotationReader()));
$extractor = new FileExtractor($twig, new NullLogger(), array(new DefaultPhpFileExtractor($docParser), new TranslationContainerExtractor(), new TwigFileExtractor($twig), new ValidationExtractor($factory), new FormExtractor($docParser)));
$extractor->setDirectory($directory);
return $extractor->extract();
}
作者:luisbrit
项目:Phraseane
public function register(Application $app)
{
$app['translation-extractor.logger'] = $app->share(function (Application $app) {
return $app['monolog'];
});
$app['translation-extractor.doc-parser'] = $app->share(function () {
$parser = new DocParser();
$parser->addNamespace("JMS\\TranslationBundle\\Annotation");
return $parser;
});
$app['translation-extractor.node-visitors'] = $app->share(function (Application $app) {
return [new ConstraintExtractor($app), new ValidationExtractor($app['validator']->getMetadataFactory()), new DefaultPhpFileExtractor($app['translation-extractor.doc-parser']), new TwigFileExtractor($app['twig']), new FormExtractor($app['translation-extractor.doc-parser']), new HelpMessageExtractor($app['translation-extractor.doc-parser'])];
});
$app['translation-extractor.file-extractor'] = $app->share(function (Application $app) {
return new FileExtractor($app['twig'], $app['translation-extractor.logger'], $app['translation-extractor.node-visitors']);
});
$app['translation-extractor.extractor-manager'] = $app->share(function (Application $app) {
return new ExtractorManager($app['translation-extractor.file-extractor'], $app['translation-extractor.logger']);
});
$app['translation-extractor.writer'] = $app->share(function (Application $app) {
return new FileWriter($app['translation-extractor.writers']);
});
$app['translation-extractor.writers'] = $app->share(function () {
return ['po' => new SymfonyDumperAdapter(new PoFileDumper(), 'po'), 'xlf' => new XliffDumper()];
});
$app['translation-extractor.loader-manager'] = $app->share(function (Application $app) {
return new LoaderManager($app['translation-extractor.loaders']);
});
$app['translation-extractor.loaders'] = $app->share(function () {
return ['po' => new SymfonyLoaderAdapter(new PoFileLoader()), 'xlf' => new XliffLoader()];
});
$app['translation-extractor.updater'] = $app->share(function (Application $app) {
AnnotationRegistry::registerAutoloadNamespace('JMS\\TranslationBundle\\Annotation', $app['root.path'] . '/vendor/jms/translation-bundle');
return new Updater($app['translation-extractor.loader-manager'], $app['translation-extractor.extractor-manager'], $app['translation-extractor.logger'], $app['translation-extractor.writer']);
});
}
作者:willstor
项目:cm
protected function setUp()
{
$docParser = new DocParser();
$docParser->setImports(array('desc' => 'JMS\\TranslationBundle\\Annotation\\Desc', 'meaning' => 'JMS\\TranslationBundle\\Annotation\\Meaning', 'ignore' => 'JMS\\TranslationBundle\\Annotation\\Ignore'));
$docParser->setIgnoreNotImportedAnnotations(true);
$this->extractor = new FormExtractor($docParser);
}
作者:rfc148
项目:pade
public function createTestParser()
{
$parser = new DocParser();
$parser->setIgnoreNotImportedAnnotations(true);
$parser->setImports(array('name' => 'Doctrine\\Tests\\Common\\Annotations\\Name', '__NAMESPACE__' => 'Doctrine\\Tests\\Common\\Annotations'));
return $parser;
}
作者:korchas
项目:dokap
/**
* Dispatch annotations found in phpDoc with context given in second argument.
*
* @param string $phpDoc phpDoc
* @param mixed $context context
*/
private function processDocComment($phpDoc, $context = null)
{
$annotations = $this->docParser->parse($phpDoc);
foreach ($annotations as $annotation) {
$this->collection->dispatch(Events::TOKEN_PHP_ANNOTATION, new Context\PHP\Annotation($annotation, $context), $this->result);
}
}
作者:kaza
项目:foru
/**
* @param Node $node
* @return void
*/
public function enterNode(Node $node)
{
if (!$node instanceof Node\Expr\MethodCall || !is_string($node->name) || !in_array(strtolower($node->name), array_map('strtolower', array_keys($this->methodsToExtractFrom)))) {
$this->previousNode = $node;
return;
}
$ignore = false;
$desc = $meaning = null;
if (null !== ($docComment = $this->getDocCommentForNode($node))) {
if ($docComment instanceof Doc) {
$docComment = $docComment->getText();
}
foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
if ($annot instanceof Ignore) {
$ignore = true;
} elseif ($annot instanceof Desc) {
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
}
}
}
if (!$node->args[0]->value instanceof String_) {
if ($ignore) {
return;
}
$message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
$id = $node->args[0]->value->value;
$index = $this->methodsToExtractFrom[strtolower($node->name)];
if (isset($node->args[$index])) {
if (!$node->args[$index]->value instanceof String_) {
if ($ignore) {
return;
}
$message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
$domain = $node->args[$index]->value->value;
} else {
$domain = 'messages';
}
$message = new Message($id, $domain);
$message->setDesc($desc);
$message->setMeaning($meaning);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
}
作者:betes-curieuses-desig
项目:ElieJosiePhotographi
/**
* {@inheritDoc}
*/
public function getMethodAnnotations(ReflectionMethod $method)
{
$class = $method->getDeclaringClass();
$context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
$this->parser->setTarget(Target::TARGET_METHOD);
$this->parser->setImports($this->getMethodImports($method));
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
return $this->parser->parse($method->getDocComment(), $context);
}
作者:php-communit
项目:JMSTranslationBundl
/**
* @param $item
* @param null $domain
*/
private function parseItem($item, $domain = null)
{
// get doc comment
$ignore = false;
$desc = $meaning = $docComment = null;
if ($item->key) {
$docComment = $item->key->getDocComment();
}
if (!$docComment) {
$docComment = $item->value->getDocComment();
}
$docComment = is_object($docComment) ? $docComment->getText() : null;
if ($docComment) {
if ($docComment instanceof Doc) {
$docComment = $docComment->getText();
}
foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $item->value->getLine()) as $annot) {
if ($annot instanceof Ignore) {
$ignore = true;
} elseif ($annot instanceof Desc) {
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
}
}
}
// check if the value is explicitly set to false => e.g. for FormField that should be rendered without label
$ignore = $ignore || !$item->value instanceof Node\Scalar\String_ || $item->value->value == false;
if (!$item->value instanceof Node\Scalar\String_ && !$item->value instanceof Node\Scalar\LNumber) {
if ($ignore) {
return;
}
$message = sprintf('Unable to extract translation id for form label/title/placeholder from non-string values, but got "%s" in %s on line %d. Please refactor your code to pass a string, or add "/** @Ignore */".', get_class($item->value), $this->file, $item->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
if ($domain === false) {
// Don't translate when domain is `false`
return;
}
$source = $this->fileSourceFactory->create($this->file, $item->value->getLine());
$id = $item->value->value;
if (null === $domain) {
$this->defaultDomainMessages[] = array('id' => $id, 'source' => $source, 'desc' => $desc, 'meaning' => $meaning);
} else {
$this->addToCatalogue($id, $source, $domain, $desc, $meaning);
}
}
作者:rexzo
项目:swagger-ph
/**
* Use doctrine to parse the comment block and return the detected annotations.
*
* @param string $comment a T_DOC_COMMENT.
* @param Context $context
* @return array Annotations
*/
public function fromComment($comment, $context = null)
{
if ($context === null) {
$context = new Context(['comment' => $comment]);
} else {
$context->comment = $comment;
}
try {
self::$context = $context;
if ($context->is('annotations') === false) {
$context->annotations = [];
}
$comment = preg_replace_callback('/^[\\t ]*\\*[\\t ]+/m', function ($match) {
// Replace leading tabs with spaces.
// Workaround for http://www.doctrine-project.org/jira/browse/DCOM-255
return str_replace("\t", ' ', $match[0]);
}, $comment);
$annotations = $this->docParser->parse($comment, $context);
self::$context = null;
return $annotations;
} catch (Exception $e) {
self::$context = null;
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote($context, '/') . '\\.$/', $e->getMessage(), $matches)) {
$errorMessage = $matches[1];
$errorPos = $matches[2];
$atPos = strpos($comment, '@');
$context->line += substr_count($comment, "\n", 0, $atPos + $errorPos);
$lines = explode("\n", substr($comment, $atPos, $errorPos));
$context->character = strlen(array_pop($lines)) + 1;
// position starts at 0 character starts at 1
Logger::warning(new Exception($errorMessage . ' in ' . $context, $e->getCode(), $e));
} else {
Logger::warning($e);
}
return [];
}
}
作者:Nival
项目:VacantesJannaMotor
/**
* Collects parsing metadata for a given class.
*
* @param \ReflectionClass $class
*/
private function collectParsingMetadata(ReflectionClass $class)
{
$ignoredAnnotationNames = self::$globalIgnoredNames;
$annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
foreach ($annotations as $annotation) {
if ($annotation instanceof IgnoreAnnotation) {
foreach ($annotation->names as $annot) {
$ignoredAnnotationNames[$annot] = true;
}
}
}
$name = $class->getName();
$this->imports[$name] = array_merge(self::$globalImports, $this->phpParser->parseClass($class), array('__NAMESPACE__' => $class->getNamespaceName()));
$this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
}
作者:ezsystem
项目:ezpublish-kerne
/**
* @param Node $node
*/
public function enterNode(Node $node)
{
if (!$node instanceof Node\Expr\New_ || !is_string($node->class) || strtolower($node->class) !== 'validationerror') {
$this->previousNode = $node;
return;
}
$ignore = false;
$desc = $meaning = null;
if (null !== ($docComment = $this->getDocCommentForNode($node))) {
if ($docComment instanceof Doc) {
$docComment = $docComment->getText();
}
foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
if ($annot instanceof Ignore) {
$ignore = true;
} elseif ($annot instanceof Desc) {
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
}
}
}
if (!$node->args[0]->value instanceof String_) {
if ($ignore) {
return;
}
$message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
$message = new Message($node->args[0]->value->value, $this->defaultDomain);
$message->setDesc($desc);
$message->setMeaning($meaning);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
// plural
if ($node->args[1]->value instanceof String_) {
$message = new Message($node->args[1]->value->value, $this->defaultDomain);
$message->setDesc($desc);
$message->setMeaning($meaning);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
}
}
作者:christianblo
项目:codedoc
/**
* @param RefComment $comment
*
* @return Annotation[]
*/
private function parseComment(RefComment $comment = null)
{
if ($comment === null) {
return [];
}
$annotations = $this->docParser->parse($comment->text);
foreach ($annotations as $key => $annotation) {
if (!$annotation instanceof Annotation) {
unset($annotations[$key]);
continue;
}
if ($annotation instanceof ContentInterface) {
$content = $this->extractContent($comment->text, get_class($annotation));
$annotation->setContent($content);
}
}
return $annotations;
}
作者:Lazybi
项目:huis
/**
*
* @param Context $context
* @return AbstractAnnotation[]
*/
protected function parseContext($context)
{
try {
self::$context = $context;
$annotations = $this->docParser->parse($context->comment, $context);
self::$context = null;
} catch (\Exception $e) {
self::$context = null;
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote($context, '/') . '\\.$/', $e->getMessage(), $matches)) {
$errorMessage = $matches[1];
$errorPos = $matches[2];
$atPos = strpos($context->comment, '@');
$context->line += substr_count($context->comment, "\n", 0, $atPos + $errorPos);
$lines = explode("\n", substr($context->comment, $atPos, $errorPos));
$context->character = strlen(array_pop($lines)) + 1;
// position starts at 0 character starts at 1
Logger::warning(new \Exception($errorMessage . ' in ' . $context, $e->getCode(), $e));
} else {
Logger::warning($e);
}
return array();
}
foreach ($annotations as $annotation) {
foreach ($this->processors as $processor) {
$processor->process($annotation, $context);
}
if ($annotation instanceof AbstractAnnotation) {
if ($annotation->hasPartialId()) {
if ($this->hasPartial($annotation->_partialId)) {
Logger::notice('partial="' . $annotation->_partialId . '" is not unique. another was found in ' . $annotation->_context);
}
$this->setPartial($annotation->_partialId, $annotation);
} elseif ($annotation instanceof Resource) {
$this->resources[] = $annotation;
} elseif ($annotation instanceof Model) {
$this->models[] = $annotation;
}
}
}
return $annotations;
}