本文整理汇总了PHP中Symfony\Component\Stopwatch\Stopwatch::getSectionEvents方法的典型用法代码示例。如果您正苦于以下问题:PHP Stopwatch::getSectionEvents方法的具体用法?PHP Stopwatch::getSectionEvents怎么用?PHP Stopwatch::getSectionEvents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Stopwatch\Stopwatch
的用法示例。
在下文中一共展示了Stopwatch::getSectionEvents方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: microtime
/**
* Render interface and add to the toolbar.
*
* @param \WP_Admin_Bar $wp_admin_bar
*/
static function admin_bar_menu($wp_admin_bar)
{
if (!apply_filters('laps_can_see', current_user_can('manage_options'))) {
return;
}
global $timestart, $wpdb;
$mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader(dirname(__DIR__) . '/views'), 'cache' => new Mustache_Cache_FrozenCache(dirname(__DIR__) . '/views/cache')));
self::$stopwatch->stop('Toolbar');
$events = self::$stopwatch->getSectionEvents('__root__');
$start = $timestart * 1000;
$end = microtime(true) * 1000;
$total = $end - $start;
$event_data = array();
$http_data = array();
foreach ($events as $name => $event) {
$offset = round(($event->getOrigin() - $start) / $total * 100, 2);
$duration = $event->getDuration();
$width = round($duration / $total * 100, 2);
$category = $event->getCategory();
if ('http' === $category) {
$http_data[] = compact('name', 'offset', 'duration', 'width', 'category');
continue;
}
$memory = $event->getMemory() / 1024 / 1024;
$event_data[] = compact('name', 'offset', 'duration', 'width', 'category', 'memory');
}
$query_data = array();
$last_query_end = 0;
$last_offset = 0;
$last_duration = 0;
$category = 'query';
if (defined('SAVEQUERIES') && SAVEQUERIES) {
foreach ($wpdb->queries as $key => $query) {
$query_start = isset(self::$query_starts[$key]) ? self::$query_starts[$key] : $last_query_end;
list($sql, $duration, $trace) = $query;
$duration *= 1000;
$last_query_end = $query_start + $duration;
$offset = round(($query_start - $start) / $total * 100, 2);
// if query is indistinguishably close to previous then stack it
if ($offset === $last_offset) {
$key = count($query_data) - 1;
$query_data[$key]['sql'] .= '<br />' . $sql;
$last_duration += $duration;
$width = round($last_duration / $total * 100, 2);
$query_data[$key]['width'] = $width;
continue;
}
$width = round($duration / $total * 100, 2);
$last_offset = $offset;
$last_duration = $duration;
$query_data[] = compact('sql', 'duration', 'offset', 'width', 'category');
}
}
$html = $mustache->render('laps', array('events' => $event_data, 'queries' => $query_data, 'savequeries' => defined('SAVEQUERIES') && SAVEQUERIES, 'http' => $http_data, 'savehttp' => !empty($http_data)));
$wp_admin_bar->add_node(array('id' => 'laps', 'title' => sprintf('Lap: %ss', round($total / 1000, 3))));
$wp_admin_bar->add_node(array('id' => 'laps_output', 'parent' => 'laps', 'meta' => array('html' => $html)));
}
示例2: testStopwatchSections
public function testStopwatchSections()
{
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
$kernel = $this->getHttpKernel($dispatcher, function () {
return new Response();
});
$request = Request::create('/');
$response = $kernel->handle($request);
$kernel->terminate($request, $response);
$events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
$this->assertEquals(array('__section__', 'kernel.request', 'kernel.controller', 'controller', 'kernel.response', 'kernel.terminate'), array_keys($events));
}
示例3: outputPerformance
/**
* @param Stopwatch $stopwatch
* @param OutputInterface $output
*/
protected function outputPerformance(Stopwatch $stopwatch, OutputInterface $output)
{
if (OutputInterface::VERBOSITY_DEBUG <= $this->verbosity) {
$output->writeln('Fixing time per file:');
foreach ($stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('Fixed all files in %.3f seconds, %.3f MB memory used', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
}
示例4: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$errorsManager = new ErrorsManager();
$stopwatch = new Stopwatch();
$this->fixer = new Fixer($this->getFinder($input->getArgument('path')), $errorsManager, $stopwatch);
$this->fixer->registerBuiltInFixers();
if (!$input->getOption('no-use-reorder')) {
$this->fixer->addFixer(new OrderedUseFixer());
}
$stopwatch->start('fixFiles');
$changed = $this->fixer->fix($input->getOption('dry-run'));
$stopwatch->stop('fixFiles');
$verbosity = $output->getVerbosity();
$i = 1;
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$output->write(sprintf(' (<comment>%s</comment>)', implode(', ', $fixResult)));
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('Fixed all files in %.3f seconds, %.3f MB memory used', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
if (!$errorsManager->isEmpty()) {
$output->writeLn('');
$output->writeLn('Files that were not fixed due to internal error:');
foreach ($errorsManager->getErrors() as $i => $error) {
$output->writeLn(sprintf('%4d) %s', $i + 1, $error['filepath']));
}
}
return empty($changed) ? 0 : 1;
}
示例5: execute
//.........这里部分代码省略.........
$changed = $this->fixer->fix($config, $input->getOption('dry-run'), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');
if ($showProgress) {
$this->fixer->setEventDispatcher(null);
$this->eventDispatcher->removeListener(FixerFileProcessedEvent::NAME, $fileProcessedEventListener);
$output->writeln('');
$legend = array();
foreach (FixerFileProcessedEvent::getStatusMap() as $status) {
if ($status['symbol'] && $status['description']) {
$legend[] = $status['symbol'] . '-' . $status['description'];
}
}
$output->writeln('Legend: ' . implode(', ', array_unique($legend)));
}
$verbosity = $output->getVerbosity();
$i = 1;
switch ($input->getOption('format')) {
case 'txt':
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$output->write(sprintf(' (<comment>%s</comment>)', implode(', ', $fixResult['appliedFixers'])));
}
if ($input->getOption('diff')) {
$output->writeln('');
$output->writeln('<comment> ---------- begin diff ----------</comment>');
$output->writeln($fixResult['diff']);
$output->writeln('<comment> ---------- end diff ----------</comment>');
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('Fixed all files in %.3f seconds, %.3f MB memory used', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
break;
case 'xml':
$dom = new \DOMDocument('1.0', 'UTF-8');
$filesXML = $dom->createElement('files');
$dom->appendChild($filesXML);
foreach ($changed as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$appliedFixersXML = $dom->createElement('applied_fixers');
$fileXML->appendChild($appliedFixersXML);
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
}
}
if ($input->getOption('diff')) {
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$fileXML->appendChild($diffXML);
示例6: testReopenASection
public function testReopenASection()
{
$stopwatch = new Stopwatch();
$stopwatch->openSection();
$stopwatch->start('foo', 'cat');
$stopwatch->stopSection('section');
$stopwatch->openSection('section');
$stopwatch->start('bar', 'cat');
$stopwatch->stopSection('section');
$events = $stopwatch->getSectionEvents('section');
$this->assertCount(3, $events);
$this->assertCount(2, $events['__section__']->getPeriods());
}
示例7: execute
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$verbosity = $output->getVerbosity();
$resolver = new ConfigurationResolver();
$resolver->setCwd(getcwd())->setDefaultConfig($this->defaultConfig)->setFixer($this->fixer)->setOptions(array('config' => $input->getOption('config'), 'config-file' => $input->getOption('config-file'), 'dry-run' => $input->getOption('dry-run'), 'level' => $input->getOption('level'), 'fixers' => $input->getOption('fixers'), 'path' => $input->getArgument('path'), 'progress' => OutputInterface::VERBOSITY_VERBOSE <= $verbosity && 'txt' === $input->getOption('format'), 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file')))->resolve();
$config = $resolver->getConfig();
$configFile = $resolver->getConfigFile();
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln(sprintf('Loaded config from "%s"', $configFile));
}
// register custom fixers from config
$this->fixer->registerCustomFixers($config->getCustomFixers());
if ($config->usingLinter()) {
try {
$this->fixer->setLinter(new Linter($config->getPhpExecutable()));
} catch (UnavailableLinterException $e) {
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln('Unable to use linter, can not find PHP executable');
}
}
}
$showProgress = $resolver->getProgress();
if ($showProgress) {
$this->fixer->setEventDispatcher($this->eventDispatcher);
$progressOutput = new ProcessOutput($this->eventDispatcher);
}
$this->stopwatch->start('fixFiles');
$changed = $this->fixer->fix($config, $resolver->isDryRun(), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');
if ($showProgress) {
$progressOutput->printLegend();
$this->fixer->setEventDispatcher(null);
}
$i = 1;
switch ($input->getOption('format')) {
case 'txt':
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$output->write(sprintf(' (<comment>%s</comment>)', implode(', ', $fixResult['appliedFixers'])));
}
if ($input->getOption('diff')) {
$output->writeln('');
$output->writeln('<comment> ---------- begin diff ----------</comment>');
$output->writeln($fixResult['diff']);
$output->writeln('<comment> ---------- end diff ----------</comment>');
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('Fixed all files in %.3f seconds, %.3f MB memory used', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
break;
case 'xml':
$dom = new \DOMDocument('1.0', 'UTF-8');
$filesXML = $dom->createElement('files');
$dom->appendChild($filesXML);
foreach ($changed as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$appliedFixersXML = $dom->createElement('applied_fixers');
$fileXML->appendChild($appliedFixersXML);
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
}
}
if ($input->getOption('diff')) {
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$fileXML->appendChild($diffXML);
}
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$timeXML = $dom->createElement('time');
$memoryXML = $dom->createElement('memory');
$dom->appendChild($timeXML);
$dom->appendChild($memoryXML);
$memoryXML->setAttribute('value', round($fixEvent->getMemory() / 1024 / 1024, 3));
$memoryXML->setAttribute('unit', 'MB');
$timeXML->setAttribute('unit', 's');
$timeTotalXML = $dom->createElement('total');
$timeTotalXML->setAttribute('value', round($fixEvent->getDuration() / 1000, 3));
$timeXML->appendChild($timeTotalXML);
//.........这里部分代码省略.........
示例8: execute
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : null;
if ($stdErr && extension_loaded('xdebug')) {
$stdErr->writeln(sprintf($stdErr->isDecorated() ? '<bg=yellow;fg=black;>%s</>' : '%s', 'You are running php-cs-fixer with xdebug enabled. This has a major impact on runtime performance.'));
}
$verbosity = $output->getVerbosity();
$resolver = new ConfigurationResolver();
$resolver->setCwd(getcwd())->setDefaultConfig($this->defaultConfig)->setFixer($this->fixer)->setOptions(array('allow-risky' => $input->getOption('allow-risky'), 'config' => $input->getOption('config'), 'config-file' => $input->getOption('config-file'), 'dry-run' => $input->getOption('dry-run'), 'rules' => $input->getOption('rules'), 'path' => $input->getArgument('path'), 'progress' => OutputInterface::VERBOSITY_VERBOSE <= $verbosity && 'txt' === $input->getOption('format'), 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file'), 'format' => $input->getOption('format')))->resolve();
$config = $resolver->getConfig();
$configFile = $resolver->getConfigFile();
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln(sprintf('Loaded config from "%s"', $configFile));
}
if ($config->usingLinter()) {
try {
$this->fixer->setLinter(new Linter($config->getPhpExecutable()));
} catch (UnavailableLinterException $e) {
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln('Unable to use linter, can not find PHP executable');
}
}
}
$showProgress = $resolver->getProgress();
if ($showProgress) {
$this->fixer->setEventDispatcher($this->eventDispatcher);
$progressOutput = new ProcessOutput($this->eventDispatcher);
}
$this->stopwatch->start('fixFiles');
$changed = $this->fixer->fix($config, $resolver->isDryRun(), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');
if ($showProgress) {
$progressOutput->printLegend();
$this->fixer->setEventDispatcher(null);
}
$i = 1;
switch ($resolver->getFormat()) {
case 'txt':
$fixerDetailLine = false;
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$fixerDetailLine = $output->isDecorated() ? ' (<comment>%s</comment>)' : ' %s';
}
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if ($fixerDetailLine) {
$output->write(sprintf($fixerDetailLine, implode(', ', $fixResult['appliedFixers'])));
}
if ($input->getOption('diff')) {
$output->writeln('');
$output->writeln('<comment> ---------- begin diff ----------</comment>');
$output->writeln($fixResult['diff']);
$output->writeln('<comment> ---------- end diff ----------</comment>');
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('%s all files in %.3f seconds, %.3f MB memory used', $input->getOption('dry-run') ? 'Checked' : 'Fixed', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
break;
case 'xml':
$dom = new \DOMDocument('1.0', 'UTF-8');
// new nodes should be added to this or existing children
$root = $dom->createElement('report');
$dom->appendChild($root);
$filesXML = $dom->createElement('files');
$root->appendChild($filesXML);
foreach ($changed as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$appliedFixersXML = $dom->createElement('applied_fixers');
$fileXML->appendChild($appliedFixersXML);
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
}
}
if ($input->getOption('diff')) {
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$fileXML->appendChild($diffXML);
}
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$timeXML = $dom->createElement('time');
//.........这里部分代码省略.........
示例9: Stopwatch
require_once 'vendor/autoload.php';
require_once 'functions.php';
use Symfony\Component\Stopwatch\Stopwatch;
$stopwatch = new Stopwatch();
$stopwatch->openSection();
$stopwatch->start('do_phase_1');
doSomeFunction();
$stopwatch->stopSection('step1');
$stopwatch->openSection();
$stopwatch->start('do_phase_1');
$totalLap = 10;
for ($count = 0; $count < $totalLap; $count++) {
doSomeFunction();
$stopwatch->lap('do_phase_1');
}
$stopwatch->stopSection('step2');
echo '<p>Step 1 :</p>';
$events_1 = $stopwatch->getSectionEvents('step1');
echo '<ul>';
foreach ($events_1 as $id => $event) {
echo '<li> phase ' . $id . ':' . $event->getDuration() . '</li>';
}
echo '</ul>';
echo '<p>Step 2 :</p>';
$events_2 = $stopwatch->getSectionEvents('step2');
echo '<ul>';
foreach ($events_2 as $id => $event) {
echo '<li> phase ' . $id . ':' . $event->getDuration() . '</li>';
}
echo '</ul>';
示例10: execute
//.........这里部分代码省略.........
$output->write($event->getStatusAsString());
};
}
if ($listenForFixerFileProcessedEvent) {
$this->fixer->setEventDispatcher($this->eventDispatcher);
$this->eventDispatcher->addListener(FixerFileProcessedEvent::NAME, $fileProcessedEventListener);
}
$this->stopwatch->start('fixFiles');
$changed = $this->fixer->fix($config, $input->getOption('dry-run'), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');
if ($listenForFixerFileProcessedEvent) {
$this->fixer->setEventDispatcher(null);
$this->eventDispatcher->removeListener(FixerFileProcessedEvent::NAME, $fileProcessedEventListener);
$output->writeln('');
}
$i = 1;
switch ($input->getOption('format')) {
case 'txt':
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$output->write(sprintf(' (<comment>%s</comment>)', implode(', ', $fixResult['appliedFixers'])));
if ($input->getOption('diff')) {
$output->writeln('');
$output->writeln('<comment> ---------- begin diff ----------</comment>');
$output->writeln($fixResult['diff']);
$output->writeln('<comment> ---------- end diff ----------</comment>');
}
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('Fixed all files in %.3f seconds, %.3f MB memory used', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
break;
case 'xml':
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($filesXML = $dom->createElement('files'));
foreach ($changed as $file => $fixResult) {
$filesXML->appendChild($fileXML = $dom->createElement('file'));
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$fileXML->appendChild($appliedFixersXML = $dom->createElement('applied_fixers'));
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixersXML->appendChild($appliedFixerXML = $dom->createElement('applied_fixer'));
$appliedFixerXML->setAttribute('name', $appliedFixer);
}
if ($input->getOption('diff')) {
$fileXML->appendChild($diffXML = $dom->createElement('diff'));
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
}
}
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$timeXML = $dom->createElement('time');
$memoryXML = $dom->createElement('memory');
示例11: execute
/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$verbosity = $output->getVerbosity();
$resolver = new ConfigurationResolver();
$resolver->setCwd(getcwd())->setDefaultConfig($this->defaultConfig)->setFixer($this->fixer)->setOptions(array('allow-risky' => $input->getOption('allow-risky'), 'config' => $input->getOption('config'), 'config-file' => $input->getOption('config-file'), 'dry-run' => $input->getOption('dry-run'), 'rules' => $input->getOption('rules'), 'path' => $input->getArgument('path'), 'progress' => OutputInterface::VERBOSITY_VERBOSE <= $verbosity && 'txt' === $input->getOption('format'), 'using-cache' => $input->getOption('using-cache'), 'cache-file' => $input->getOption('cache-file')))->resolve();
$config = $resolver->getConfig();
$configFile = $resolver->getConfigFile();
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln(sprintf('Loaded config from "%s"', $configFile));
}
if ($config->usingLinter()) {
try {
$this->fixer->setLinter(new Linter($config->getPhpExecutable()));
} catch (UnavailableLinterException $e) {
if ($configFile && 'txt' === $input->getOption('format')) {
$output->writeln('Unable to use linter, can not find PHP executable');
}
}
}
$showProgress = $resolver->getProgress();
if ($showProgress) {
$this->fixer->setEventDispatcher($this->eventDispatcher);
$progressOutput = new ProcessOutput($this->eventDispatcher);
}
$this->stopwatch->start('fixFiles');
$changed = $this->fixer->fix($config, $resolver->isDryRun(), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');
if ($showProgress) {
$progressOutput->printLegend();
$this->fixer->setEventDispatcher(null);
}
$translator = new Translator('en');
$translator->addResource('yml', __DIR__ . '/../../Resources/translations/messages.en.yml', 'en');
$translator->addLoader('yml', new YamlFileLoader());
switch ($input->getOption('format')) {
case 'txt':
$helper = new FixResultTxtOutputHelper($output, $translator, $verbosity, $input->getOption('diff'));
$helper->write($changed, $this->stopwatch);
break;
case 'xml':
$i = 1;
$dom = new \DOMDocument('1.0', 'UTF-8');
// new nodes should be added to this or existing children
$root = $dom->createElement('report');
$dom->appendChild($root);
$filesXML = $dom->createElement('files');
$root->appendChild($filesXML);
foreach ($changed as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$appliedFixersXML = $dom->createElement('applied_fixers');
$fileXML->appendChild($appliedFixersXML);
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
}
}
if ($input->getOption('diff')) {
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$fileXML->appendChild($diffXML);
}
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$timeXML = $dom->createElement('time');
$memoryXML = $dom->createElement('memory');
$root->appendChild($timeXML);
$root->appendChild($memoryXML);
$memoryXML->setAttribute('value', round($fixEvent->getMemory() / 1024 / 1024, 3));
$memoryXML->setAttribute('unit', 'MB');
$timeXML->setAttribute('unit', 's');
$timeTotalXML = $dom->createElement('total');
$timeTotalXML->setAttribute('value', round($fixEvent->getDuration() / 1000, 3));
$timeXML->appendChild($timeTotalXML);
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$timeFilesXML = $dom->createElement('files');
$timeXML->appendChild($timeFilesXML);
$eventCounter = 1;
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$timeFileXML = $dom->createElement('file');
$timeFilesXML->appendChild($timeFileXML);
$timeFileXML->setAttribute('id', $eventCounter++);
$timeFileXML->setAttribute('name', $file);
$timeFileXML->setAttribute('value', round($event->getDuration() / 1000, 3));
}
}
$dom->formatOutput = true;
$output->write($dom->saveXML());
break;
case 'json':
//.........这里部分代码省略.........
示例12: execute
//.........这里部分代码省略.........
switch ($resolver->getFormat()) {
case 'txt':
$fixerDetailLine = false;
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$fixerDetailLine = $output->isDecorated() ? ' (<comment>%s</comment>)' : ' %s';
}
foreach ($changed as $file => $fixResult) {
$output->write(sprintf('%4d) %s', $i++, $file));
if ($fixerDetailLine) {
$output->write(sprintf($fixerDetailLine, implode(', ', $fixResult['appliedFixers'])));
}
if ($isDiff) {
$output->writeln('');
$output->writeln('<comment> ---------- begin diff ----------</comment>');
if ($output->isDecorated()) {
$diff = implode(PHP_EOL, array_map(function ($string) {
$string = preg_replace('/^(\\+){3}/', '<info>+++</info>', $string);
$string = preg_replace('/^(\\+){1}/', '<info>+</info>', $string);
$string = preg_replace('/^(\\-){3}/', '<error>---</error>', $string);
$string = preg_replace('/^(\\-){1}/', '<error>-</error>', $string);
$string = str_repeat(' ', 6) . $string;
return $string;
}, explode(PHP_EOL, OutputFormatter::escape($fixResult['diff']))));
$output->writeln($diff);
} else {
$output->writeln($fixResult['diff'], OutputInterface::OUTPUT_RAW);
}
$output->writeln('<comment> ---------- end diff ----------</comment>');
}
$output->writeln('');
}
if (OutputInterface::VERBOSITY_DEBUG <= $verbosity) {
$output->writeln('Fixing time per file:');
foreach ($this->stopwatch->getSectionEvents('fixFile') as $file => $event) {
if ('__section__' === $file) {
continue;
}
$output->writeln(sprintf('[%.3f s] %s', $event->getDuration() / 1000, $file));
}
$output->writeln('');
}
$fixEvent = $this->stopwatch->getEvent('fixFiles');
$output->writeln(sprintf('%s all files in %.3f seconds, %.3f MB memory used', $input->getOption('dry-run') ? 'Checked' : 'Fixed', $fixEvent->getDuration() / 1000, $fixEvent->getMemory() / 1024 / 1024));
break;
case 'xml':
$dom = new \DOMDocument('1.0', 'UTF-8');
$filesXML = $dom->createElement('files');
$dom->appendChild($filesXML);
foreach ($changed as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$appliedFixersXML = $dom->createElement('applied_fixers');
$fileXML->appendChild($appliedFixersXML);
foreach ($fixResult['appliedFixers'] as $appliedFixer) {
$appliedFixerXML = $dom->createElement('applied_fixer');
$appliedFixerXML->setAttribute('name', $appliedFixer);
$appliedFixersXML->appendChild($appliedFixerXML);
}
}
if ($isDiff) {
$diffXML = $dom->createElement('diff');
$diffXML->appendChild($dom->createCDATASection($fixResult['diff']));
$fileXML->appendChild($diffXML);