Example #1
0
 /**
  * Selects a random sample of records.
  *
  * @param array $all_records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in the fetcher manipulator.
  */
 public function manipulate($all_records)
 {
     $numRecs = count($all_records);
     echo "Filtering {$numRecs} records through the RandomSet fetcher manipulator.\n";
     // Instantiate the progress bar if we're not running on Windows.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     $randomSet = $this->getRandomSet($numRecs);
     $record_num = 0;
     $filtered_records = array();
     foreach ($all_records as $record) {
         if (in_array($record_num, $randomSet)) {
             $filtered_records[] = $record;
         }
         $record_num++;
         if ($this->onWindows) {
             print '.';
         } else {
             $progress->current($record_num);
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     return $filtered_records;
 }
Example #2
0
 /**
  * @param Result $result
  */
 public function output(Result $result)
 {
     $assumptions = $result->getAssumptions();
     foreach ($assumptions as $assumption) {
         $fileElements = $this->xpath->query('/phpa/files/file[@name="' . $assumption['file'] . '"]');
         if ($fileElements->length === 0) {
             $files = $this->xpath->query('/phpa/files')->item(0);
             $fileElement = $this->document->createElement('file');
             $fileElement->setAttribute('name', $assumption['file']);
             $files->appendChild($fileElement);
         } else {
             $fileElement = $fileElements->item(0);
         }
         $lineElement = $this->document->createElement('line');
         $lineElement->setAttribute('number', $assumption['line']);
         $lineElement->setAttribute('message', $assumption['message']);
         $fileElement->appendChild($lineElement);
     }
     $this->document->documentElement->setAttribute('assumptions', $result->getAssumptionsCount());
     $this->document->documentElement->setAttribute('bool-expressions', $result->getBoolExpressionsCount());
     $this->document->documentElement->setAttribute('percentage', $result->getPercentage());
     $this->document->preserveWhiteSpace = false;
     $this->document->formatOutput = true;
     $this->document->save($this->file);
     $this->cli->out(sprintf('Written %d assumption(s) to file %s', $result->getAssumptionsCount(), $this->file));
 }
Example #3
0
 /**
  * @test
  */
 public function itShouldShowUsageWithNoArgs()
 {
     $this->argumentManager->parse(Argument::type('array'))->willThrow(\Exception::class);
     $args = ['phpa'];
     $this->climate->usage($args)->shouldBeCalled();
     $this->cli->handle($args);
 }
Example #4
0
 /**
  * Tests each record to see if it has a .cpd file, and if so,
  * what the value of the CPD <type> element is.
  *
  * @param array $all_records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in the fetcher manipulator.
  */
 public function manipulate($all_records)
 {
     $numRecs = count($all_records);
     echo "Fetching {$numRecs} records, filitering them.\n";
     // Instantiate the progress bar if we're not running on Windows.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     $record_num = 0;
     $filtered_records = array();
     foreach ($all_records as $record) {
         $structure = $this->getDocumentStructure($record->pointer);
         if ($record->filetype == 'cpd' && $structure['type'] == $this->type) {
             $filtered_records[] = $record;
         }
         $record_num++;
         if ($this->onWindows) {
             print '.';
         } else {
             $progress->current($record_num);
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     return $filtered_records;
 }
 /**
  * Tests each record to see if it has one of the extensions in
  * $this->allowed_extensions.
  *
  * @param array $all_records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in this function.
  */
 public function manipulate($records)
 {
     // var_dump($records);
     $numRecs = count($records);
     echo "Fetching {$numRecs} records, filitering them.\n";
     // Instantiate the progress bar.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     $record_num = 0;
     $filtered_records = array();
     foreach ($records as $record) {
         // var_dump($record);
         $ext = pathinfo($record->{$this->file_name_field}, PATHINFO_EXTENSION);
         if (in_array($ext, $this->allowed_extensions)) {
             $filtered_records[] = $record;
         }
         $record_num++;
         if ($this->onWindows) {
             print '.';
         } else {
             $progress->current($record_num);
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     return $filtered_records;
 }
Example #6
0
 /**
  * Tests each record to see if the 'filetype' propery is not 'cpd',
  * and then tests to see whether the object has no parent.
  *
  * @param array $all_records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in the
  *   fetcher manipulator.
  */
 public function manipulate($all_records)
 {
     $numRecs = count($all_records);
     echo "Filtering {$numRecs} records through the CdmNoParent fetcher manipulator.\n";
     // Instantiate the progress bar if we're not running on Windows.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     $record_num = 0;
     $filtered_records = array();
     foreach ($all_records as $record) {
         if (property_exists($record, 'key') && property_exists($record, 'filetype') && is_string($record->filetype) && $record->filetype != 'cpd') {
             $pointer = $record->key;
             // And that have no parent.
             if (!$this->getCdmParent($pointer)) {
                 $filtered_records[] = $record;
             }
             $record_num++;
             if ($this->onWindows) {
                 print '.';
             } else {
                 $progress->current($record_num);
             }
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     return $filtered_records;
 }
Example #7
0
 /**
  * @param array $args
  */
 public function handle(array $args)
 {
     $this->cli->out(sprintf('PHPAssumptions analyser v%s by @rskuipers', Cli::VERSION))->br();
     try {
         $this->cli->arguments->parse($args);
     } catch (\Exception $e) {
         $this->cli->usage($args);
         return;
     }
     switch ($this->cli->arguments->get('format')) {
         case 'xml':
             $output = new XmlOutput($this->cli, $this->cli->arguments->get('output'));
             break;
         default:
             $output = new PrettyOutput($this->cli);
             break;
     }
     $nodeTraverser = new NodeTraverser();
     $analyser = new Analyser($this->parser, $nodeTraverser);
     $nodeTraverser->addVisitor(new NodeVisitor($analyser, new Detector()));
     $target = $this->cli->arguments->get('path');
     $targets = [];
     if (is_file($target)) {
         $targets[] = $target;
     } else {
         $directory = new \RecursiveDirectoryIterator($target);
         $iterator = new \RecursiveIteratorIterator($directory);
         $regex = new \RegexIterator($iterator, '/^.+\\.php$/i', \RecursiveRegexIterator::GET_MATCH);
         foreach ($regex as $file) {
             $targets[] = $file[0];
         }
     }
     $result = $analyser->analyse($targets);
     $output->output($result);
 }
 /**
  * @param Result $result
  */
 public function output(Result $result)
 {
     if ($result->getAssumptionsCount() > 0) {
         $this->cli->table($result->getAssumptions())->br();
     }
     $this->cli->out(sprintf('%d out of %d boolean expressions are assumptions (%d%%)', $result->getAssumptionsCount(), $result->getBoolExpressionsCount(), $result->getPercentage()));
 }
 public function setUp()
 {
     $this->file = tempnam(sys_get_temp_dir(), 'xml');
     $this->cli = $this->prophesize(CLImate::class);
     $this->result = $this->prophesize(Result::class);
     $this->xmlOutput = new XmlOutput($this->cli->reveal(), $this->file);
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 public function load($path, array $arguments = [])
 {
     $content = $this->loader->load($path, $arguments);
     $this->cli->out($content);
     $bufferWriter = $this->output->get('buffer');
     return $bufferWriter->get();
 }
Example #11
0
 /**
  * Filter on pattern in file name.
  *
  * @param array $records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in this function.
  */
 public function manipulate($records)
 {
     $numRecs = count($records);
     echo "Filtering {$numRecs} records through the CsvSingleFileByFilename fetcher manipulator.\n";
     // Instantiate the progress bar.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     $record_num = 0;
     $filtered_records = array();
     foreach ($records as $record) {
         $filename = pathinfo($record->{$this->file_name_field}, PATHINFO_FILENAME);
         if (preg_match('/' . $this->allowed_pattern . '/', $filename)) {
             $filtered_records[] = $record;
         }
         $record_num++;
         if ($this->onWindows) {
             print '.';
         } else {
             $progress->current($record_num);
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     return $filtered_records;
 }
 /**
  * Tests each record to see if the extension of the file named in
  * the 'find' field has an extension matching any in the list of
  * extensions defined as manipulator paramters.
  *
  * @param array $all_records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in the
  *   fetcher manipulator.
  */
 public function manipulate($all_records)
 {
     $numRecs = count($all_records);
     echo "Filtering {$numRecs} records through the CdmSingleFileByExtension fetcher manipulator.\n";
     // Instantiate the progress bar if we're not running on Windows.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     $record_num = 0;
     $filtered_records = array();
     foreach ($all_records as $record) {
         if (property_exists($record, 'find') && is_string($record->find) && strlen($record->find)) {
             $ext = pathinfo($record->find, PATHINFO_EXTENSION);
             if (in_array($ext, $this->extensions)) {
                 $filtered_records[] = $record;
             }
             $record_num++;
             if ($this->onWindows) {
                 print '.';
             } else {
                 $progress->current($record_num);
             }
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     return $filtered_records;
 }
Example #13
0
 public function setUp()
 {
     $this->app = $this->getMockBuilder(CliApp::class)->setMethods(['getCli'])->getMock();
     $output = new Output();
     $output->defaultTo('buffer');
     $this->buffer = $output->get('buffer');
     $cli = new CLImate();
     $cli->setOutput($output);
     $this->app->method('getCli')->will($this->returnValue($cli));
 }
Example #14
0
 /**
  * @param $results
  * @param $pary
  * @param $list
  * @param $max_len
  */
 public function exec($results, $pary, $list, $max_len)
 {
     $output = [];
     foreach ($results as $precentage => $result_list) {
         foreach ($result_list as $id => $result) {
             $output[] = ['simlarity' => number_format($result['simlarity'], 2) . '%', 'difference' => number_format($result['difference'], 2) . '%', 'Side A' => $result['a'], 'Side B' => $result['b']];
         }
     }
     $this->climate->table($output);
 }
 /**
  * Render list of registered tasks
  */
 public function actionList()
 {
     $this->importScheduleFile();
     $climate = new CLImate();
     $data = [];
     $row = 0;
     foreach ($this->schedule->getEvents() as $event) {
         $data[] = ['#' => ++$row, 'Task' => $event->getDescription(), 'Expression' => $event->getExpression(), 'Command to Run' => $event->command];
     }
     $climate->table($data);
 }
Example #16
0
 /**
  * connect to the remote server
  * @return bool|\Net_SSH2
  */
 private function connect()
 {
     $ssh = new \Net_SSH2(Setting::getSetting('remote:host'));
     $key = new Crypt_RSA();
     $key->loadKey(file_get_contents(Setting::getSetting('ssh:priv_key')));
     if (!$ssh->login(Setting::getSetting('remote:user'), $key)) {
         $cli = new CLImate();
         $cli->error("Could not connect to server");
         return false;
     }
     return $ssh;
 }
 /**
  * @param $command
  * @param bool $withoutException
  */
 public static function executeCommand($command, $withoutException = false)
 {
     exec($command . " 1> /dev/null 2> /tmp/_genie_output", $output, $returnVar);
     if ($returnVar !== 0 && $withoutException === false) {
         throw new RuntimeException(implode(",", $output));
     } elseif ($returnVar !== 0) {
         $climate = new CLImate();
         $climate->shout("FAILED!");
         $climate->shout(implode(",", $output));
         $climate->shout("See /tmp/_genie_output for failure details.");
     }
 }
Example #18
0
 /**
  * Execute the command.
  *
  * @param \Symfony\Component\Console\Input\InputInterface $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  *
  * @return mixed
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $climate = new CLImate();
     $packages = $this->getPackages();
     if (count($packages) <= 0) {
         return $climate->br()->error('We couldn\'t find any required packages.')->br();
     }
     $versions = $this->getVersions($packages);
     if (count($versions) <= 0) {
         return $climate->br()->out('All dependencies match the latest package versions <green>:)</green>')->br();
     }
     return $climate->br()->columns($versions, 3)->br();
 }
Example #19
0
 /**
  * @param string $filename
  * @return int
  */
 private function getCountLines(string $filename) : int
 {
     $linecount = 0;
     /** @var Progress $progress */
     $progress = $this->climate->progress()->total(10000000);
     $handle = fopen($filename, "r");
     while (!feof($handle)) {
         $progress->advance();
         fgets($handle);
         $linecount++;
     }
     fclose($handle);
     return $linecount;
 }
 /**
  * {@inheritdoc}
  */
 public function process()
 {
     $msg = 'Processing workflow';
     if ($this->workflow instanceof Workflow and $name = $this->workflow->getName()) {
         $msg .= ': ' . $name;
     }
     $this->climate->write($msg . "\n");
     $result = $this->workflow->process();
     $this->climate->info('Time elapsed: ' . $result->getElapsed()->format('%i minute(s) %s second(s)'));
     $this->climate->info('Total processed: ' . $result->getTotalProcessedCount() . ' row(s)');
     if ($errorCount = $result->getErrorCount() > 0) {
         $this->climate->error('Errors: ' . $errorCount);
     }
     return $result;
 }
 /**
  * Inserts tabs before a line.
  *
  * @param int $tab
  * @return CLImate
  */
 protected function tab($tab)
 {
     if ($tab > 0) {
         $this->console->tab($tab);
     }
     return $this->console;
 }
Example #22
0
 /**
  * Creates sample ini file.
  */
 protected function createSampleIniFile()
 {
     $data = "; NOTE: If non-alphanumeric characters are present, enclose in value in quotes.\n\n[staging]\n    quickmode = ftp://example:password@production-example.com:21/path/to/installation\n\n[production]\n    scheme = sftp\n    user = example\n    pass = password\n    host = staging-example.com\n    path = /path/to/installation\n    port = 22";
     if (file_put_contents(getcwd() . DIRECTORY_SEPARATOR . 'phploy.ini', $data)) {
         $this->cli->info("\nSample phploy.ini file created.\n");
     }
 }
Example #23
0
 public function run($args)
 {
     $cli = new CLImate();
     $origin = Setting::getSetting('remote:user') . '@' . Setting::getSetting('remote:host') . ':' . Remote::getFilesPath();
     $dest = Local::getFilesPath();
     if (!file_exists($dest)) {
         if (!mkdir($dest, 0777, true)) {
             $cli->error('Could not create local files directory');
             return false;
         }
     }
     $rsync = new Rsync();
     $rsync->setVerbose(true);
     $rsync->setExclude(Setting::getSetting('rsync:excludes'));
     $rsync->sync($origin, $dest);
 }
Example #24
0
 /**
  *
  * @param string $string
  * @return void
  */
 protected function _log_out($string)
 {
     if (!$this->_logger) {
         return;
     }
     $this->_logger->out($string);
     return;
 }
Example #25
0
 public function run($args)
 {
     $cli = new CLImate();
     $backup_dir = Setting::getSetting('consh:db_backup_folder');
     if (!file_exists($backup_dir)) {
         if (!mkdir($backup_dir, 0777, true)) {
             $cli->error("Could not create database backup folder: {$backup_dir}");
             return false;
         }
     }
     if (count($args) != 1) {
         $cli->error("Please pass along the filename to import");
         if ($handle = opendir($backup_dir)) {
             $cli->out("possible files:");
             while (false !== ($entry = readdir($handle))) {
                 if ($entry != '.' && $entry != '..') {
                     $cli->out($entry);
                 }
             }
         } else {
             $cli->error("Could not open database backup folder");
         }
         return false;
     }
     $file = $args[0];
     $path = $backup_dir . "/" . $file;
     if (!file_exists($path)) {
         $cli->error("{$file} does not exist");
         return false;
     }
     $sql = file($path);
     $db = new LocalDB();
     $templine = '';
     $size = count($sql);
     $cli->out("Restoring database");
     $progress = $cli->progress()->total($size);
     $current = 0;
     foreach ($sql as $line) {
         $current++;
         // Skip it if it's a comment
         if (substr($line, 0, 2) == '--' || $line == '') {
             continue;
         }
         $templine .= $line;
         if (substr(trim($line), -1, 1) == ';') {
             $db->execute($templine);
             $progress->current($current);
             $templine = '';
         }
     }
 }
Example #26
0
 /**
  * Execute the command.
  *
  * @param \Symfony\Component\Console\Input\InputInterface $input
  * @param \Symfony\Component\Console\Output\OutputInterface $output
  *
  * @return mixed
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $climate = new CLImate();
     $climate->br();
     try {
         $packages = $this->ladder->getOutdatedPackages();
         if (!$packages) {
             $climate->write('All dependencies match the latest package versions <green>:)</green>')->br();
             return;
         }
         $outdated = [];
         $upgradable = [];
         foreach ($packages as $name => list($constraint, $version, $latest)) {
             if (Version::satisfies($latest, $constraint)) {
                 $latest = $this->diff($version, $latest);
                 $upgradable[] = [$name, $version, '→', $latest];
             } else {
                 $latest = $this->diff($version, $latest);
                 $outdated[] = [$name, $version, '→', $latest];
             }
         }
         if ($outdated) {
             $climate->columns($outdated, 3)->br();
         }
         if ($upgradable) {
             $climate->write('The following dependencies are satisfied by their declared version constraint, but the installed versions are behind. You can install the latest versions without modifying your composer.json file by using \'composer update\'.')->br();
             $climate->columns($upgradable, 3)->br();
         }
     } catch (ClimbException $exception) {
         $climate->error($exception->getMessage())->br();
     }
 }
Example #27
0
 /**
  * Selects a subset of records based on the value of their record keys.
  *
  * @param array $all_records
  *   All of the records from the fetcher.
  * @return array $filtered_records
  *   An array of records that pass the test(s) defined in the fetcher manipulator.
  */
 public function manipulate($all_records)
 {
     $numRecs = count($all_records);
     echo "Filtering {$numRecs} records through the RangeSet fetcher manipulator.\n";
     // Instantiate the progress bar if we're not running on Windows.
     if (!$this->onWindows) {
         $climate = new \League\CLImate\CLImate();
         $progress = $climate->progress()->total($numRecs);
     }
     // Determine what type of range test we will apply.
     if (preg_match('/^>/', $this->range)) {
         $filter = 'greaterThan';
     }
     if (preg_match('/^</', $this->range)) {
         $filter = 'lessThan';
     }
     if (preg_match('/@/', $this->range)) {
         $filter = 'between';
     }
     // The limit filter uses position in full record set,
     // not value of record key.
     if (preg_match('/,/', $this->range)) {
         $filter = 'limit';
     }
     $record_num = 0;
     $filtered_records = array();
     foreach ($all_records as $record) {
         $record_num++;
         if ($this->{$filter}($record->key, $this->range, $record_num)) {
             $filtered_records[] = $record;
         }
         if ($this->onWindows) {
             print '.';
         } else {
             $progress->current($record_num);
         }
     }
     if ($this->onWindows) {
         print "\n";
     }
     if (count($filtered_records) === 0) {
         $this->log->addError("RangeSet", array('Empty record set' => "The range " . $this->range . " has filtered out all records."));
     }
     return $filtered_records;
 }
Example #28
0
 /**
  * Outputs all the available implementation for the given type (such as Exporters)
  * Unfortunately most of these have to be hardcoded because of autoloading (otherwise we'd use reflection)
  */
 private function showList()
 {
     $type = $this->climate->arguments->get('list');
     $data = [];
     if ($type == 'exporters') {
         $data = [['name' => 'Void', 'description' => 'Does not output anything even if parsing is done. Pretty much useless.'], ['name' => '<bold>StdOut</bold>', 'description' => '<bold>Prints all the data, formatted in a human-readable format, to the standard output.</bold>'], ['name' => 'File', 'description' => 'Writes each game in its own file using the same human-readable format as StdOut'], ['name' => 'CSV', 'description' => 'Writes each game to its own file in the comma-separated-values format'], ['name' => 'MySQL', 'description' => 'Creates the appropriate tables and inserts the data in a MySQL database'], ['name' => 'MySQL-Dump', 'description' => 'Creates a MySQL .sql file with all the required statements to create a database']];
     }
     if (!empty($data)) {
         $this->climate->table($data);
     }
 }
Example #29
0
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $this->options = $this->input->getOptions();
     $this->gpm = new GPM($this->options['force']);
     $this->displayGPMRelease();
     $this->data = $this->gpm->getRepository();
     $data = $this->filter($this->data);
     $climate = new CLImate();
     $climate->extend('Grav\\Console\\TerminalObjects\\Table');
     if (!$data) {
         $this->output->writeln('No data was found in the GPM repository stored locally.');
         $this->output->writeln('Please try clearing cache and running the <green>bin/gpm index -f</green> command again');
         $this->output->writeln('If this doesn\'t work try tweaking your GPM system settings.');
         $this->output->writeln('');
         $this->output->writeln('For more help go to:');
         $this->output->writeln(' -> <yellow>https://learn.getgrav.org/troubleshooting/common-problems#cannot-connect-to-the-gpm</yellow>');
         die;
     }
     foreach ($data as $type => $packages) {
         $this->output->writeln("<green>" . strtoupper($type) . "</green> [ " . count($packages) . " ]");
         $packages = $this->sort($packages);
         if (!empty($packages)) {
             $table = [];
             $index = 0;
             foreach ($packages as $slug => $package) {
                 $row = ['Count' => $index++ + 1, 'Name' => "<cyan>" . Utils::truncate($package->name, 20, false, ' ', '...') . "</cyan> ", 'Slug' => $slug, 'Version' => $this->version($package), 'Installed' => $this->installed($package)];
                 $table[] = $row;
             }
             $climate->table($table);
         }
         $this->output->writeln('');
     }
     $this->output->writeln('You can either get more informations about a package by typing:');
     $this->output->writeln('    <green>' . $this->argv . ' info <cyan><package></cyan></green>');
     $this->output->writeln('');
     $this->output->writeln('Or you can install a package by typing:');
     $this->output->writeln('    <green>' . $this->argv . ' install <cyan><package></cyan></green>');
     $this->output->writeln('');
 }
Example #30
0
 /**
  * Print out the argument list
  *
  * @param array $arguments
  * @param string $type
  */
 protected function outputArguments($arguments, $type)
 {
     if (count($arguments) == 0) {
         return;
     }
     $this->climate->br()->out(ucwords($type) . ' Arguments:');
     foreach ($arguments as $argument) {
         $this->climate->tab()->out($this->argument($argument));
         if ($argument->description()) {
             $this->climate->tab(2)->out($argument->description());
         }
     }
 }