/** * Executes a foreach expression. * * The expression has the form: 'foo as bar', where foo is an array and bar the value. * * @param string $expression foreach expression * @param scope $scope * * @throws ParsingException * @return Scope[] */ public function execute($expression, Scope $scope) { $metaModel = new MetaModel(); $metaModel->addContainer($scope); $subScopes = []; $parseResult = $this->forEachParser->parse($expression); if (is_array($parseResult['array'])) { $functionName = $scope->getFunction($parseResult['array']['functionName']); $array = call_user_func_array($functionName, array_map(function ($parameter) use($metaModel) { return $metaModel->run($parameter); }, $parseResult['array']['parameters'])); } else { $array = $metaModel->run($parseResult['array']); } foreach ($array as $key => $value) { // New sub-scope $subScope = new Scope($scope); $subScope->bind($parseResult['value'], $value); if (isset($parseResult['key'])) { $subScope->bind($parseResult['key'], $key); } $subScopes[] = $subScope; } return $subScopes; }
public function testMethodCallRecursive() { $article = new Article(1); $category = new Category(1); $category->addArticle($article); $article->setCategory($category); $this->em->persist($article); $this->em->persist($category); $this->em->flush(); $result = $this->metaModel->run('FunctionalTest\\MetaModel\\Fixture\\Article(1).getCategory().getId()'); $this->assertSame(1, $result); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->getHelperSet()->set(new ShellHelper()); /** @var ShellHelper $shell */ $shell = $this->getHelperSet()->get('shell'); /** @var FormatterHelper $formatter */ $formatter = $this->getHelperSet()->get('formatter'); $dumpFilters = [new DoctrineCollectionFilter(), new DoctrineProxyFilter()]; $output->writeln("<info>Welcome in the MetaConsole, type ? for help</info>"); $commandHistory = []; $searchHistory = function ($command, $start = null, $backward = true) use(&$commandHistory) { if ($backward) { if ($start === null || $start < 0 || $start >= count($commandHistory)) { $start = count($commandHistory) - 1; } else { $start = $start - 1; } for ($i = $start; $i >= 0; $i--) { $oldCommand = $commandHistory[$i]; if ($command == '' || strpos($oldCommand, $command) === 0) { return ['position' => $i, 'command' => $oldCommand]; } } return null; } else { if ($start === null || $start < 0 || $start >= count($commandHistory)) { return null; } else { $start = $start + 1; } for ($i = $start; $i < count($commandHistory); $i++) { $oldCommand = $commandHistory[$i]; if ($command == '' || strpos($oldCommand, $command) === 0) { return ['position' => $i, 'command' => $oldCommand]; } } return null; } }; while (true) { $output->writeln(""); try { $expression = $shell->prompt($output, '>>> ', null, $searchHistory); // Add to history $commandHistory[] = $expression; } catch (\RuntimeException $e) { // End of file return 0; } // Exit if ($expression === 0 || $expression == 'quit' || $expression == 'exit') { return 0; } if ($expression == '') { continue; } // Help if ($expression == '?' || $expression == 'help') { $this->showHelp($output, $formatter); continue; } try { $result = $this->metaModel->run($expression); echo NumberTwo::dump($result, 2, $dumpFilters) . PHP_EOL; } catch (\Exception $e) { $block = [get_class($e), $e->getMessage()]; $output->writeln($formatter->formatBlock($block, 'error')); } } return 0; }