/** * @param Node\Scalar $scalar * @return CompiledExpression */ public function compile(Node\Scalar $scalar) { try { $this->eventManager->fire(Event\ScalarBeforeCompile::EVENT_NAME, new Event\ScalarBeforeCompile($scalar, $this->context)); return $this->factory($scalar); } catch (\Exception $e) { $this->context->debug('ScalarCompiler is not implemented for ' . get_class($scalar)); } }
/** * @return \PHPSA\Context */ protected function getContext() { /** @var \PHPSA\Context $context */ $context = $this->getMock('\\PHPSA\\Context', array('notice'), array(new ConsoleOutput(), new Application(), EventManager::getInstance())); $context->setScope(new ClassDefinition('MathTest', null, 0)); return $context; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(''); if (extension_loaded('xdebug')) { /** * This will disable only showing stack traces on error conditions. */ if (function_exists('xdebug_disable')) { xdebug_disable(); } $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); } $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos']])); /** @var Application $application */ $application = $this->getApplication(); $application->compiler = new Compiler(); $em = EventManager::getInstance(); $context = new Context($output, $application, $em); $fileParser = new FileParser($parser, $this->getCompiler()); $path = $input->getArgument('path'); if (is_dir($path)) { $directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)); $output->writeln('Scanning directory <info>' . $path . '</info>'); $count = 0; /** @var SplFileInfo $file */ foreach ($directoryIterator as $file) { if ($file->getExtension() !== 'php') { continue; } $context->debug($file->getPathname()); $count++; } $output->writeln("Found <info>{$count} files</info>"); if ($count > 100) { $output->writeln('<comment>Caution: You are trying to scan a lot of files; this might be slow. For bigger libraries, consider setting up a dedicated platform or using ci.lowl.io.</comment>'); } $output->writeln(''); /** @var SplFileInfo $file */ foreach ($directoryIterator as $file) { if ($file->getExtension() !== 'php') { continue; } $fileParser->parserFile($file->getPathname(), $context); } } elseif (is_file($path)) { $fileParser->parserFile($path, $context); } /** * Step 2 Recursive check ... */ $application->compiler->compile($context); }
/** * @param object|string $expr * @throws InvalidArgumentException when $expr is not string/object/null * @throws RuntimeException when compiler class does not return a CompiledExpression * @return CompiledExpression */ public function compile($expr) { if (is_string($expr)) { return new CompiledExpression(CompiledExpression::STRING, $expr); } if (is_null($expr)) { return new CompiledExpression(CompiledExpression::NULL); } if (!is_object($expr)) { throw new InvalidArgumentException('$expr must be string/object/null'); } if ($expr instanceof Node\Scalar) { $scalar = new \PHPSA\Compiler\Scalar($this->context, $this->eventManager); return $scalar->compile($expr); } $this->eventManager->fire(ExpressionBeforeCompile::EVENT_NAME, new ExpressionBeforeCompile($expr, $this->context)); $className = get_class($expr); switch ($className) { case Node\Arg::class: /** * @todo Better compile */ return $this->compile($expr->value); /** * Names */ /** * Names */ case Node\Name::class: return $this->getNodeName($expr); case Node\Name\FullyQualified::class: return $this->getFullyQualifiedNodeName($expr); } $expressionCompiler = $this->factory($expr); if (!$expressionCompiler) { $this->context->debug("Expression compiler is not implemented for {$className}"); return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); } $result = $expressionCompiler->pass($expr, $this->context); if (!$result instanceof CompiledExpression) { throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); } return $result; }
public function testEvenManager() { // Create event listener $callback = function (Event $event) { return $event->parameter * 10; }; $secondCallback = function (Event $event) { return $event->parameter * 20; }; // This listener should be executed last EventManager::getInstance()->listen('test.event')->handler($callback)->priority(200); // This listener should be executed first EventManager::getInstance()->listen('test.event')->handler($secondCallback)->priority(400); $results = EventManager::getInstance()->fire('test.event', ['parameter' => 2]); // Event results are returned in form of an array $this->assertEquals(40, $results[0]); $this->assertEquals(20, $results[1]); }
/** * @param string $analyzerName * @return EventManager * @throws \Webiny\Component\EventManager\EventManagerException */ protected function getEventManager($analyzerName) { if (!class_exists($analyzerName, true)) { throw new \InvalidArgumentException("Analyzer with name: {$analyzerName} doesnot exist"); } /** @var \PHPSA\Analyzer\Pass\Metadata $metaData */ $metaData = $analyzerName::getMetadata(); if (!$metaData->allowsPhpVersion(PHP_VERSION)) { parent::markTestSkipped(sprintf('We cannot tests %s with %s because PHP required version is %s', $analyzerName, PHP_VERSION, $metaData->getRequiredPhpVersion())); } $analyzerConfiguration = $metaData->getConfiguration(); $analyzerConfiguration->attribute('enabled', true); $config = [$analyzerName::getMetadata()->getConfiguration()]; $em = EventManager::getInstance(); $configuration = new Configuration([], $config); \PHPSA\Analyzer\Factory::factory($em, $configuration); return $em; }
/** * binds the listeners */ public function bind() { $this->eventManager->listen(Compiler\Event\ExpressionBeforeCompile::EVENT_NAME)->handler(new ExpressionListener($this->bindOnExpressions))->method('beforeCompile'); $this->eventManager->listen(Compiler\Event\StatementBeforeCompile::EVENT_NAME)->handler(new StatementListener($this->bindOnStatements))->method('beforeCompile'); $this->eventManager->listen(Compiler\Event\ScalarBeforeCompile::EVENT_NAME)->handler(new ScalarListener($this->bindOnScalars))->method('beforeCompile'); }