/** * Show messages only on php script die! */ public function __destruct() { foreach ($this->_info as $message) { Cli::out($message, false); } foreach ($this->_error as $message) { Cli::err($message, false); } }
/** * @param $key * @param $value */ protected function _tcEcho($key, $value) { $key = str_replace("'", "\\'", $key); $key = $this->_prefix ? $this->_prefix . ': ' . $key : $key; if (strpos($value, '.') || is_float($value)) { $value = round($value, 6); } else { $value = str_replace("'", "\\'", $value); } Cli::out("##teamcity[buildStatisticValue key='{$key}' value='{$value}']"); }
/** * Progress wrapper (if use stepmode). * * @param string $name * @param int $total * @param int $stepSize * @param \Closure $onStart * @param \Closure $onStepMode * @param \Closure $onFinish */ protected function _progressWrap($name, $total, $stepSize, $onStart, $onStepMode, $onFinish) { $_this = $this; $step = $this->_getOpt('step'); $profile = $this->_getOpt('profile'); $stepMode = $this->_getOpt('stepmode'); $isFinished = false; if ($stepMode) { if ($step >= 0) { $onStepMode($step); } else { $this->_progressBar($name, $total, $stepSize, function ($currentStep) use($profile, $total, $_this, $name) { $phpBin = Env::getBinary(); $binPath = './' . FS::getRelative($_SERVER['SCRIPT_FILENAME'], JPATH_ROOT, '/'); $options = array('profile' => $profile, 'step' => (int) $currentStep, 'stepmode' => '', 'q' => ''); $command = $phpBin . ' ' . $binPath . ' ' . $name; $result = Cli::exec($command, $options, JPATH_ROOT, false); if (0 && $this->_isDebug()) { $_this->_($result); } return $currentStep <= $total; }); $isFinished = true; } } else { $isFinished = $onStart(); } $onFinish($isFinished); }
/** * Execute cli commands * * @param string $command * @param array $args * @param null $cwd * @param bool $verbose * @return string * @throws ProcessFailedException * @throws \Exception */ public static function exec($command, $args = array(), $cwd = null, $verbose = false) { if (!class_exists('\\Symfony\\Component\\Process\\Process')) { throw new \Exception("Symfony/Process package required for Cli::exec() method"); // @codeCoverageIgnore } $cmd = self::build($command, $args); $cwd = $cwd ? $cwd = realpath($cwd) : null; //@codeCoverageIgnoreStart if ($verbose) { // Only in testing mode if (function_exists('\\JBZoo\\PHPUnit\\cliMessage')) { \JBZoo\PHPUnit\cliMessage('Process: ' . $cmd); \JBZoo\PHPUnit\cliMessage('CWD: ' . $cwd); } else { Cli::out('Process: ' . $cmd); Cli::out('CWD: ' . $cwd); } } //@codeCoverageIgnoreEnd // execute command $process = new Process($cmd, $cwd); $process->run(); // executes after the command finishes if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } return $process->getOutput(); }
/** * @param string $message * @param bool $addEol */ public function out($message, $addEol = true) { if (function_exists('\\JBZoo\\PHPUnit\\cliMessage')) { \JBZoo\PHPUnit\cliMessage($message, $addEol); } else { Cli::out($message, $addEol); } }
/** * @return bool */ public function isCli() { return Cli::check(); }
/** * @covers \JBZoo\Utils\Cli::getNumberOfColumns * @uses \JBZoo\Utils\Cli::isInteractive */ public function testCanDetectNumberOfColumns() { $this->assertInternalType('integer', Cli::getNumberOfColumns()); }
/** * Execute method of command * * @param InputInterface $input * @param OutputInterface $output * @return int|null|void * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function execute(InputInterface $input, OutputInterface $output) { $this->_executePrepare($input, $output); $this->_init(); // Prepare if ((int) $this->_config->get('reindex', 0)) { $this->_('Database ReIndex = true', 'info'); $_GET['controller'] = $_REQUEST['controller'] = 'cli-import'; // emulate browser and admin CP } else { $this->_('Database ReIndex = false', 'info'); $_GET['controller'] = $_REQUEST['controller'] = 'jbimport'; // emulate browser and admin CP } $stepMode = $this->_getOpt('stepmode'); $step = $this->_getOpt('step'); $profileName = $this->_getOpt('profile'); $csvInfo = $this->_getCsvInfo(); $sesData = $this->_initJoomlaSession($csvInfo); $stepSize = $this->_getStepSize(); $rowsCount = $sesData['count']; $stepsCount = (int) ceil($rowsCount / $stepSize); if ($stepsCount <= 0) { $this->_('Count of steps is <= 0', 'Error', 1); } $this->_showProfiler('Import - prepared'); $this->_('CSV File: ' . $csvInfo['path'], 'Info'); $this->_('CSV lines: ' . $csvInfo['count'], 'Info'); $this->_('Step size: ' . $stepSize, 'Info'); $this->_('Steps count: ' . $stepsCount, 'Info'); $this->_('Step mode: ' . ($stepMode ? 'on' : 'off'), 'Info'); // Show progress bar and run process $jbimport = $this->_jbimport; $_this = $this; $isFinished = false; if ($stepMode) { if ($step >= 0) { $jbimport->itemsProcess($step); $this->_addTmpData($this->_jbsession->get('ids', 'import-ids')); } else { $this->_cleanTmpData(); $this->_progressBar('import', $stepsCount, 1, function ($currentStep) use($jbimport, $stepsCount, $profileName, $_this) { $phpBin = Env::getBinary(); $binPath = './' . FS::getRelative($_SERVER['SCRIPT_FILENAME'], JPATH_ROOT, '/'); $options = array('profile' => $profileName, 'step' => (int) $currentStep, 'stepmode' => '', 'q' => ''); $result = Cli::exec($phpBin . ' ' . $binPath . ' import:items', $options, JPATH_ROOT, false); if (0 && $this->_isDebug()) { $_this->_($result); } return $currentStep <= $stepsCount; }); $this->_jbsession->set('ids', $this->_getTmpData(), 'import-ids'); $isFinished = true; } } else { $this->_progressBar('import', $stepsCount, 1, function ($currentStep) use($jbimport) { $result = $jbimport->itemsProcess($currentStep); return $result['progress'] >= 100 ? false : true; }); $isFinished = true; } if ($isFinished) { $this->_showProfiler('Import - finished'); // Remove or disable other items $this->_jbsession->set('ids', $this->_getTmpData(), 'import-ids'); $this->_postImport(); $this->_showProfiler('Import - Post handler'); $this->_moveCsvFile($csvInfo['path']); $this->_showProfiler('Import - Done!'); $this->_cleanTmpData(); } }
public function testPHPMetrics() { $actual = Cli::exec($this->_binpath . ' teamcity:phpmetrics ./tests/fixtures/phpmetrics.xml'); $expected = implode(PHP_EOL, ["##teamcity[buildStatisticValue key='PHP Metrics: Number of lines of code' value='1790']", "##teamcity[buildStatisticValue key='PHP Metrics: Number of logical lines of code' value='363']", "##teamcity[buildStatisticValue key='PHP Metrics: Cyclomatic complexity' value='4.76']", "##teamcity[buildStatisticValue key='PHP Metrics: Maintainability index' value='117.48']", "##teamcity[buildStatisticValue key='PHP Metrics: Volume' value='660.36']", "##teamcity[buildStatisticValue key='PHP Metrics: Used vocabulary' value='27.28']", "##teamcity[buildStatisticValue key='PHP Metrics: Difficulty index' value='8.15']", "##teamcity[buildStatisticValue key='PHP Metrics: Effort to understand' value='15348.57']", "##teamcity[buildStatisticValue key='PHP Metrics: Number of estimated bugs' value='0.22']", "##teamcity[buildStatisticValue key='PHP Metrics: Time to understand' value='852.64']", "##teamcity[buildStatisticValue key='PHP Metrics: Intelligent content' value='43.46']", "##teamcity[buildStatisticValue key='PHP Metrics: Comment weight' value='43.64']", "##teamcity[buildStatisticValue key='PHP Metrics: Length' value='109.52']", "##teamcity[buildStatisticValue key='PHP Metrics: Lack of cohesion' value='1.04']", "##teamcity[buildStatisticValue key='PHP Metrics: Class resilience to change' value='0.34']", "##teamcity[buildStatisticValue key='PHP Metrics: Efferent coupling' value='1.48']", "##teamcity[buildStatisticValue key='PHP Metrics: Afferent coupling' value='0.24']", "##teamcity[buildStatisticValue key='PHP Metrics: Total System complexity' value='6.08']", "##teamcity[buildStatisticValue key='PHP Metrics: Relative System complexity' value='1.09']", "##teamcity[buildStatisticValue key='PHP Metrics: Data Complexity' value='2']", "##teamcity[buildStatisticValue key='PHP Metrics: Relative data complexity' value='0.41']", "##teamcity[buildStatisticValue key='PHP Metrics: System complexity' value='4.08']", "##teamcity[buildStatisticValue key='PHP Metrics: Relative structural complexity' value='0.68']", "##teamcity[buildStatisticValue key='PHP Metrics: Number of classes' value='16']", "##teamcity[buildStatisticValue key='PHP Metrics: Number of abstract classes' value='3']", "##teamcity[buildStatisticValue key='PHP Metrics: Number of concrete classes' value='13']", "##teamcity[buildStatisticValue key='PHP Metrics: Number Of Interfaces' value='0']", "##teamcity[buildStatisticValue key='PHP Metrics: Number Of Methods' value='55']", ""]); isSame($expected, $actual); }
/** * @param string $command * @param array $args * @param null $cwd * @param bool $verbose * @return string * @throws Exception */ function cmd($command, $args = array(), $cwd = null, $verbose = false) { if (!class_exists('\\JBZoo\\Utils\\Cli')) { throw new Exception('jbzoo/utils required for cmd() function'); } if (!class_exists('\\Symfony\\Component\\Process\\Process')) { throw new Exception("symfony/process package required for cmd() function"); } return Cli::exec($command, $args, $cwd, $verbose); }