protected function log($message, $verbosity = 1) { if (!$this->output instanceof OutputInterface) { return $this; } if (1 == $verbosity && $this->output->isVerbose()) { $this->output->writeln($message); } if (2 == $verbosity && $this->output->isVeryVerbose()) { $this->output->writeln(' ' . $message); } return $this; }
/** * Runs an external process. * * @param OutputInterface $output An OutputInterface instance * @param string|array|Process $cmd An instance of Process or an array of arguments to escape and run or a command to run * @param string|null $error An error message that must be displayed if something went wrong * @param callable|null $callback A PHP callback to run whenever there is some * output available on STDOUT or STDERR * * @return Process The process that ran */ public function run(OutputInterface $output, $cmd, $error = null, $callback = null) { $formatter = $this->getHelperSet()->get('debug_formatter'); if (is_array($cmd)) { $process = ProcessBuilder::create($cmd)->getProcess(); } elseif ($cmd instanceof Process) { $process = $cmd; } else { $process = new Process($cmd); } if ($output->isVeryVerbose()) { $output->write($formatter->start(spl_object_hash($process), $process->getCommandLine())); } if ($output->isDebug()) { $callback = $this->wrapCallback($output, $process, $callback); } $process->run($callback); if ($output->isVeryVerbose()) { $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode()); $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful())); } if (!$process->isSuccessful() && null !== $error) { $output->writeln(sprintf('<error>%s</error>', $error)); } return $process; }
protected function execute(InputInterface $input, OutputInterface $output) { if ($this->installerIsUpdated()) { return; } try { $this->downloadNewVersion()->checkNewVersionIsValid()->backupCurrentVersion()->replaceCurrentVersionbyNewVersion()->cleanUp(); } catch (IOException $e) { throw new \RuntimeException(sprintf("The installer couldn't be updated, probably because of a permissions issue.\n" . "Try to execute the command again with super user privileges:\n" . " sudo %s\n", $this->getExecutedCommand())); if ($output->isVeryVerbose()) { echo $e->getMessage(); } } catch (\Exception $e) { $this->rollback(); if ($output->isVeryVerbose()) { echo $e->getMessage(); } } }
protected function initialize(InputInterface $input, OutputInterface $output) { $level = Reporter::VERBOSITY_ERROR; if ($output->isQuiet()) { $level = Reporter::VERBOSITY_NONE; } elseif ($output->isVeryVerbose()) { $level = Reporter::VERBOSITY_ALL; } $this->getContainer()->get('reporter')->setVerbosity($level); if ($input->getOption('diff')) { $this->diff = $input->getOption('diff'); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $destination = realpath($input->getArgument('moduledir')); $hooksToFind = $this->getHooks(); $ourCwd = getcwd(); chdir($destination); $modules = array(); $hooks = array(); // Loop through the hooks. foreach ($hooksToFind as $hook) { $commandOutput = ''; if ($output->isVerbose()) { $output->writeln($hook); } // We're just grepping the tree for each function. $command = "grep 'function .*_{$hook}(' * -Hnr"; if ($output->isVeryVerbose()) { $output->writeln($command); } exec($command, $commandOutput); // Iterate over the results, pulling out the name of the module. // Note that for sandboxes, this will produce duplicates and could // require further modifications. foreach ($commandOutput as $line) { list($file, ) = explode(':', $line); $just_the_name = explode('/', $file); $module_filename_parts = explode('.', end($just_the_name)); $module = reset($module_filename_parts); $hooks[$hook][] = $module; $modules[$module][] = $hook; } } chdir($ourCwd); // Write output. $fh = fopen($input->getOption('output-hook'), 'w'); foreach ($hooks as $hook => $these_hooks) { if (!empty($these_hooks)) { fputcsv($fh, array($hook, implode(';', $these_hooks))); } } fclose($fh); $fm = fopen($input->getOption('output-modules'), 'w'); foreach ($modules as $module => $these_hooks) { fputcsv($fm, array($module, implode(';', $these_hooks))); } fclose($fm); return 0; }
public function computeLogThreshold(OutputInterface $output) { if ($output->isDebug()) { return Logger::DEBUG; } if ($output->isVeryVerbose()) { return Logger::INFO; } if ($output->isVerbose()) { return Logger::INFO; } if ($output->isQuiet()) { return \PHP_INT_MAX; } return Logger::WARNING; }
/** * Deploy a static view file * * @param string $filePath * @param string $area * @param string $themePath * @param string $locale * @param string $module * @return void * @SuppressWarnings(PHPMD.NPathComplexity) */ private function deployFile($filePath, $area, $themePath, $locale, $module) { $requestedPath = $filePath; if (substr($filePath, -5) == '.less') { $requestedPath = preg_replace('/.less$/', '.css', $filePath); } $logMessage = "Processing file '$filePath' for area '$area', theme '$themePath', locale '$locale'"; if ($module) { $logMessage .= ", module '$module'"; } if ($this->output->isVeryVerbose()) { $this->output->writeln($logMessage); } try { $asset = $this->assetRepo->createAsset( $requestedPath, ['area' => $area, 'theme' => $themePath, 'locale' => $locale, 'module' => $module] ); $asset = $this->minifyService->getAssets([$asset], true)[0]; if ($this->output->isVeryVerbose()) { $this->output->writeln("\tDeploying the file to '{$asset->getPath()}'"); } else { $this->output->write('.'); } if ($this->isDryRun) { $asset->getContent(); } else { $this->assetPublisher->publish($asset); $this->bundleManager->addAsset($asset); } $this->count++; } catch (\Less_Exception_Compiler $e) { $this->verboseLog( "\tNotice: Could not parse LESS file '$filePath'. " . "This may indicate that the file is incomplete, but this is acceptable. " . "The file '$filePath' will be combined with another LESS file." ); $this->verboseLog("\tCompiler error: " . $e->getMessage()); } catch (\Exception $e) { $this->output->writeln($e->getMessage() . " ($logMessage)"); $this->verboseLog($e->getTraceAsString()); $this->errorCount++; } }
/** * Deploy a static view file * * @param string $filePath * @param string $area * @param string $themePath * @param string $locale * @param string $module * @param string|null $fullPath * @return string * @throws \InvalidArgumentException * @throws LocalizedException * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ private function deployFile($filePath, $area, $themePath, $locale, $module, $fullPath = null) { $compiledFile = ''; $extension = pathinfo($filePath, PATHINFO_EXTENSION); foreach ($this->alternativeSources as $name => $alternative) { if (in_array($extension, $alternative->getAlternativesExtensionsNames(), true) && strpos(basename($filePath), '_') !== 0) { $compiledFile = substr($filePath, 0, strlen($filePath) - strlen($extension) - 1); $compiledFile = $compiledFile . '.' . $name; } } if ($this->output->isVeryVerbose()) { $logMessage = "Processing file '{$filePath}' for area '{$area}', theme '{$themePath}', locale '{$locale}'"; if ($module) { $logMessage .= ", module '{$module}'"; } $this->output->writeln($logMessage); } try { $asset = $this->assetRepo->createAsset($filePath, ['area' => $area, 'theme' => $themePath, 'locale' => $locale, 'module' => $module]); if ($this->output->isVeryVerbose()) { $this->output->writeln("\tDeploying the file to '{$asset->getPath()}'"); } else { $this->output->write('.'); } if ($this->getOption(Options::DRY_RUN)) { $asset->getContent(); } else { $this->assetPublisher->publish($asset); $this->bundleManager->addAsset($asset); } $this->count++; } catch (ContentProcessorException $exception) { $pathInfo = $fullPath ?: $filePath; $errorMessage = __('Compilation from source: ') . $pathInfo . PHP_EOL . $exception->getMessage(); $this->errorCount++; $this->output->write(PHP_EOL . PHP_EOL . $errorMessage . PHP_EOL, true); $this->getLogger()->critical($errorMessage); } catch (\Exception $exception) { $this->output->write('.'); $this->verboseLog($exception->getTraceAsString()); $this->errorCount++; } return $compiledFile; }
/** * @param ProjectConfiguration $projectConfig * @param OutputInterface $output * * @return int Exit code */ protected function createRemoteVendorDir(ProjectConfiguration $projectConfig, OutputInterface $output) { if ($output->isVeryVerbose()) { $output->writeln(sprintf('<comment>Creates composer autoload file for use vendor dir "<info>%s</info>" for project "<info>%s</info>"</comment>', $projectConfig->getRemoteVendorDir(), $projectConfig->getProjectName())); } $filesystem = $this->getLocalFilesystem(); $filesystem->remove($projectConfig->getLocalWebappDir() . '/vendor'); $autoloadFilePath = $projectConfig->getLocalWebappDir() . '/vendor/autoload.php'; $autoloadContent = sprintf('<?php return require \'%s/autoload.php\';', $projectConfig->getRemoteVendorDir()); $filesystem->dumpFile($autoloadFilePath, $autoloadContent); if ($output->isVeryVerbose()) { $output->writeln(sprintf('<comment>Creates remote vendor dir for project "<info>%s</info>"</comment>', $projectConfig->getProjectName())); } $this->getSshExec()->exec(strtr('test -d %composer_vendor_dir% || mkdir -p %composer_vendor_dir%', ['%composer_vendor_dir%' => $projectConfig->getRemoteVendorDir()])); if (0 !== $this->getSshExec()->getLastReturnStatus()) { $this->getSshExec()->checkStatus($output); } return $this->getSshExec()->getLastReturnStatus(); }
/** * Parse the command line options concerning the date of the last update * * @param string $lastUpdatePath The path to the last update file * @param string $date|null The date command line argument * @param OutputInterface $output The command's output * @return void */ private function parseOptions($lastUpdatePath, $date, $output) { $message = null; if ($date) { $this->lastUpdateDate = \TimeDate::from($date)->startOfDay(); } elseif (!file_exists($lastUpdatePath)) { $message = "Last update file not found, a new one is going to be created"; } else { $message = $this->parseLastUpdate($lastUpdatePath); } if ($output->isVeryVerbose()) { $output->writeln($message); if ($this->lastUpdateDate) { $formattedDate = $this->lastUpdateDate->toFormattedDateString(); $output->writeln("Showing changes since <options=bold>{$formattedDate}</options=bold>"); } $output->writeln(""); } }
/** * Execute the "scan" command * * @param InputInterface $input Input object * @param OutputInterface $output Output object * @throws RuntimeException If output format is not valid * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { $dispatcher = new EventDispatcher(); $exitCode = new ExitCodeCatcher(); $dispatcher->addSubscriber($exitCode); $fileIterator = new FileIterator($input->getArgument('path'), $this->parseCsv($input->getOption('ignore-paths')), $this->parseCsv($input->getOption('extensions'))); $format = strtolower($input->getOption('format')); switch ($format) { case 'dots': case 'progress': $output->writeln("<info>Parse: A PHP Security Scanner</info>\n"); if ($output->isVeryVerbose()) { $dispatcher->addSubscriber(new ConsoleDebug($output)); } elseif ($output->isVerbose()) { $dispatcher->addSubscriber(new ConsoleLines($output)); } elseif ('progress' == $format && $output->isDecorated()) { $dispatcher->addSubscriber(new ConsoleProgressBar(new ProgressBar($output, count($fileIterator)))); } else { $dispatcher->addSubscriber(new ConsoleDots($output)); } $dispatcher->addSubscriber(new ConsoleReport($output)); break; case 'xml': $dispatcher->addSubscriber(new Xml($output)); break; default: throw new RuntimeException("Unknown output format '{$input->getOption('format')}'"); } $ruleFactory = new RuleFactory($this->parseCsv($input->getOption('whitelist-rules')), $this->parseCsv($input->getOption('blacklist-rules'))); $ruleCollection = $ruleFactory->createRuleCollection(); $ruleNames = implode(',', array_map(function (RuleInterface $rule) { return $rule->getName(); }, $ruleCollection->toArray())); $dispatcher->dispatch(Events::DEBUG, new MessageEvent("Using ruleset {$ruleNames}")); $docCommentFactory = new DocCommentFactory(); $scanner = new Scanner($dispatcher, new CallbackVisitor($ruleCollection, $docCommentFactory, !$input->getOption('disable-annotations'))); $scanner->scan($fileIterator); return $exitCode->getExitCode(); }
/** * Runs the current application. * * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * * @return integer 0 if everything went fine, or an error code */ public function doRun(InputInterface $input, OutputInterface $output) { //potentially override the environment if ($input->hasParameterOption(array('--env', '-e'))) { $this->neptune->setEnv($input->getParameterOption(array('--env', '-e'))); } if ($output->isVeryVerbose() && $this->neptune->getEnv()) { $output->writeln(sprintf('Using environment <info>%s</info>', $this->neptune->getEnv())); } //load the app configuration now to give a useful message if //it fails try { $this->neptune['config']; } catch (ConfigFileException $e) { $this->renderException($e, $output); $output->writeln('Run `<info>./vendor/bin/neptune-install .</info>` to set up a default configuration.'); return; } if (!$this->commands_registered) { $this->registerCommands($output); } return parent::doRun($input, $output); }
protected function setupListeners(EmitterInterface $emitter, OutputInterface $output) { if (!$output->isVerbose()) { return; } $emitter->addListener('git_remote.repository_discovery', function ($event) use($output) { /** @var GitRepositoryEvent $event */ $output->writeln(sprintf('Discovered repository <info>%s</info> at <comment>%s</comment>', $event->getRepository()->getName(), $event->getRepository()->getAnonymousUri())); if ($output->isVeryVerbose()) { $data = $event->getData(); $output->writeln(sprintf('Repository definition: %s', json_encode($data['definition'], JSON_PRETTY_PRINT))); } }); $emitter->addListener('git_guardian.pre_clone_repository', function ($event) use($output) { /** @var GitRepositoryEvent $event */ $data = $event->getData(); $output->write(sprintf('Cloning <info>%s</info> into <comment>%s</comment> ', $event->getRepository()->getName(), $data['path'])); }); $emitter->addListener('git_guardian.create_git', function ($event) use($output) { /** @var GitRepositoryEvent $event */ $output->write(sprintf('Preparing Git for <info>%s</info> in <comment>%s</comment> ', $event->getRepository()->getName(), $event->getGit()->getPath())); }); $emitter->addListener('git_guardian.exception_repository', function ($event) use($output) { /** @var GitRepositoryEvent $event */ $data = $event->getData(); $output->writeln(sprintf('[<error>Errored: %s</error>]', $data['exception']->getMessage())); }); $emitter->addListener('git_guardian.config_skip_repository', function () use($output) { /** @var GitRepositoryEvent $event */ $output->writeln('[<info>Skipped</info>]'); }); $emitter->addListener('git_guardian.post_fetch_repository', function () use($output) { /** @var GitRepositoryEvent $event */ $output->writeln('[<info>Fetched</info>]'); }); }
protected function execute(InputInterface $input, OutputInterface $output) { $source = realpath($input->getArgument(self::ARG_SOURCE)); $target = $input->getArgument(self::ARG_TARGET); $force = $input->getOption('force'); if ($output->isVeryVerbose()) { $message = sprintf('<info>Using %s as the hook.</info>', $source); $output->writeln($message); $message = sprintf('<info>Using %s for the install path.</info>', $target); $output->writeln($message); } if (!file_exists($source)) { $error = sprintf('<error>The hook %s does not exist!</error>', $source); $output->writeln($error); exit(1); } if (!is_dir(dirname($target))) { $message = sprintf('<error>The directory at %s does not exist.</error>', $target); $output->writeln($message); exit(1); } if (file_exists($target) && $force) { unlink($target); $message = sprintf('<comment>Removed existing file at %s.</comment>', $target); $output->writeln($message); } if (!file_exists($target) || $force) { symlink($source, $target); chmod($target, 0755); $output->writeln('Symlink created.'); } else { $message = sprintf('<error>A file at %s already exists.</error>', $target); $output->writeln($message); exit(1); } }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $verbosity = $output->getVerbosity(); if ($verbosity > 0) { $verbosityMapping = [1 => Logger::WARNING, 2 => Logger::NOTICE, 3 => Logger::INFO, 4 => Logger::DEBUG]; $handler = new StreamHandler('php://stdout', $verbosityMapping[$verbosity]); $handler->setFormatter(new ColoredLineFormatter()); $this->logger->pushHandler($handler); } if ($output->isVerbose()) { $this->eventDispatcher->addListener(EventEnum::QUEUE_BINDED, function (AMQPQueueBindedEvent $event) use($output) { $output->writeln('topic binded: ' . $event->getBindingKey()); }); $this->eventDispatcher->addListener(\Lexsign\StateMachine\Events\EventEnum::VALUE_UPDATED, function (ValueEvent $event) use($output) { $changeSet = $event->getChangeSet(); if ($changeSet->hasChanged()) { $output->writeln('<info>' . $changeSet->getTimestamp()->format(DateTime::ISO8601) . ' - ' . $changeSet->getKey() . ': ' . var_export($changeSet->getPreviousValue(), true) . ' -> ' . var_export($changeSet->getCurrentValue(), true) . '</info>'); } }); } if ($output->isVeryVerbose()) { $this->eventDispatcher->addListener(\Lexsign\StateMachine\Events\EventEnum::VALUE_UPDATED, function (ValueEvent $event) use($output) { $changeSet = $event->getChangeSet(); if (!$changeSet->hasChanged()) { $output->writeln($changeSet->getTimestamp()->format(DateTime::ISO8601) . ' - ' . $changeSet->getKey() . ': not changed - ' . var_export($changeSet->getPreviousValue(), true)); } }); } if ($output->isDebug()) { $this->eventDispatcher->addListener(EventEnum::INCOMING_MESSAGE, function (AMQPIncomingEvent $event) use($output) { $msg = $event->getMessage(); $output->writeln('<comment>' . $msg->delivery_info['routing_key'] . ' - ' . $msg->body . '</comment>'); }, 1000); } $this->amqpFetcher->run(); }
/** * Returns whether verbosity is very verbose (-vv). * * @return bool */ public function isVeryVerbose() { return $this->output->isVeryVerbose(); }
/** * Creates a new ProcessSlave instance and forks the process. * * @param integer $port */ function newInstance($port) { if ($this->inShutdown) { //when we are in the shutdown phase, we close all connections //as a result it actually tries to reconnect the slave, but we forbid it in this phase. return; } $keepClosed = false; $bootstrapFailed = 0; if (isset($this->slaves[$port])) { $bootstrapFailed = $this->slaves[$port]['bootstrapFailed']; $keepClosed = $this->slaves[$port]['keepClosed']; if ($keepClosed) { return; } } if ($this->output->isVeryVerbose()) { $this->output->writeln("Start new worker at port={$port}"); } $dir = var_export(__DIR__, true); $this->slaves[$port] = ['ready' => false, 'pid' => null, 'port' => $port, 'closeWhenFree' => false, 'waitForRegister' => true, 'duringBootstrap' => false, 'bootstrapFailed' => $bootstrapFailed, 'keepClosed' => $keepClosed, 'busy' => false, 'requests' => 0, 'connections' => 0, 'connection' => null]; $bridge = var_export($this->getBridge(), true); $bootstrap = var_export($this->getAppBootstrap(), true); $config = ['port' => $port, 'app-env' => $this->getAppEnv(), 'debug' => $this->isDebug(), 'logging' => $this->isLogging(), 'static' => true]; $config = var_export($config, true); $script = <<<EOF <?php require_once file_exists({$dir} . '/vendor/autoload.php') ? {$dir} . '/vendor/autoload.php' : {$dir} . '/../../autoload.php'; new \\PHPPM\\ProcessSlave({$bridge}, {$bootstrap}, {$config}); EOF; if ($this->phpCgiExecutable) { $commandline = $this->phpCgiExecutable; } else { $executableFinder = new PhpExecutableFinder(); $commandline = $executableFinder->find() . '-cgi'; } $file = tempnam(sys_get_temp_dir(), 'dbg'); file_put_contents($file, $script); register_shutdown_function('unlink', $file); //we can not use -q since this disables basically all header support //but since this is necessary at least in Symfony we can not use it. //e.g. headers_sent() returns always true, although wrong. $commandline .= ' -C ' . ProcessUtils::escapeArgument($file); $descriptorspec = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']]; $this->slaves[$port]['process'] = proc_open($commandline, $descriptorspec, $pipes); $this->slaves[$port]['stderr'] = new \React\Stream\Stream($pipes[2], $this->loop); $this->slaves[$port]['stderr']->on('data', function ($data) use($port) { if ($this->lastWorkerErrorPrintBy !== $port) { $this->output->writeln("<info>--- Worker {$port} stderr ---</info>"); $this->lastWorkerErrorPrintBy = $port; } $this->output->write("<error>{$data}</error>"); }); }
/** * Returns whether verbosity is very verbose (-vv). * * @return bool true if verbosity is set to VERBOSITY_VERY_VERBOSE, false otherwise */ public function isVeryVerbose() { return $this->decoratedOutput->isVeryVerbose(); }
/** * Creates a new ProcessSlave instance. * * @param integer $port */ protected function newInstance($port) { if ($this->inShutdown) { //when we are in the shutdown phase, we close all connections //as a result it actually tries to reconnect the slave, but we forbid it in this phase. return; } $keepClosed = false; $bootstrapFailed = 0; if (isset($this->slaves[$port])) { $bootstrapFailed = $this->slaves[$port]['bootstrapFailed']; $keepClosed = $this->slaves[$port]['keepClosed']; if ($keepClosed) { return; } } if ($this->output->isVeryVerbose()) { $this->output->writeln(sprintf("Start new worker #%d", $port)); } $host = Utils::isWindows() ? 'tcp://127.0.0.1' : $this->getNewSlaveSocket($port); $slave = ['ready' => false, 'pid' => null, 'port' => $port, 'closeWhenFree' => false, 'waitForRegister' => true, 'duringBootstrap' => false, 'bootstrapFailed' => $bootstrapFailed, 'keepClosed' => $keepClosed, 'busy' => false, 'requests' => 0, 'connections' => 0, 'connection' => null, 'host' => $host]; $bridge = var_export($this->getBridge(), true); $bootstrap = var_export($this->getAppBootstrap(), true); $config = ['port' => $slave['port'], 'host' => $slave['host'], 'session_path' => session_save_path(), 'controllerHost' => Utils::isWindows() ? 'tcp://127.0.0.1' : $this->controllerHost, 'app-env' => $this->getAppEnv(), 'debug' => $this->isDebug(), 'logging' => $this->isLogging(), 'static' => $this->isServingStatic()]; $config = var_export($config, true); $dir = var_export(__DIR__, true); $script = <<<EOF <?php set_time_limit(0); require_once file_exists({$dir} . '/vendor/autoload.php') ? {$dir} . '/vendor/autoload.php' : {$dir} . '/../../autoload.php'; require_once {$dir} . '/functions.php'; //global for all global functions \\PHPPM\\ProcessSlave::\$slave = new \\PHPPM\\ProcessSlave({$bridge}, {$bootstrap}, {$config}); \\PHPPM\\ProcessSlave::\$slave->run(); EOF; $commandline = $this->phpCgiExecutable; $file = tempnam(sys_get_temp_dir(), 'dbg'); file_put_contents($file, $script); register_shutdown_function('unlink', $file); //we can not use -q since this disables basically all header support //but since this is necessary at least in Symfony we can not use it. //e.g. headers_sent() returns always true, although wrong. $commandline .= ' -C ' . ProcessUtils::escapeArgument($file); $descriptorspec = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']]; $this->slaves[$port] = $slave; $this->slaves[$port]['process'] = proc_open($commandline, $descriptorspec, $pipes); $stderr = new \React\Stream\Stream($pipes[2], $this->loop); $stderr->on('data', function ($data) use($port) { if ($this->lastWorkerErrorPrintBy !== $port) { $this->output->writeln("<info>--- Worker {$port} stderr ---</info>"); $this->lastWorkerErrorPrintBy = $port; } $this->output->write("<error>{$data}</error>"); }); $this->slaves[$port]['stderr'] = $stderr; }
private function detectModules(OutputInterface $output) { $finder = new Finder(); $files = $finder->directories()->in('vendor'); $paths = array(); /** @var SplFileInfo $file */ foreach ($files as $file) { if ($output->isVeryVerbose()) { $output->writeln($file->getRealPath()); } $frameworks = Repository::get()->getFrameworks(); /** @var Framework $framework */ foreach ($frameworks as $framework) { if ($framework->validatePath($file->getRealPath())) { $paths[] = array('path' => 'vendor/' . $file->getRelativePathname(), 'framework' => $framework->getName()); } } } return $paths; }
protected function execute(InputInterface $input, OutputInterface $output) { $doctrine = $this->getContainer()->get('doctrine'); $em = $doctrine->getManager(); $pageRepository = $doctrine->getRepository('S2bCrawlerBundle:Page'); $id = $input->getArgument('id'); $page = $pageRepository->findOneById($id); if (!$page) { $output->writeln("<error>Page not found</error>"); return; } if ($page->isCrawled()) { $output->writeln("<error>Page already crawled</error>"); return; } $client = new Client(); try { $response = $client->get($page->getUrl()); } catch (ClientException $e) { switch ($e->getResponse()->getStatusCode()) { case 404: $output->writeln('<error>Not found ' . $page->getUrl() . '</error>'); $page->setCrawledAt(new \DateTime()); $em->persist($page); $em->flush(); return; case 503: sleep(1); continue; } } $links = $this->parseLinks((string) $response->getBody(), $page->getUrl()); $links = $this->filterLinks($links); $crawled = new PageCrawled(); $crawled->setPage($page)->setContent((string) $response->getBody())->setLinks($links); $page->setCrawled($crawled)->setCrawledAt(new \DateTime()); if ($links) { $links_added = 0; foreach ($links as $i => $link) { if ($pageRepository->findOneByUrl($link)) { if ($output->isVeryVerbose()) { $output->writeln("Skipped #" . $i . ' ' . $link . ' - already in database'); } continue; } $linkPage = new Page(); $linkPage->setUrl($link)->setDepth($page->getDepth() + 1); $em->persist($linkPage); $em->flush(); $links_added++; if ($output->isVerbose()) { $output->writeln("Added #" . $i . ' ' . $link); } } $output->writeln('Added ' . $links_added . '/' . count($links) . ' links'); } $em->persist($crawled); $em->persist($page); $em->flush(); $output->writeln('Crawled ' . $page->getUrl()); }
protected function execute(InputInterface $input, OutputInterface $output) { $skipExtensions = $input->getOption('skip-extensions'); $skipModules = $input->getOption('skip-modules'); $skipHooks = $input->getOption('skip-hooks'); $skipWidgets = $input->getOption('skip-widgets'); $skipTasks = $input->getOption('skip-tasks'); $skipPlugins = $input->getOption('skip-plugins'); $skipSetup = $input->getOption('skip-setup'); $save_path = $input->getOption('path'); $save_basedir = join(\DIRECTORY_SEPARATOR, [getcwd(), $save_path]); // Check for any existing files if ($output->isVeryVerbose()) { $output->writeln('Checking for any existing proxy class files in ' . $save_basedir); } if (is_dir($save_basedir)) { $glob_path = join(\DIRECTORY_SEPARATOR, [$save_basedir, '*']); $files = glob($glob_path); if ($output->isVerbose()) { $output->writeln(sprintf('Removing %d old proxy class files', count($files))); } else { $output->writeln('Removing old proxy class files'); } // Iterate over the files and delete them foreach ($files as $file) { if (is_file($file)) { if ($output->isDebug()) { $output->writeln('Removing file: ' . $file); } unlink($file); } } } // Make sure our base directories exist if (!is_dir($save_basedir)) { if ($output->isVeryVerbose()) { $output->writeln('Creating proxy classes directory: ' . $save_basedir); } mkdir($save_basedir, 0755); } $excluded = [$save_path, 'datastore']; $filter = function ($file, $key, $iterator) use($excluded) { if ($iterator->hasChildren() && !in_array($file->getFilename(), $excluded)) { return true; } return $file->isFile(); }; $dirIterator = new \RecursiveDirectoryIterator(getcwd(), \RecursiveDirectoryIterator::SKIP_DOTS); $iterator = new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator($dirIterator, $filter), \RecursiveIteratorIterator::SELF_FIRST); // Get a list of files to iterate over $files_to_parse = []; $iterator = new \RegexIterator($iterator, '/^.+\\.php$/i', \RecursiveRegexIterator::GET_MATCH); foreach ($iterator as $file) { $files_to_parse[] = $file; } $count = count($files_to_parse); if ($output->isDebug()) { $output->writeln(count($files_to_parse) . ' files matched'); } // Start our progress bar and disable the console cursor $output->write("[?25l", true); $progress = new ProgressBar($output, $count); $progress->setFormat(" %namespace%\n %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%"); $i = 0; foreach ($files_to_parse as $file) { $filePath = $file[0]; $handle = @fopen($filePath, "r"); if ($handle) { $namespace = ''; while (!feof($handle)) { $line = fgets($handle, 4096); $matches = array(); // Get the namespace preg_match('/^namespace(.+?)([^\\;]+)/', $line, $matched); if (isset($matched[0])) { $namespace = $matched[0]; } // Filter results # Extensions if (mb_strpos($namespace, '\\extensions\\') and $skipExtensions) { if ($output->isDebug()) { $output->writeln('Skipping extension: ' + $filePath); } $progress->advance(); continue; } # Modules if (mb_strpos($namespace, '\\modules\\front\\') or mb_strpos($namespace, '\\modules\\admin\\')) { if ($skipModules) { if ($output->isDebug()) { $output->writeln('Skipping module: ' + $filePath); } $progress->advance(); continue; } } # Hooks if (mb_strpos($filePath, \DIRECTORY_SEPARATOR . 'hooks' . \DIRECTORY_SEPARATOR) and $skipHooks) { if ($output->isDebug()) { $output->writeln('Skipping hook: ' + $filePath); } $progress->advance(); continue; } # Widgets if (mb_strpos($filePath, \DIRECTORY_SEPARATOR . 'widgets' . \DIRECTORY_SEPARATOR) and $skipWidgets) { if ($output->isDebug()) { $output->writeln('Skipping widget: ' + $filePath); } $progress->advance(); continue; } # Tasks if (mb_strpos($filePath, \DIRECTORY_SEPARATOR . 'tasks' . \DIRECTORY_SEPARATOR) and $skipTasks) { if ($output->isDebug()) { $output->writeln('Skipping task: ' + $filePath); } $progress->advance(); continue; } # Plugins if (mb_strpos($filePath, \DIRECTORY_SEPARATOR . 'plugins' . \DIRECTORY_SEPARATOR) and $skipPlugins) { if ($output->isDebug()) { $output->writeln('Skipping plugin: ' + $filePath); } $progress->advance(); continue; } # Setup if (mb_strpos($filePath, \DIRECTORY_SEPARATOR . 'setup' . \DIRECTORY_SEPARATOR) and $skipSetup) { if ($output->isDebug()) { $output->writeln('Skipping setup: ' + $filePath); } $progress->advance(); continue; } if (preg_match('#^(\\s*)((?:(?:abstract|final|static)\\s+)*)class\\s+([-a-zA-Z0-9_]+)(?:\\s+extends\\s+([-a-zA-Z0-9_]+))?(?:\\s+implements\\s+([-a-zA-Z0-9_,\\s]+))?#', $line, $matches)) { if (substr($matches[3], 0, 1) === '_') { $content = ''; $append = ltrim($matches[3], '\\'); $m = ltrim($matches[3], '\\'); $m = str_replace('_', '', $m); $content = "<?php\n\n"; if ($namespace) { $content .= $namespace . ";\n\n"; } $content .= $matches[2] . 'class ' . $m . ' extends ' . $append . '{}' . "\n"; $progress->setMessage($namespace, 'namespace'); $filename = mb_strtolower($m) . '.php'; $filePath = join(\DIRECTORY_SEPARATOR, [getcwd(), $save_path, $filename]); if (!is_file($filePath)) { file_put_contents($filePath, $content); } else { $alt = str_replace(["\\", " ", ";"], "_", $namespace); $filename = $alt . "_" . $filename; $filePath = join(\DIRECTORY_SEPARATOR, [getcwd(), $save_path, $filename]); file_put_contents($filePath, $content); } $i++; } } } $progress->advance(); fclose($handle); } else { $progress->advance(); } } // Finish and re-enable the cursor $progress->finish(); $output->write("[?25h", true); }