本文整理汇总了PHP中PhpParser\NodeTraverser::removeVisitor方法的典型用法代码示例。如果您正苦于以下问题:PHP NodeTraverser::removeVisitor方法的具体用法?PHP NodeTraverser::removeVisitor怎么用?PHP NodeTraverser::removeVisitor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpParser\NodeTraverser
的用法示例。
在下文中一共展示了NodeTraverser::removeVisitor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: unregisterExtension
/**
* Unregisters an extension from the engine
*
* @param ExtensionInterface $extension Extension to unregister
*/
public static function unregisterExtension(ExtensionInterface $extension)
{
self::$traverser->removeVisitor($extension);
}
示例2: applyMagic
/**
* Perform the analysis for special properties and write them to the files.
*
* Use addSource() before calling this.
*
* @return int Number of source files changed (does not necessarily mean they
* have been actually modified in case dryRun if true)
*/
public function applyMagic()
{
$sourcesChanged = 0;
# start processing
$traverser = new NodeTraverser();
$classVisitor = new ClassVisitor();
$traverser->addVisitor($classVisitor);
# Store which class is in which file and whose parent it has
$classes = [];
foreach ($this->files as $file) {
$this->logger->info('Parsing ' . $file->getRealPath());
$code = file($file);
$parser = new Parser(new Lexer());
$tree = $parser->parse(join('', $code));
$traverser->traverse($tree);
$classesInFile = $classVisitor->getClasses();
foreach ($classesInFile as $class) {
# A class without an parent cannot be any CakePHP managed class
if (!isset($class->extends)) {
continue;
}
if (isset($classes[$class->name])) {
$this->logger->error(sprintf('Ignoring class definition of %s found in file %s:%d, first' . ' found in %s:%d', $class->name, $file, $class->getAttribute('startLine'), $classes[$class->name]['file'], $classes[$class->name]['class']->getAttribute('startLine')));
continue;
}
# Remember for next pass
$classes[$class->name] = ['class' => $class, 'file' => $file->getRealPath(), 'code' => $code];
}
}
$traverser->removeVisitor($classVisitor);
$propertyVisitor = new PropertyVisitor();
$propertyVisitor->setProperties($this->getProperties());
$traverser->addVisitor($propertyVisitor);
if (empty($classes)) {
$this->logger->warning('No classes found');
return $sourcesChanged;
}
$fnFindTopAncestor = function ($className) use($classes) {
while (isset($classes[$className])) {
$className = $classes[$className]['class']->extends->toString();
}
return $className;
};
foreach ($classes as $className => $classData) {
$fileName = $classData['file'];
$transformedSource = NULL;
$topAncestor = $fnFindTopAncestor($className);
if (!isset($this->classToPropertySymbolTransform[$topAncestor])) {
$this->logger->info("Ignoring {$fileName} , not a recognized class");
continue;
}
$traverser->traverse([$classData['class']]);
$transformedSource = ClassTransformer::apply($classData['code'], $propertyVisitor->getClasses(), $this->classToPropertySymbolTransform[$topAncestor], $this->removeUnknownProperties);
if ($classData['code'] === $transformedSource) {
continue;
}
$sourcesChanged++;
if ($this->dryRun) {
$this->logger->info('Dry-run, not writing changes to ' . $fileName);
continue;
}
$this->logger->info('Writing changes to ' . $fileName);
file_put_contents($fileName, $transformedSource);
}
return $sourcesChanged;
}